none
Long MIME encoded-word subject lines RRS feed

  • Question

  • I am trying to read email's subject (contain non-ASCII characters) which is sent using the SmtpClient and MailMessage classes. In some of the email's subject  has  repeated encoding type like

    =?utf-8?B?44OE5paw44GX44GE44OG44Kt44K544OIIOODieOCreODpeOD?=

     =?utf-8?B?oeODs+ODiA==?=

    Apparently this causes an issue when decoding.

    I know there is a workaround, set encoding type explicitly of property(SubjectEncoding) of object (mailMessage) to Unicode. But is there any other solution (e.g: parser, regex expression) to resolve this issue.

    Any help appreciated.

    Thanks,
    Rajesh Patel

    Wednesday, August 22, 2018 6:52 PM

Answers

  • Hi Wendy,

    I set SubjectEncoding to Unicode and its working.

    By the way, default encoding is  UTF8 of property [mailMessage.SubjectEncoding].

    • Marked as answer by DevRajKatie Monday, September 10, 2018 9:11 AM
    Monday, September 10, 2018 9:11 AM

All replies

  • Hi DevRajKatie,

    Thank you for posting here.

    For your question, if you want to send the mail with the subject, please use the UTF8 encoding.

    =?utf-8?B?oeODs+ODiA==?=


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Mail;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp
    {
        class Send_mail
        {
            static void Main(string[] args)
            {
                string smtpAddress = "smtp.live.com";
                int portNumber = 587;
                bool enableSSL = true;
    
                string emailFrom = "wendyzang@hotmail.com";
                string password = "t%R8x@Qv";
                string emailTo = "v-wezan@microsoft.com";
    
                Encoding encoding = Encoding.UTF8;
                string s = "=? utf - 8 ? B ? oeODs + ODiA ==?=";
    
                string subject = encoding.GetString(encoding.GetBytes(s));
                string body = "Hello, I'm just writing this to say Hi!";
    
                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress(emailFrom);
                    mail.To.Add(emailTo);
                    mail.Subject = subject;
                    mail.Body = body;
                    mail.IsBodyHtml = true;
                    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                    {
                        smtp.Credentials = new NetworkCredential(emailFrom, password);
                        smtp.EnableSsl = enableSSL;
                        smtp.Send(mail);
                    }
                }
            }
        }
    }

    But the code does not work for the subject below. I wanna to know what is it. And how does it generate.

    =?utf-8?B?44OE5paw44GX44GE44OG44Kt44K544OIIOODieOCreODpeOD?=
     

    If I use UTF8, it will generate Japanese language. It is same with the solution to set SubjectEncoding to Unicode.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, August 23, 2018 6:39 AM
    Moderator
  • Yes, my email's subject language is Japanese and text is ツ新しいテキスト ドキュメント.

    When I send mail by explicitly setting Encoding type Unicode ( mailMessage.SubjectEncoding = Encoding.Unicode) and then read it by my mailParser class,

     I got encoded Text =?utf-16?B?xDCwZVcwRDDGMK0wuTDIMCAAyTCtMOUw4TDzMMgw?=

    and Decoded Text is: ツ新しいテキスト ドキュメント.

    with no property set : [mailMessage.SubjectEncodin] I got below text

     encoded text : =?utf-8?B?44OE5paw44GX44GE44OG44Kt44K544OIIOODieOCreODpeOD?=

     =?utf-8?B?oeODs+ODiA==?= and Decoded Text is: ツ新しいテキスト ドキュ��ント 

    for the same subject :  ツ新しいテキスト ドキュメント.

    My EmailParser logic works well with other Japanese texts expect this one.

     I also have observed the specific char '' in present in the both language (Japanese  and Chinese)

     So, my only concern is what Should I suppose to do

     1) Do I need to modified my email Parser logic ? (which works in most of case)

     2) Do I need to set Encoding property Uni-code explicitly?

     3) Does this specific char is the culprit or my subject text is Invalid?

     4) Is there any kind of problem with UTF8 Encoding Type?

    Thanks

    Rajesh Patel

    • Edited by DevRajKatie Friday, August 24, 2018 5:20 AM
    Friday, August 24, 2018 5:15 AM
  • Hi DevRajKatie,

    Based on my test, you could set SubjectEncoding to Unicode or set the subject string with encoding UTF8.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 30, 2018 8:16 AM
    Moderator
  • Hi Wendy,

    I set SubjectEncoding to Unicode and its working.

    By the way, default encoding is  UTF8 of property [mailMessage.SubjectEncoding].

    • Marked as answer by DevRajKatie Monday, September 10, 2018 9:11 AM
    Monday, September 10, 2018 9:11 AM