Question

This has to do with a mailing tool we use: MailBee, which is pretty easy to use.

  1. We create a Mailing (define the message body and attachments if needed)
  2. We create a list of contacts and add this to a DataTable
  3. We call MailBee's AddJob method which generates an .eml file in ANSI format
  4. After writing the files is completed, we read the files and find the To: string using: Match match = Regex.Match(recipient, @"""(.*?)"" <(.*?)>");

This value seems to be base64 encoded. Here is my code to unit test the parsing.

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        TestMethods.DecodeString("To: \"=?utf-8?B?QWJkdXJyYWhpbSDvv716Z2Vub2dsdQ==?=\" <email@somehost.com;;>");
        // This results in "Abdurrahim �zgenoglu" while it should be "Abdurrahim Özgenoglu"
    }
}

public class TestMethods {
    public static string DecodeString(string stringToDecode)
    {
        Match base64Match = Regex.Match(stringToDecode, @"=\?utf-8\?B\?(.*)\?=");
        if (base64Match.Success)
        {
            string encodedName = base64Match.Groups[1].Value;
            byte[] bytes = Convert.FromBase64String(encodedName);
            return Encoding.UTF8.GetString(bytes);
        }

        return stringToDecode;
    }
}

Any suggestions on what could be going wrong here? I suspect something that MailBee does right before it converts the text into base64. But I can't verify that.

Was it helpful?

Solution 2

The reason this decoding wasn't working was that the string being encoded by Mailbee was wrong from the beginning.

What I did find was that you can specify the RequestEncoding and ResponseEncoding of mailbee, which I set to Encoding.UTF8.

Anyway, It's solved when I did this and made sure the imported CSV containing the names was in UTF8 in the first place.

OTHER TIPS

You're trying to convert an ANSI string to UTF-8. That's why you're seeing this error.

Instead of...

Encoding.UTF8.GetString(bytes);

Try using:

Encoding.GetEncoding(1252).GetString(bytes);

Or

Encoding.GetEncoding("ISO-8859-1").GetString(bytes);

Source

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top