Reputation: 304
I'm updating an application for sending emails to use the GMAIL API instead of SMTP.
I am facing a problem with encoding of the email. The subject is shown as it should, but the body is not shown properly. [below is a screenshot of the recieved email]
the code i am using to send the email:
public bool SendMailGmailAPI(System.Net.Mail.MailMessage mailMessage, GmailService service)
{
bool isEverythingOK = false;
try
{
var mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mailMessage);
string mm = mimeMessage.ToString();
var gmailMessage = new Google.Apis.Gmail.v1.Data.Message
{
Raw = Encode(mm)
};
UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(gmailMessage, mailMessage.From.Address);
request.Execute();
isEverythingOK = true;
}
catch (Exception e)
{
ErrorMessage = e.Message;
isEverythingOK = false;
}
return isEverythingOK;
}
public static string Encode(string text)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
return System.Convert.ToBase64String(bytes)
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "");
}
mailMessage is generated from this:
public MailMessage CreateEmailMessage(string mailto="")
{
EmailMessageToSend = new MailMessage();
//setting from, to, cc, bcc, replyto
//make body
EmailMessageToSend.BodyEncoding = System.Text.Encoding.UTF8;
//EmailMessageToSend.HeadersEncoding = System.Text.Encoding.UTF8;
EmailMessageToSend.SubjectEncoding = System.Text.Encoding.UTF8;
EmailMessageToSend.IsBodyHtml = true;
EmailMessageToSend.Body = Body + "<hr>";
EmailMessageToSend.Body += DateTime.Now.ToString("dd.MM.yyyy HH:mm");
EmailMessageToSend.Body += "<br /><br />ΔΗΛΩΣΗ ΕΜΠΙΣΤΕΥΤΙΚΟΤΗΤΑΣ / ΑΠΟΠΟΙΗΣΗ ΕΥΘΥΝΗΣ <br /> Αυτό το μήνυμα περιέχει εμπιστευτικές και απόρρητες πληροφορίες και η χρήση τους επιτρέπεται μόνον από τον αναφερόμενο παραλήπτη. Σε περίπτωση που περιέλθει σε σας από λάθος, παρακαλούμε να το διαγράψετε άμεσα από το σύστημά σας ή να το καταστρέψετε και να ειδοποιήσετε τον αποστολέα. <br /><br />" +" CONFIDENTIALITY WARNING / DISCLAIMER <br /> The information in this email is confidential and is intended solely for the addressee(s). If you have received this transmission in error, and you are not an intended recipient, be aware that any disclosure, copying, distribution or use of this transmission or its contents is prohibited.";
var htmlView = AlternateView.CreateAlternateViewFromString(EmailMessageToSend.Body, null, "text/html");
EmailMessageToSend.Subject = Subject+" - Mail v2.0";
return EmailMessageToSend;
}
on mimeMessage.ToString() i get this result
Any suggestions on how to fix the encoding problem?
Upvotes: 2
Views: 1606
Reputation: 41
Not sure if it's still relevant, however. Ran into the same problem recently and spent quite frustrating hours in attempts to find solution. And it appeared to be pretty simple - don't use MimeMessage.ToString() method as it's marked as 'unreliable' by the authors. Use WriteTo(Stream stream) instead.
Something like this:
MimeMessage mimeMessage = MimeMessage.CreateFromMailMessage(msg);
using (MemoryStream ms = new MemoryStream())
{
mimeMessage.WriteTo(ms);
return Convert.ToBase64String(ms.GetBuffer())
.TrimEnd('=')
.Replace('+', '-')
.Replace('/', '_');
}
Upvotes: 4