Search code examples
c#emailmime

Weird characters in email


I have written a mail-processing program, which basically slaps a template on incoming mail and forwards it on. Incoming mail goes to a Gmail account, which I download using POP, then I read the mail (both html and plain text multipart-MIME), make whatever changes I need to the template, then create a new mail with the appropriate plain+html text and send it on to another address.

Trouble is, when the mail gets to the other side, some of the mails have been mangled, with weird characters like à and  magically getting inserted. They weren't in the original mails, they're not in my template, and I can't find any sort of predictable pattern as to when these characters appear. I'm sure it's got something to do with the encoding properties of the mails, but I am making sure to set both the charset and the transfer encoding of the outgoing mail to be the same as the incoming mail. So what else do I need to do?

EDIT: Here's a snipped sample of an incoming mail:

Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

=0A=0ASafari Special:=0A=0A=A0=0A=0ASafari in Thornybush Priv=
ate Game Reserve 9-12=0AJanuary 2012 (3nights)

After processing, this comes out as:

Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

=0D=0A=0D=0ASafari Special:=0D=0A=0D=0A=C2=A0=0D=0A=0D=0A=
Safari in Thornybush Private Game Reserve 9-12=0D=0AJanuary=
2012 (3nights)

Notice the insertion of the =0D and =C2 characters (aside from a few =0A's that weren't in the original).

So what does you think is happening here?

ANOTHER CLUE: Here's my code that creates the alternate view:

var htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
htmlView.ContentType.CharSet = charSet;
htmlView.TransferEncoding = transferEncoding;
m.AlternateViews.Add(htmlView);

Along the lines of what @mjwills suggested, perhaps the CreateAlternativeViewFromString() method already assumes UTF-8, and changing it later to iso-8859-1 doesn't make a difference?


Solution

  • So every =0A is becoming =0D=0A. And every =A0 is becoming =C2=A0.

    The former looks like it might be related to Carriage Return / Line Feeds. The latter looks like it might be related to What is "=C2=A0" in MIME encoded, quoted-printable text?.

    My guess is that even though you have specified the charset, something alone the line is treating it as UTF8.

    You may want to try using this form of CreateAlternateViewFromString, where the ContentType.CharSet is set appropriately.