The Filename that will be used to name the email.
Some mail services, such as IMAIL, require multiple files for its service. Other services, such as MS's SMTP service only require 1 filename. Be careful when modifing this event. If the filenames do not match the same schema, as required by the service, the email may not be sent. Test carefully before executing a production run.
The following example uses this table for sample data
| emailaddress | attachmentPath | emailBody |
| bill@mycompany.com | c:\temp\orders\bill.xls | please review your order details |
| steve@hiscompany.com | c:\temp\orders\steve.xls | please review your order details |
| debbie@yourcompany.com | please review your order details |
[C#]
static void Main(string[] args)
{
EmailMessage msg = new EmailMessage();
msg.Server = "mail.mycompany.com";
msg.FromAddress = "me@mycompany.com";
msg.To = "##emailaddress##";
msg.Subject = "see attached orders";
msg.Body = "##emailBody##";
msg.BeforeQueueWrite += new BeforeQueueWriteEventHandler( OnBeforeQueueWrite );
msg.MSPickupDirectory = "c:\\temp\\pickupDirectory\\";
msg.SendMailMergeToMSPickup( GetAttachmentTable() );
}
public static void OnBeforeQueueWrite( object sender, BeforeQueueWriteEventArgs e )
{
//if we wanted to cancel the write we can call
//e.DoWrite = false;
//write out the filename
for( int i=0;i<e.Filenames.Length;i++)
{
Console.WriteLine( e.Filenames[ i ] );
}
//if we wanted to change the pickup directory, we can call
e.Directory = "c:\\temp\\pickupDirectory2\\";
}
public static DataTable GetAttachmentTable()
{
string connectionString = "server=(local);database=northwind;trusted_connection=true;";
string sqlText = "SELECT emailaddress, emailBody FROM testEmailTable";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter( sqlText, connectionString );
da.Fill( dt );
return dt;
}
[Visual Basic]
Sub Main(ByVal args() As String)
Dim msg As New EmailMessage()
msg.Server = "mail.mycompany.com"
msg.FromAddress = "me@mycompany.com"
msg.To = "##emailaddress##"
msg.Subject = "see attached orders"
msg.Body = "##emailBody##"
AddHandler msg.BeforeQueueWrite, AddressOf OnBeforeQueueWrite
msg.MSPickupDirectory = "c:\temp\pickupDirectory\"
msg.SendMailMergeToMSPickup(GetAttachmentTable())
End Sub 'Main
Public Sub OnBeforeQueueWrite(ByVal sender As Object, ByVal e As BeforeQueueWriteEventArgs)
'if we wanted to cancel the write we can call
'e.DoWrite = false;
'write out the filename
Dim i As Integer
For i = 0 To e.Filenames.Length - 1
Console.WriteLine(e.Filenames(i))
Next i
'if we wanted to change the pickup directory, we can call
e.Directory = "c:\temp\pickupDirectory2\"
End Sub 'OnBeforeQueueWrite
Public Function GetAttachmentTable() As DataTable
Dim connectionString As String = "server=(local);database=northwind;trusted_connection=true;"
Dim sqlText As String = "SELECT emailaddress, emailBody FROM testEmailTable"
Dim dt As New DataTable()
Dim da As New SqlDataAdapter(sqlText, connectionString)
da.Fill(dt)
Return dt
End Function 'GetAttachmentTable