4

I`m trying to send e-mail via c# and use the following code:

public static bool SendSMTPMail(string smtphost, int smtpport, string smtplogin, string smtppassword, string from, string to, string subject, string body, bool isHtml)
{
  try
  {               
    using (MailMessage message = new MailMessage(from, to, subject, body))
    {
      message.IsBodyHtml = isHtml;
      message.SubjectEncoding = System.Text.Encoding.UTF8;
      message.BodyEncoding = System.Text.Encoding.UTF8;

      SmtpClient mailClient = new SmtpClient(smtphost, smtpport);
      mailClient.EnableSsl = false;
      mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
      mailClient.Credentials = new NetworkCredential(smtplogin, smtppassword);

      mailClient.Send(message);
      return true;
    }
  }
  catch
  {
    return false;
  }
}

It works fine, when mails recieved in Windows, but when user trying to read them in MacOS - subject header is in wrong encoding. If I set subject encoding to Windows-1251, it works good, but only for cyrillic subjects, and I`m going to send asian too...

How can I send emails using pure Unicode?

And the second question - if I`ll add any attachment to the mail, it will be added with extra files - "filelist.xml" and "header.htm".

How to get rid of them?

Thaks!

JleruOHeP
  • 10,106
  • 3
  • 45
  • 71
  • In Outlook subject looks like this "Subject: =?utf-8?B?QnVzaW5lc3MgU3R1ZGlvINC00LvRjyDQv9GA0L7RhNC10YHR?= =?utf-8?B?gdC40L7QvdCw0LvQvtCyLCDQstC10LHQuNC90LDRgCAwNi4wNC4xMiDQ?= =?utf-8?B?sy4=?=". It seems, like the error is with splitting... – JleruOHeP Mar 29 '12 at 08:00
  • Can the MacOS user find out what encoding the Mac thinks it is? What email program are they using? If they're using webmail, suggest they set their browser's text encoding to "auto". – Mr Lister Mar 29 '12 at 08:01
  • MAcOS uses auto encoding, and it doesn`t helps. They also tryed to find right encoding by checking every possibility - but it also doesnt helps – JleruOHeP Mar 29 '12 at 08:03
  • Extra info: if subject is long enought, and it splitted into 3 =?utf-8?B?QWE...?= then first 2 sections displayed as "QWE..." and the last one is decoded correctly – JleruOHeP Mar 29 '12 at 08:23

2 Answers2

1

The better solution is to create a structure or an automation that impse to system different encodings following the receiver format:

  • generic utf8
  • iso xxx for cyrillic
  • iso xxy for chinese

and so on.

Gian
  • 126
  • 7
  • Can you explain, what do you offer? – JleruOHeP Mar 29 '12 at 08:21
  • Sure, I'm proposing you to have multiple encoding settings instead one: I guess you know when system are sending an email in cyrillic or in chinese, or in US/EUR, so you can use a switch case, or an adapter structure to impose a different encoding due to the receiver, I hope it is clear :) – Gian Mar 29 '12 at 08:26
0

About my second question found this, and it`s helps

For encoding issues I choose to send mails with subjects in english only...

Community
  • 1
  • 1
JleruOHeP
  • 10,106
  • 3
  • 45
  • 71