Problem with accents during Email merge

Hi,


I have a little problem with aspose email (.net 4.0, version 1.9.0.0) merge function: the accented characters are replaced with question marks ‘?’ when I do a merge.

Here is some code:

TemplateEngine engine = new TemplateEngine(_myMailMessage);

MailMessageCollection __mergedMessageCollection = engine.Instantiate(_datasetParticipantInfos.Tables[“General”]);

_myMailMessageMerged = __mergedMessageCollection[0];

  • _myMailMessage is a MailMessage with some ‘mergefields’ like ‘#FirstName#’
  • _datasetParticipantInfos.Tables[“General”] contains the infos to merge with, coming from sql storred proc

When I do merge with ‘normal characters’, it just works. My problem occurs when I try to merge with accented characters (é, è, ô): all these are replaced with ‘?’.
I’ve tried to set the _myMailMessage.preferedEncoding and myMailMessageMerged.preferedEncoding to “UTF8” or “Unicode”, but the result is the same.

While debuging in Visual Studio, i can see this in the ‘engine’ value:
"Subject: Rejected email subject #FirstName# #LastName# \r\nFrom: test@sharpdev.com\r\nTo: p@gmail.com\r\nMIME-Version: 1.0\r\nContent-Type: multipart/alternative;\r\n boundary="–=NextPart0_f86c5f24-eb99-4d05-b0c9-9b1739715bf2"\r\n\r\n\r\n----=NextPart0_f86c5f24-eb99-4d05-b0c9-9b1739715bf2\r\nContent-Type: text/plain; charset=us-ascii\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\nHello #FirstName# #LastName#,

We are sorry to inform you that =\r\nyour registration has been rejected

\r\n----
=NextPart0_f86c5f24-eb99-4d05-b0c9-9b1739715bf2\r\nContent-Type: text/html; charset=us-ascii\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\nHello #FirstName# #LastName#,

We are sorry to inform you that =\r\nyour registration has been rejected

\r\n----
=_NextPart0_f86c5f24-eb99-4d05-b0c9-9b1739715bf2–\r\n\r\n"

the ’
TemplateEngine ’ seems to be set to ASCII encoding, and I can’t see any public property of the TemplateEngine where I could change the encoding to UTF8. And I don’t know if that would be a solution to my problem.

I’m also using Aspose Words with MailMerge based on the same dataset, and I don’t have that kind of problems with the accented characters.

Thank you

Hi Jerome,


Thank you for using Aspose.Email.

I can notice/reproduce the issue at my end using Mail merge with Data table. I have forwarded these details to our development team for further investigation. Once the issue is resolved or we have any update from our development team, we will let you know here. We appreciate your patience in this regard.

The issue has been logged in our Issue Tracking System as: NETWORKNET-33372.

Hello,


We’ve been able to resolve part of this problem by setting the MailMessage.BodyEncoding Encoding to 'Encoding.GetEncoding(1252)" just after the creation of the mailMessage (before setting the HtmlBody value, and before creating the TemplateEngine wth this MailMessage).

But it seems it’s not possible to handle the problem with the subject in the same way.

Thank you

Hi Jerome,


Thank you for the feedback.

I have logged your additional comments/findings against the issue ID: NETWORKNET-33372. We will update you here once we have any information regarding the resolution of this issue from our development team. We appreciate your patience in this regard.

Hi,

Thank you for being patient towards the resolution of this issue.

We have further investigated this issue and, hopefully, we will provide a fix for this in the upcoming release of Aspose.Email for .NET. In the meanwhile, you can use the following work around to manage your needs. We appreciate your patience in this regard.

static void Main(string[] args)
{
    License license = new License();
    license.SetLicense("K://Aspose.Total.Product.Family.lic");

    PerformMailMerge();

    Console.WriteLine("Press any key to continue...");
    Console.ReadKey();
}

static void PerformMailMerge()
{
    MailMessage msg = new MailMessage();
    msg.BodyEncoding = Encoding.UTF8;
    msg.Subject = "Hello, #FirstName_HdrEnc#";
    msg.From = "someone@gmail.com";
    msg.To.Add("someone@gmail.com");
    msg.HtmlBody = "Hello #FirstName_BodyEnc#, Your message here";
    msg.HtmlBody += "Thank you for your interest in Aspose.Email.";
    msg.HtmlBody += "\nHave fun with it.\n";

    TemplateEngine engine = new TemplateEngine(msg);
    DataTable dt = new DataTable();
    dt.Columns.Add("FirstName_BodyEnc", typeof(string));
    dt.Columns.Add("FirstName_HdrEnc", typeof(string));
    dt.Columns.Add("LastName", typeof(string));

    DataRow dr;
    dr = dt.NewRow();
    string value = "éèôsaadd";
    dr["FirstName_HdrEnc"] = EncodeHeaderValue(value, Encoding.UTF8);
    dr["FirstName_BodyEnc"] = EncodeValue(value, Encoding.UTF8);
    dr["LastName"] = "Iqbal";
    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, "username", "password");

    // set the port to 587. This is the SSL port of Gmail SMTP server
    client.Port = 587;

    // set the security mode to explicit
    client.SecurityMode = SmtpSslSecurityMode.Explicit;

    // enable SSL
    client.EnableSsl = true;

    //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());
}

private static string EncodeHeaderValue(string value, Encoding encoding)
{
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.Append("=?");
    stringBuilder.Append(encoding.BodyName);
    stringBuilder.Append("?");
    stringBuilder.Append("Q");
    stringBuilder.Append("?");
    stringBuilder.Append(EncodeValue(value, encoding));
    stringBuilder.Append("?=");
    
    return stringBuilder.ToString();
}

private static string EncodeValue(string value, Encoding encoding)
{
    byte[] bytes = encoding.GetBytes(value);
    
    StringBuilder hex = new StringBuilder(bytes.Length * 2);
    
    foreach (byte b in bytes)
        hex.AppendFormat("={0:x2}", b);
    
    return hex.ToString();
}