aspNetEmail

EmailMessage.SendMailMergeToMSPickup Method (DataSet, String)

Performs a mail merge, from data found in the first DataTable of the dataset, and sends the resulting emails to the Microsoft SMTPsvc Pickup directory.

public bool SendMailMergeToMSPickup(
   DataSet dataSet,
   string MSPickupDirectoryPath
);

Parameters

dataSet
The DataSet that contains the DataTable of mail merge info.
MSPickupDirectoryPath
Filesystem path to the Pickup directory.

Return Value

True or False for the success of the mail merge.

Remarks

Locating the MS Pickup directory. aspNetEmail will intelligently try to determine the MS Pickup directory. It uses the following steps to locate the directory.

  1. If a SendToMSPickup() method accepts the MSPickupDirectoryPath, it will use that value.
  2. If MSPickupDirectoryPath is not used, the aspNetEmail will attempt to use the value of EmailMessage.MSPickupDirectory property.
  3. If that property is not set, it inspects the Application configuration files (app.config or web.config) or the machine.config for the value aspNetEmail.MSPickupDirectory under the <appSettings key> This value can be placed in the machine.config by administrators to hide the location of the MS Pickup directory, if they do not want to users of aspNetEmail to locate it.
  4. If none of the above values are found, aspNetEmail will attempt to scan the registry to look for the MS Pickup spool directory that was set during the installation of the SMTP Service.
Please note it is highly recommended you test sending email using the standard Send(), to be sure your code works, before moving on to this method.

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
                DataSet ds =  GetDataSet();
             
                message.SendMailMergeToMSPickup( ds, "c:\\inetpub\\mailroot\\pickup\\" );
             
             
            }
             
            //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 DataSet GetDataSet()
            {
                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");
                return oDataSet;
             
            }
                
[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 ds As DataSet = GetDataSet()
               
               message.SendMailMergeToMSPickup(ds, "c:\inetpub\mailroot\pickup\")
            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 GetDataSet() As DataSet
               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")
               Return oDataSet
            End Function 'GetDataTable
                

See Also

EmailMessage Class | aspNetEmail Namespace | EmailMessage.SendMailMergeToMSPickup Overload List