aspNetEmail

EmailMessage.BeginSendMailMerge Method 

Begins an asynchronous request so send a mail merge.

public IAsyncResult BeginSendMailMerge(
   DataTable dataTable,
   int batchSize,
   int pause,
   AsyncCallback callBack,
   object state
);

Parameters

dataTable
The datatable of information.
batchSize
The number of emails to send at one time (set to -1 to send all).
pause
The number of milliseconds to pause between batches.
callBack
The AsyncCallback
state
The State object

Return Value

An IAsyncResult instance that references the asynchronous request.

Example

[C#]
            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();
             
                //send all the emails by setting BatchSize =-1, and Pause=-1
                message.BeginSendMailMerge( dt, -1, -1, new AsyncCallback( MyCallback ), message );
             
             
            }
             
            //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.MessageId );
                
            }
             
            void MyCallback( IAsyncResult result )
            {
                EmailMessage message = (EmailMessage)result.AsyncState;
                bool Success;
                try
                {
                    Success = message.EndSendMailMerge( result );
                }
                catch(Exception ex )
                {
                    Success = false;
                    Console.WriteLine( ex.Message );
                }
                Console.WriteLine( "success: " + Success );
            }
                
[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()
               
               'send all the emails by setting BatchSize =-1, and Pause=-1
               message.BeginSendMailMerge(dt, - 1, - 1, New AsyncCallback(MyCallback), message)
            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.MessageId)
            End Sub 'OnRowSent
             
             
            Sub MyCallback(result As IAsyncResult)
               Dim message As EmailMessage = CType(result.AsyncState, EmailMessage)
               Dim Success As Boolean
               Try
                  Success = message.EndSendMailMerge(result)
               Catch ex As Exception
                  Success = False
                  Console.WriteLine(ex.Message)
               End Try
               Console.WriteLine(("success: " + Success))
            End Sub 'MyCallback
                

See Also

EmailMessage Class | aspNetEmail Namespace