We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to send a Email Tempate to multiple mail addresses

Hi

We are using licensed version of Aspose.Total.

I am building a Email component using Aspose.Email based on our need.

I have a requirement of sending a Template to multiple mail addresses. My template has fields(place holder) for FirstName, CompanyName, City, State and Zip code of the customer.

Issue I am facing is, all messages is going to same mail address which is assigned to "To". I did not get way to send to multiple addresses.

Please suggest

Here is my code:

--------------------

private MailMessageCollection BulkMailMerge(string from, string recipient, string subject, string mailTemplate, DataTable data)

{

var message = new MailMessage { From = from, HtmlBody = mailTemplate, Subject = subject };
message.To.Add(recipient);
var messages = new TemplateEngine(message).Instantiate(data);

return messages;
}

public void BulkSend(string from, string recipient, string subject, string mailTemplate, DataTable data)
{
var bulkMessage = BulkMailMerge(from, recipient, subject, mailTemplate, data);
using (var client = new SmtpClient(_smtpConfig.Host, _smtpConfig.Port, _smtpConfig.UserName, _smtpConfig.Password))
{
client.BulkSend(bulkMessage);
}
}

Hi Ramesha,

Thank you for contacting Aspose support team.

Following is a sample code which can be used to send email to multiple recipients. Could you please give it a try and let us know your feedback?

static object GetSignature(object[] args)
{
return “Aspose.Email
Aspose Development Team
Aspose Ltd.
” + DateTime.Now.ToShortDateString();

}
static void PerformMailMerge()
{
//template routine to provide signature
//Create a new MailMessage instance
MailMessage msg = new MailMessage();
msg.PreferredTextEncoding = Encoding.Unicode;

//Add subject and from address
msg.Subject = “Hello, #FirstName#”;
msg.From = "sender@gmail.com";

//Add email address to send email
MailAddress mail = new MailAddress("#Receipt#", true);
msg.To.Add(mail);

//Add mesage field to html body
msg.HtmlBody = “Your message here”;
msg.HtmlBody += “Thank you for your interest in Aspose.Email.”;

//Use GetSignment as the template routine, which will provide the same signature
msg.HtmlBody += “

Have fun with it.

#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”] = “userreceiver1@gmail.com”;
dr[“FirstName”] = “This is first name 1”;
dr[“LastName”] = “bc”;
dt.Rows.Add(dr);
dr = dt.NewRow();

dr[“Receipt”] = “Development Teamreceiver2@gmail.com”;
dr[“FirstName”] = “This is first name 2”;
dr[“LastName”] = “Team”;
dt.Rows.Add(dr);
dr = dt.NewRow();

dr[“Receipt”] = “Support Teamreceiver3@gmail.com”;
dr[“FirstName”] = “This is first name 3”;
dr[“LastName”] = “Huang”;
dt.Rows.Add(dr);

MailMessageCollection messages;

try

{
//Create messages from the message and datasource.
messages = engine.Instantiate(dt);

//Create an instance of SmtpClient and specify server, port, username and password
SmtpClient client = new SmtpClient(“smtp.gmail.com”, 587, “senderuser”, “password”,SecurityOptions.SSLExplicit);

//Send messages in bulk
client.BulkSend(messages);
}
catch (MailException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
catch (SmtpException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}