Our company bought Aspose.email for .NET a couple of months ago and I was wondering if you could provide me the following:
Hi Luis,
Thank you for writing to Aspose support team.
I have analyzed the requirement and observed that there is no direct method to embed bar code images into MailMessage using template engine. However a workaround is given here for your reference.
lucho1981:
- an example on how to include a different Bar-code for each mail merge email utilizing email templates and bulk-send. We are planning on sending less than 1 million emails each with a different Bar-code and additional custom data. Both the Bar-code number and custom data will be stored in a database table-row or view-row
Comments:
Please give a try to the following sample code by using your own DataTable and let us know the feedback:
static private void Email_668794()
{
//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(“PlainText”, typeof(string));
dt.Columns.Add(“Barcode”, typeof(byte[]));
//Create an instance of DataRow
DataRow dr;
dr = dt.NewRow();
dr[“Receipt”] = “newcustomeronnet@gmail.com>”;
dr[“FirstName”] = “a”;
dr[“PlainText”] = “bc”;
byte[] imageByte1 = {1,2,3,4,5,6,7,8,9,10};//dummy barcode bytes
dr[“Barcode”] = imageByte1;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[“Receipt”] = “email@domain.com”;
dr[“FirstName”] = “Guangzhou”;
dr[“PlainText”] = “Team”;
byte[] imageByte2 = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };//dummy barcode bytes
dr[“Barcode”] = imageByte2;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[“Receipt”] = “email.address@domain.com>”;
dr[“FirstName”] = “Kyle”;
dr[“PlainText”] = “Huang”;
byte[] imageByte3 = { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };//dummy barcode bytes
dr[“Barcode”] = imageByte3;
dt.Rows.Add(dr);
MailMessageCollection messages = new MailMessageCollection();
foreach (DataRow dataRow in dt.Rows)
{
MailMessage mail = new MailMessage();//You may load some template mail here like MailMessage mail = new MailMessage(“teamplate.msg”);
//Set the content
mail.Subject = "Hello " + dr[“FirstName”];
//Set the addresses
mail.From = new MailAddress(“sender@receiver”);
mail.To.Add(dataRow[“Receipt”].ToString());
//Create the plain text part
//It is viewable by those clients that don’t support HTML
AlternateView plainView =AlternateView.CreateAlternateViewFromString(dr[“PlainText”].ToString(), null,“text/plain”);
//Create the HTML part.
//To embed images, we need to use the prefix ‘cid’ in the img src value.
//The cid value will map to the Content-Id of a Linked resource.
//Thus will map to a LinkedResource with a ContentId of //‘companylogo’.
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(“Here is an embedded image.”, null, “text/html”);
//create the LinkedResource (embedded image)
Stream stream = new MemoryStream((byte[])dr[“Barcode”]);
LinkedResource logo = new LinkedResource(stream, MediaTypeNames.Image.Png);
logo.ContentId = “companylogo”;
//Add the LinkedResource to the appropriate view
mail.LinkedResources.Add(logo);
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
messages.Add(mail);
}
try
{
SmtpClient client = new SmtpClient(“smtp.gmail.com”, 587, “testuser”,“password”);
client.SecurityOptions = SecurityOptions.SSLExplicit;
SmtpClient client = GetSmtpClient();
//Send messages in bulk
client.BulkSend(messages);
}
catch (Aspose.Email.Mail.MailException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
catch (SmtpException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
lucho1981:
- An example on how to catch all the failures without stopping the email dispatch process until it is done attempting to send all the emails.
Comments:
You may please log the Smtp client activity as given here.
lucho1981:
- An example on how to step one by one (if so desired) to view the merged results (messages with specific details already composed, including bar-code) and so test/approve them before actually dispatching the emails.
Comments:
As mentioned in the first example you may please save/observe all the messages being prepared before sending through BulkSend.
Please feel free to write us back if you have any other query in this regard.
Thanks for your reply Muhammad, following up a bit on my questions:
Hi,
Hello Muhammad.
Hi,
We are analyzing the issues and preparing the sample code which may fulfill your requirements. Please spare us little time to complete the task and provide assistance accordingly.
lucho1981:Different data includes different images, a bar code number (generated Image), a couple of links, different body line items like salutation, and some other details.
lucho1981:
1. Utilize a single message-template2. utilizing a reader that reads the dynamic-data one a a time (and not all of them placed into a datatable)3. A simple way to merge the message-template with the dynamic-data generating a rendered message that can then be:3.1. Returned for previewing and aproval or3.2 Dispatched if so desired so that the recipients gets their email.Notice that 3.1 and 3.2 are an or condition that means two separate paths and times, the reviewing is one path and time, the dispatching is another path and time
lucho1981:
Q1. It seems that you are saying that I can not utilize the template engine because that way we can not include bar-code images. So how do I merge the dynamic-data with the message template if not utilizing the template engine. What Aspose functionality do I utilize to merge if I can not utilize the template engine because of the bar-codes?
lucho1981:
Q.2 If I have to replace the message-template keys-data with my own code to search for the keys and replaces them with the dynamic-data row, and then generate the MailMessage and then I will have to Add the MailMessage to the MalMessageCollection. Could the MailMEssageColleciton hold 1 million items completely rendered at the same time? what is the memory foot print for such case? in other words what is the size of the MAilMessage Object MailMessage Collection object if the content I place in them is just what I mentioned above (no attachments)
long workingSetStart = System.Diagnostics.Process.GetCurrentProcess().WorkingSet64;
//Create and add messages here
long workingSetEnd = System.Diagnostics.Process.GetCurrentProcess().WorkingSet64;
//Get the difference of both the WorkingSet.
lucho1981:
Q3. I believe this requirements are nothing out of the ordinary. No small data-table examples will work, or some sort of asynchronous methods barely mentioned in different places. Is there a good example that shows a more mature case of sending 1 million emails with the above requirements? If I have to do the template merging myself, If I have to do something like the Parallel.ForEach(..) myself. And if I am utilizing Aspose just to send the emails that I created with all this work. then the Aspose functionality has very little usability right? I really hope you can provide me some sort of answer that is complete and consistent throughout