aspNetEmail

EmailMessage.BeginSendMailMergeToMSPickup Method 

Begins an asynchronous request so send a mail merge.

public IAsyncResult BeginSendMailMergeToMSPickup(
   DataTable dataTable,
   string MSPickupDirectoryPath,
   AsyncCallback callBack,
   object state
);

Parameters

dataTable
The datatable of information.
MSPickupDirectoryPath
The file system path to the MS Pickup spool directory.
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();
             
                message.BeginSendMailMergeToMSPickup( dt, null, 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.EndSendMailMergeToMSPickup( result );
                }
                catch(Exception ex )
                {
                    Success = false;
                    Console.WriteLine( ex.Message );
                }
                Console.WriteLine( "success: " + Success );
            }
             
             
             
             
             
            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.BeginSendMailMergeToMSPickup(dt, Nothing, 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.EndSendMailMergeToMSPickup(result)
               Catch ex As Exception
                  Success = False
                  Console.WriteLine(ex.Message)
               End Try
               Console.WriteLine(("success: " + Success))
            End Sub 'MyCallback
             
             
             
             
             
             
            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
                

See Also

EmailMessage Class | aspNetEmail Namespace | SendMailMergeToMSPickup | SendMailMergeToMSPickup | SendMailMergeToMSPickup