C# MailMessage AlternateView ignoring TransferEncoding

840 views Asked by At

We send calendar invites from our system in multiple languages. (English, Spanish, German etc) Recently we added Georgian and are having issue the the encoding of MailMessage.

For example 'თბილისი' in the email arrives as 'თბილისი'

The email we send includes a HTML AlternateView and a text/calendar AlternateView. I have both AlternateViews TransferEncoding set to QuotedPrintable.

It works correctly for the HTML view but not the calendar view. It always gets sent as 7bit.

In my SmtpClient I have set DeliveryFormat = SmtpDeliveryFormat.International

This is how I add my AlternateView:

var iCalInvite = InterviewService.CreateICALInvite(uniqueId, model.StartTimeUTC, model.EndTimeUTC, model.Location, model.Note, personCompany.Person.Email, personCompany.Person.FullName, interviewAttendee, model.Attendees, msg, _currentUser);

iCalInvite.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
msg.AlternateViews.Add(iCalInvite);

This is the output of the HTML View

Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

This is the incorrect output of the calendar view

Content-Type: text/calendar; method=REQUEST; charset=UTF-8;
Content-Transfer-Encoding: 7bit

I've tried setting to the encoding of the MailMessage in a number of combinations but none seems to output the Georgian text correctly.

MailMessage msg = new MailMessage();
msg.BodyTransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
msg.BodyEncoding = Encoding.UTF8;

Any help of direction would be greatly appreciated. I've spent hours trying various combinations of encoding to no avail.

Thanks!

Here's an example of what appears in Gmail. The HTML view appears location appears correctly but the card doesn't. enter image description here

2

There are 2 answers

0
Eilimint On BEST ANSWER

It turns out it's nothing to do with my code above.

We use a 3rd party provider for sending emails (PostmarkApp). They convert all text/calendar attachments/alternative views to 7-bit before sending. As a result, non-ASCII characters will end up getting mangled.

I changed the SMTP details to Gmail and the email sent with the correct encoding. I have an open ticket with them regarding the issue.

1
György Kőszeg On

Setting the MailMessage.BodyEncoding is simply not enough; you must set the CharSet for all of the alternate views separately.

var calendarView = AlternateView.CreateAlternateViewFromString(body, new ContentType("text/calendar"));
calendarView.ContentType.CharSet = Encoding.UTF8.WebName;
msg.AlternateViews.Add(calendarView);