Problem sending e-mail with subject when it contain accent

  • Thread starter Thread starter Simon Chang
  • Start date Start date
S

Simon Chang

I use System.Net.Mail.MailMessage to send e-mail.
When the subject containt accent and the string is longer then 179 characters.
The receiver of the e-mail get "=?utf-8?B?MSDDqcOpw6kgdGVzdCBEVVAgOiBI..."
in the subject instead of the original subject.

Here is the code :

// Case 1 with problem in the subject: The receiver of the e-mail will
get the next string in the subject
"=?utf-8?B?MSDDqcOpw6kgdGVzdCBEVVAgOiBIMDkwMDAwMDExNSAqKiogZXNzYWkgZCdlbnZveWVyIHVuIGF2aXMgc2FucyBkb3NzaWVyIGRlIHJhdHRhY2hlLjUuLi4uLi4uLi42Li4uLi4uLi4uNy4uLi4uLi4uLjguLi4uLi4uLi45Li4uLi4uLi4uMCAvIGVzc2FpIGQnZW52b3llciB1biBhdmlzIHNhbnMgZG9zc2llciBkZSByYX..."
SendMail("(e-mail address removed)", "(e-mail address removed)", "1 ééé test
DUP : XXXXXXXXXXX *** essai d'envoyer un XXXX sans dossier de
rattache.5.........6.........7.........8.........9.........0 / essai
d'envoyer un XXXX sans dossier de rattache", "testé");


// Case 2 no problem in the subject: The receiver of the e-mail will get
the right string in the subject : 2 é test DUP
SendMail("(e-mail address removed)", "(e-mail address removed)", "2 é test
DUP", "testé");


private void SendMail(string sender, string receiver, string subject,
string body)
{
System.Net.Mail.SmtpClient __smtpClient = null;
__smtpClient = new System.Net.Mail.SmtpClient("....");

using (System.Net.Mail.MailMessage __mailMessage = new
System.Net.Mail.MailMessage(
sender, receiver))
{
__mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
__mailMessage.Subject = subject;
__mailMessage.IsBodyHtml = true;
__mailMessage.Body = body;
__smtpClient.Send(__mailMessage);
}
}

I try different approach by replacing the "é" by [&ecute;] or [/u00EF] or
[É] or [&#Xc9] but get the same result.
 
Try adding

MailAddress to = new MailAddress(receiver)
MailAddress from = new MailAddress(sender)

using (System.Net.Mail.MailMessage __mailMessage = new
System.Net.Mail.MailMessage( sender, receiver))
{
.............
}
 
sorry that should be
MailAddress to = new MailAddress(receiver)
MailAddress from = new MailAddress(sender)

using (System.Net.Mail.MailMessage __mailMessage = new
System.Net.Mail.MailMessage(from, to))
{
.............
}




Mel Weaver said:
Try adding

MailAddress to = new MailAddress(receiver)
MailAddress from = new MailAddress(sender)

using (System.Net.Mail.MailMessage __mailMessage = new
System.Net.Mail.MailMessage( sender, receiver))
{
.............
}



Simon Chang said:
I use System.Net.Mail.MailMessage to send e-mail.
When the subject containt accent and the string is longer then 179
characters.
The receiver of the e-mail get
"=?utf-8?B?MSDDqcOpw6kgdGVzdCBEVVAgOiBI..."
in the subject instead of the original subject.

Here is the code :

// Case 1 with problem in the subject: The receiver of the e-mail will
get the next string in the subject
"=?utf-8?B?MSDDqcOpw6kgdGVzdCBEVVAgOiBIMDkwMDAwMDExNSAqKiogZXNzYWkgZCdlbnZveWVyIHVuIGF2aXMgc2FucyBkb3NzaWVyIGRlIHJhdHRhY2hlLjUuLi4uLi4uLi42Li4uLi4uLi4uNy4uLi4uLi4uLjguLi4uLi4uLi45Li4uLi4uLi4uMCAvIGVzc2FpIGQnZW52b3llciB1biBhdmlzIHNhbnMgZG9zc2llciBkZSByYX..."
SendMail("(e-mail address removed)", "(e-mail address removed)", "1 ééé test
DUP : XXXXXXXXXXX *** essai d'envoyer un XXXX sans dossier de
rattache.5.........6.........7.........8.........9.........0 / essai
d'envoyer un XXXX sans dossier de rattache", "testé");


// Case 2 no problem in the subject: The receiver of the e-mail will
get
the right string in the subject : 2 é test DUP
SendMail("(e-mail address removed)", "(e-mail address removed)", "2 é test
DUP", "testé");


private void SendMail(string sender, string receiver, string subject,
string body)
{
System.Net.Mail.SmtpClient __smtpClient = null;
__smtpClient = new System.Net.Mail.SmtpClient("....");

using (System.Net.Mail.MailMessage __mailMessage = new
System.Net.Mail.MailMessage(
sender, receiver))
{
__mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
__mailMessage.Subject = subject;
__mailMessage.IsBodyHtml = true;
__mailMessage.Body = body;
__smtpClient.Send(__mailMessage);
}
}

I try different approach by replacing the "é" by [&ecute;] or [/u00EF] or
[É] or [&#Xc9] but get the same result.
 
The problem is not sending the e-mail.

The problem is how the receiver get the e-mail.

In the receiver mailbox, if he open the e-mail, the subject will be :
=?utf-8?B?MSDDqcOpw6kgdGVzdCBEVVAgOiBIMDkwMDAwMDExNSAqKiogZXNzYWkgZCdlbnZveWVyIHVuIGF2aXMgc2FucyBkb3NzaWVyIGRlIHJhdHRhY2hlLjUuLi4uLi4uLi42Li4uLi4uLi4uNy4uLi4uLi4uLjguLi4uLi4uLi45Li4uLi4uLi4uMCAvIGVzc2FpIGQnZW52b3llciB1biBhdmlzIHNhbnMgZG9zc2llciBkZSByYX...

instead of the original that is : 1 ééé test DUP : H0900000115 *** essai
d'envoyer un avis sans dossier de
rattache.5.........6.........7.........8.........9.........0 / essai
d'envoyer un avis sans dossier de rattache



Mel Weaver said:
sorry that should be
MailAddress to = new MailAddress(receiver)
MailAddress from = new MailAddress(sender)

using (System.Net.Mail.MailMessage __mailMessage = new
System.Net.Mail.MailMessage(from, to))
{
.............
}




Mel Weaver said:
Try adding

MailAddress to = new MailAddress(receiver)
MailAddress from = new MailAddress(sender)

using (System.Net.Mail.MailMessage __mailMessage = new
System.Net.Mail.MailMessage( sender, receiver))
{
.............
}



Simon Chang said:
I use System.Net.Mail.MailMessage to send e-mail.
When the subject containt accent and the string is longer then 179
characters.
The receiver of the e-mail get
"=?utf-8?B?MSDDqcOpw6kgdGVzdCBEVVAgOiBI..."
in the subject instead of the original subject.

Here is the code :

// Case 1 with problem in the subject: The receiver of the e-mail will
get the next string in the subject
"=?utf-8?B?MSDDqcOpw6kgdGVzdCBEVVAgOiBIMDkwMDAwMDExNSAqKiogZXNzYWkgZCdlbnZveWVyIHVuIGF2aXMgc2FucyBkb3NzaWVyIGRlIHJhdHRhY2hlLjUuLi4uLi4uLi42Li4uLi4uLi4uNy4uLi4uLi4uLjguLi4uLi4uLi45Li4uLi4uLi4uMCAvIGVzc2FpIGQnZW52b3llciB1biBhdmlzIHNhbnMgZG9zc2llciBkZSByYX..."
SendMail("(e-mail address removed)", "(e-mail address removed)", "1 ééé test
DUP : XXXXXXXXXXX *** essai d'envoyer un XXXX sans dossier de
rattache.5.........6.........7.........8.........9.........0 / essai
d'envoyer un XXXX sans dossier de rattache", "testé");


// Case 2 no problem in the subject: The receiver of the e-mail will
get
the right string in the subject : 2 é test DUP
SendMail("(e-mail address removed)", "(e-mail address removed)", "2 é test
DUP", "testé");


private void SendMail(string sender, string receiver, string subject,
string body)
{
System.Net.Mail.SmtpClient __smtpClient = null;
__smtpClient = new System.Net.Mail.SmtpClient("....");

using (System.Net.Mail.MailMessage __mailMessage = new
System.Net.Mail.MailMessage(
sender, receiver))
{
__mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
__mailMessage.Subject = subject;
__mailMessage.IsBodyHtml = true;
__mailMessage.Body = body;
__smtpClient.Send(__mailMessage);
}
}

I try different approach by replacing the "é" by [&ecute;] or [/u00EF] or
[É] or [&#Xc9] but get the same result.
 
The problem is not sending the e-mail.

The problem is how the receiver get the e-mail.

In the receiver mailbox, if he open the e-mail, the subject will be :
=?utf-8?B?MSDDqcOpw6kgdGVzdCBEVVAgOiBIMDkwMDAwMDExNSAqKiogZXNzYWkgZCdlbnZve WVyIHVuIGF2aXMgc2FucyBkb3NzaWVyIGRlIHJhdHRhY2hlLjUuLi4uLi4uLi42Li4uLi4uLi4u Ny4uLi4uLi4uLjguLi4uLi4uLi45Li4uLi4uLi4uMCAvIGVzc2FpIGQnZW52b3llciB1biBhdml zIHNhbnMgZG9zc2llciBkZSByYX...

instead of the original that is : 1 ééé test DUP : H0900000115 *** essai
d'envoyer un avis sans dossier de
rattache.5.........6.........7.........8.........9.........0 / essai
d'envoyer un avis sans dossier de rattache

This is correct. Only ASCII symbols are allowed in email content
according to the RFC; everything else has to be escaped, using either
quoted-printable (which is what you see here), or base64. See here for
more:

http://en.wikipedia.org/wiki/Quoted-printable
http://tools.ietf.org/html/rfc2045#section-6.7

If the receiving client does not handle this encoding correctly, then
it is broken.
 
Back
Top