Menu

Charset encoding

Help
Anonymous
2003-04-28
2003-04-29
  • Anonymous

    Anonymous - 2003-04-28

    Thanks for the great work done on this project. It si very useful to me.

    I had a problem with charset encoding however. I need to send Cyrillic text and it did not arrive in any readable format. I made some changes to the source code and now everything works fine. I tried to make it work for all types of encoding and hope it should work. I made tests with text body, html body and different attachments - all successful.

    In this forum there was a request for help with german umlaut letter and I guess this problem would be solved as well.

    Generally, what I did was to change all encodings to be compliant with the charset specified  for MailMessage.Charset.

    There is one important question that I have:
    Why were all non-ASCII characters converted using MailEncoder.ConvertToQP(). I believe this was one of the problems. Is this some standard?

    ===================================

    Here is the detailed description of all changes that I made.

    If someone wants the modified source files or a compiled version of them, write me: panayot@dir.bg

    Since for each different charset I needed to change encodings in several places, I thought it would be better if I have a globally visible field, that is why I decided to use a static field in SmtpConfig class.
    Here are all the changes that I made:

    - added a default value for Charset in SmtpConfig class.
    - commented charset field in the definition of MailMessage class and in Reset().
    - the Charset property of MailMessage class sets and gets the value of SmtpConfig.Charset
    - replaced all occurences of charset in MailMessage with SmtpConfig.Charset
    - made the following 6 replacement in MailEncoder and Smtp clasess (The first two - UTF7 and UTF8 for file attachments in MailMessage class I did not touch because perhaps you have some reason for using these different encodings. I don't understand this part very well). The 6 replacements that I made were substitutions of Encoding.ASCII with Encoding.GetEncoding(SmtpConfig.Charset):

    MailMessage.cs(510):                sb.Append(Encoding.UTF7.GetString(bin, 0, len));
    MailMessage.cs(638):                        attSection.Append(Encoding.UTF8.GetString(bin , 0, len)+"\r\n");

    MailEncoder.cs(96):            return Encoding.GetEncoding(SmtpConfig.Charset).GetString(ret, 0, ret.Length);

    Smtp.cs(398):                    WriteToStream(ref nwstream, Convert.ToBase64String(Encoding.GetEncoding(SmtpConfig.Charset).GetBytes(this.username.ToCharArray())) + "\r\n");

    Smtp.cs(402):                    WriteToStream(ref nwstream, Convert.ToBase64String(Encoding.GetEncoding(SmtpConfig.Charset).GetBytes(this.password.ToCharArray())) + "\r\n");

    Smtp.cs(447):                byte[] arrToSend = Encoding.GetEncoding(SmtpConfig.Charset).GetBytes(line);

    Smtp.cs(464):                string returnMsg = Encoding.GetEncoding(SmtpConfig.Charset).GetString(readBuffer, 0, length);

    Total found: 8    Matching files: 3   

    - Made the following change to MailEncoder class so that ConvertToQP(s) is skipped:

    internal static string ConvertHeaderToQP(string s, string charset)
            {
               
                return "=?" + charset + "?Q?" + s + "?="; //ConvertToQP(s)
            }
           
    - changed to 8bit transfer encoding and skipped message body conversion in MailMessage.GetTextMessageBody()

    sb.Append("Content-Transfer-Encoding: 8bit\r\n\r\n");
    sb.Append(messageBody);//(MailEncoder.ConvertToQP(messageBody));

    I think these are all changes. I made tests with attachments (a text file attachment that contained cyrillic text, and a word document), and with html body and all was OK.

    I would suggest using unicode (utf-8) as the default encoding because it is the most general one and will accomodate most users if they do not implicitly set the charset type.

     
    • Ian Stallings

      Ian Stallings - 2003-04-29

      Thanks for the info. I sent you an email requesting  the modified files and hope to integrate your request into the next release,

      thanks again,
      Ian Stallings

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.