Hi,
Thanks for considering Aspose.
For sending template based emails please refer to the article at http://www.aspose.com/documentation/file-format-components/aspose.network-for-.net/performing-mail-merge.html. Since, you already implemented mail merge (creating template and filling email fields dynamically), please refer to http://www.aspose.com/documentation/file-format-components/aspose.network-for-.net/working-with-embedded-objects.html for inserting images as embedded objects in email body.
Below is the sample code to send emails with embedded image using mail merge:
//template routine to provide signature
static object GetSignature(object[] args)
{
return "Aspose.Network
Aspose Ltd.
" + DateTime.Now.ToShortDateString();
}
--------------------------------------------------
Write the following code in some even handler e.g. method invoked on button click :
//Create a new MailMessage instance
MailMessage msg = new MailMessage();
//Add subject and from address
msg.Subject = "Hello, #FirstName#";
msg.From = "sender@domain.com";
//Add email address to send email
msg.To.Add("sender@domain.com");
//Add mesage field to html body
msg.HtmlBody = "Your message here";
msg.HtmlBody += "Thank you for your interest in Aspose.Network.
Here is an embedded image.
";
//Use GetSignment as the template routine, which will provide the same signature
msg.HtmlBody += "
HTML tags can be added here.
#GetSignature()#";
//Create a new TemplateEngine with the msg message.
TemplateEngine engine = new TemplateEngine(msg);
// Register GetSignature routine. It will be used in msg.
engine.RegisterRoutine("GetSignature", new TemplateRoutine(GetSignature));
//Create an instance of DataTable
//Fill a DataTable as data source
DataTable dt = new DataTable();
dt.Columns.Add("Receipt", typeof(string));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
//Create an instance of DataRow
DataRow dr;
dr = dt.NewRow();
dr["Receipt"] = "Name";
dr["FirstName"] = "First";
dr["LastName"] = "Last";
dt.Rows.Add(dr);
MailMessageCollection messages;
try
{
//Create messages from the message and datasource.
messages = engine.Instantiate(dt);
LinkedResource logo = new LinkedResource("c:\\test.bmp", MediaTypeNames.Image.Jpeg);
logo.ContentId = "companylogo";
foreach (MailMessage msg1 in messages)
{
msg1.LinkedResources.Add(logo);
// msg1.Save("C:\\test.eml");
}
//Create an instance of SmtpClient and specify server, port, username and password
SmtpClient client = new SmtpClient("mail.domain.com", 25, "username", "password");
//Send messages in bulk
client.BulkSend(messages);
MessageBox.Show("Messagen sent");
}
catch (MailException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
catch (SmtpException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}