Performs a mailmerge using data found in the DataTable, and sends the emails to Microsoft's SMTPSvc Pickup Queue directory.
True or False for a successful mail merge
Locating the MS Pickup directory. aspNetEmail will intelligently try to determine the MS Pickup directory. It uses the following steps to locate the directory.
public void MailMerge()
{
EmailMessage message = new EmailMessage( "127.0.0.1" );
//demonstrate the BeforeRowMerge event
message.BeforeRowMerge += new BeforeRowMergeEventHandler( OnBeforeRowMerge );
//demonstrate the MergedRowSent event
message.MergedRowSent += new MergedRowSentEventHandler( OnRowSent );
message.FromAddress = "me@mycompany.com";
message.ThrowException = false;
message.LogOverwrite = true;
//matches a column name in the tabled named 'fldEmail'
message.To = "##fldEmail##";
//matches a column name in the tabled named 'fldFirstName'
message.Subject = "hello ##fldFirstName##";
//set the text part
message.TextBodyPart = "Hi ##fldFirstName##,\r\n";
message.TextBodyPart += "Here is your daily newsletter....";
//set the html part
message.HtmlBodyPart = "<html><body>Hi <b>##fldFirstName##</b>,\r\n";
message.HtmlBodyPart += "Here is your daily newsletter....";
message.HtmlBodyPart += "</body></html>";
message.LogBody = false;
message.LogPath = "c:\\aspnetemail.log";
message.Logging = true;
//load the datatable
DataTable dt = GetDataTable();
message.SendMailMergeToMSPickup( dt );
}
//This event is raised before each row is merged into the EmailMessage object
public void OnBeforeRowMerge( object sender, BeforeRowMergeEventArgs e )
{
DataRow dr = e.DataRow;
//in this example, the emailAddress happens to be in the first column
string emailAddresses = dr[ "fldEmail" ].ToString();
//manually add in the address
((EmailMessage)sender).AddTo( "customto@custom.com" );
Console.WriteLine( emailAddresses );
}
//This event is raised after each row is sent
public void OnRowSent( object sender, MergedRowSentEventArgs e )
{
EmailMessage m = (EmailMessage)sender;
DataRow dr = e.Row;
string emailAddress = dr[ 0 ].ToString();
Console.WriteLine( emailAddress );
Console.WriteLine( e.Success );
Console.WriteLine( m.SmtpData.Length );
Console.WriteLine( m.MessageId );
}
private DataTable GetDataTable()
{
OleDbDataAdapter oDa;
DataSet oDataSet = new DataSet();
//Usually you would use error-handling here. It is left out to make the code as simple as possible.
//Create the connection and command objects
string sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MailMerge.mdb;Persist Security Info=False";
OleDbConnection oConn = new OleDbConnection(sConn);
string sqlText = "SELECT fldEmail, fldFirstName, fldDatePurchased, fldAmount FROM tblMembers";
oDa = new OleDbDataAdapter(sqlText, oConn);
//Fill the dataset with the results of the query
oDa.Fill(oDataSet, "tblMailMerge");
DataTable dtMailMerge = oDataSet.Tables[0];
return dtMailMerge;
}
[Visual Basic]
Public Sub MailMerge()
Dim message As New EmailMessage("127.0.0.1")
'demonstrate the BeforeRowMerge event
AddHandler message.BeforeRowMerge, AddressOf OnBeforeRowMerge
'demonstrate the MergedRowSent event
AddHandler message.MergedRowSent, AddressOf OnRowSent
message.FromAddress = "me@mycompany.com"
message.ThrowException = False
message.LogOverwrite = True
'matches a column name in the tabled named 'fldEmail'
message.To = "##fldEmail##"
'matches a column name in the tabled named 'fldFirstName'
message.Subject = "hello ##fldFirstName##"
'set the text part
message.TextBodyPart = "Hi ##fldFirstName##," + ControlChars.Cr + ControlChars.Lf
message.TextBodyPart += "Here is your daily newsletter...."
'set the html part
message.HtmlBodyPart = "<html><body>Hi <b>##fldFirstName##</b>," + ControlChars.Cr + ControlChars.Lf
message.HtmlBodyPart += "Here is your daily newsletter...."
message.HtmlBodyPart += "</body></html>"
message.LogBody = False
message.LogPath = "c:\aspnetemail.log"
message.Logging = True
'load the datatable
Dim dt As DataTable = GetDataTable()
message.SendMailMergeToMSPickup(dt)
End Sub 'MailMerge
'This event is raised before each row is merged into the EmailMessage object
Public Sub OnBeforeRowMerge(sender As Object, e As BeforeRowMergeEventArgs)
Dim dr As DataRow = e.DataRow
'in this example, the emailAddress happens to be in the first column
Dim emailAddresses As String = dr("fldEmail").ToString()
'manually add in the address
CType(sender, EmailMessage).AddTo("customto@custom.com")
Console.WriteLine(emailAddresses)
End Sub 'OnBeforeRowMerge
'This event is raised after each row is sent
Public Sub OnRowSent(sender As Object, e As MergedRowSentEventArgs)
Dim m As EmailMessage = CType(sender, EmailMessage)
Dim dr As DataRow = e.Row
Dim emailAddress As String = dr(0).ToString()
Console.WriteLine(emailAddress)
Console.WriteLine(e.Success)
Console.WriteLine(m.SmtpData.Length)
Console.WriteLine(m.MessageId)
End Sub 'OnRowSent
Private Function GetDataTable() As DataTable
Dim oDa As OleDbDataAdapter
Dim oDataSet As New DataSet()
'Usually you would use error-handling here. It is left out to make the code as simple as possible.
'Create the connection and command objects
Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MailMerge.mdb;Persist Security Info=False"
Dim oConn As New OleDbConnection(sConn)
Dim sqlText As String = "SELECT fldEmail, fldFirstName, fldDatePurchased, fldAmount FROM tblMembers"
oDa = New OleDbDataAdapter(sqlText, oConn)
'Fill the dataset with the results of the query
oDa.Fill(oDataSet, "tblMailMerge")
Dim dtMailMerge As DataTable = oDataSet.Tables(0)
Return dtMailMerge
End Function 'GetDataTable
EmailMessage Class | aspNetEmail Namespace | EmailMessage.SendMailMergeToMSPickup Overload List