These args are raised in the BeforeRowMergedEvent
For a list of all members of this type, see BeforeRowMergeEventArgs Members.
System.Object
EventArgs
BeforeRowMergeEventArgs
Exposes the DataRow about to me merged with the EmailMessage object.
This following example, demonstrates the possibility of building a complex email, that cannot be handled internally, by aspNetEmail. In this example, a custom footer is added to the email message. Although this particular example could be handled, internally, by aspNetEmail, it is meant more as a demonstration of the possibility.
[CS]
public static void SendToMsPickup( string path )
{
DataTable dt = GetDataTable();
EmailMessage m = new EmailMessage();
m.BeforeRowMerge += new BeforeRowMergeEventHandler( OnBeforeRowMerge );
m.FromAddress = "blah@blah.com";
m.To = "##fldEmail##";
m.Subject = "hello ##fldFirstName##";
m.Body = "this is a test.";
m.ThrowException = false;
m.SendMailMergeToMSPickup( dt, path );
}
public static void OnBeforeRowMerge( object sender, BeforeRowMergeEventArgs e )
{
DataRow dr = e.DataRow;
//in this example, the emailAddress happens to be in the first column
string emailAddress = dr[ 0 ].ToString();
Console.WriteLine( emailAddress );
//change the body to add some custom data, based upon the email address
string customData = BuildCustomData( emailAddress );
//now, modify the body
EmailMessage m = (EmailMessage)sender;
m.Body += m.Body + customData;
}
static DataTable GetDataTable()
{
OleDbConnection oConn;
OleDbDataAdapter oDa;
string sConn;
string sqlText;
DataSet oDataSet = new DataSet();
//Build the connection string
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MailMerge.mdb;Persist Security Info=False";
//Build the SQL string
sqlText = "SELECT fldEmail, fldFirstName, fldDatePurchased, fldAmount FROM tblMembers";
//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
oConn = new OleDbConnection(sConn);
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;
}
public static string BuildCustomData( string emailAddress )
{
//this method is used to build custom data, that is too complex to be handed internally by aspNetEmail's MailMerge process.
//This example is based upon the email address, which builds a simple custom string footer
string footer = "\r\n-------------------------------------------------------\r\n";
footer += "This email was inteded for " + emailAddress + "\r\n";
footer += "-------------------------------------------------------\r\n";
return footer;
}
[Visual Basic]
Public Shared Sub SendToMsPickup(path As String)
Dim dt As DataTable = GetDataTable()
Dim m As New EmailMessage()
AddHandler m.BeforeRowMerge, AddressOf OnBeforeRowMerge
m.FromAddress = "blah@blah.com"
m.To = "##fldEmail##"
m.Subject = "hello ##fldFirstName##"
m.Body = "this is a test."
m.ThrowException = False
m.SendMailMergeToMSPickup(dt, path)
End Sub 'SendToMsPickup
Public Shared 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 emailAddress As String = dr(0).ToString()
Console.WriteLine(emailAddress)
'change the body to add some custom data, based upon the email address
Dim customData As String = BuildCustomData(emailAddress)
'now, modify the body
Dim m As EmailMessage = CType(sender, EmailMessage)
m.Body += m.Body + customData
End Sub 'OnBeforeRowMerge
Shared Function GetDataTable() As DataTable
Dim oConn As OleDbConnection
Dim oDa As OleDbDataAdapter
Dim sConn As String
Dim sqlText As String
Dim oDataSet As New DataSet()
'Build the connection string
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MailMerge.mdb;Persist Security Info=False"
'Build the SQL string
sqlText = "SELECT fldEmail, fldFirstName, fldDatePurchased, fldAmount FROM tblMembers"
'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
oConn = New OleDbConnection(sConn)
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
Public Shared Function BuildCustomData(emailAddress As String) As String
'this method is used to build custom data, that is too complex to be handed internally by aspNetEmail's MailMerge process.
'This example is based upon the email address, which builds a simple custom string footer
Dim footer As String = ControlChars.Cr + ControlChars.Lf + "-------------------------------------------------------" + ControlChars.Cr + ControlChars.Lf
footer += "This email was inteded for " + emailAddress + ControlChars.Cr + ControlChars.Lf
footer += "-------------------------------------------------------" + ControlChars.Cr + ControlChars.Lf
Return footer
End Function 'BuildCustomData
Namespace: aspNetEmail
Assembly: aspNetEmail (in aspNetEmail.dll)