Reputation: 1024
I'm using graph sdk from c# to send emails from a web app. The body is formatted as HTML. I'm attaching PDFs to the email. The recipients are receiving the email with winmail.dat
and sometimes noname
. It seems like the email is coming in as plain text. Gmail seems to able to unpack winmail.dat, but not all clients seem to do this.
Some research tells me to use MIME format, but having trouble finding a concise example.
How can I send emails from graph api / sdk formatted as HTML with MIME with attachments without creating winmail.dat
Here's a snip from my create message code:
private MimeMessage GetMimeMessage(string fromEmail, string subject, string htmlBody, IEnumerable<string> toRecipients, IEnumerable<string> ccRecipients, IEnumerable<string> attachments)
{
var message = new MimeMessage();
foreach (var recipient in toRecipients)
message.To.Add(MailboxAddress.Parse(recipient));
foreach (var recipient in ccRecipients)
message.Cc.Add(MailboxAddress.Parse(recipient));
message.From.Add(MailboxAddress.Parse(fromEmail));
message.Subject = subject;
var builder = new BodyBuilder();
builder.TextBody = htmlBody;
builder.HtmlBody = htmlBody;
foreach (var attachment in attachments)
builder.Attachments.Add(attachment);
message.Body = builder.ToMessageBody();
return message;
}
Send Code:
private async Task SendMimeMessageWithGraph(string sendFromEmail, MimeMessage mimeMessage)
{
var stream = new MemoryStream();
mimeMessage.WriteTo(stream);
stream.Position = 0;
StringContent MessagePost = new StringContent(Convert.ToBase64String(stream.ToArray()), Encoding.UTF8, "text/plain");
var message = new Message();
var mypost = await MessagePost.ReadAsStringAsync();
var sendMailRequest = _GraphClient.Users[sendFromEmail].SendMail(message, true).Request().GetHttpRequestMessage();
sendMailRequest.Content = MessagePost;
sendMailRequest.Method = HttpMethod.Post;
var sendResult = await _GraphClient.HttpProvider.SendAsync(sendMailRequest);
Debug.WriteLine(sendResult.StatusCode);
}
From: [email protected]
Date: Tue, 06 Sep 2022 21:39:47 -0400
Subject: Invoice #0000003
Message-Id: <2FWZXJIFTHU4.JPGM32IG50SI2@person1234>
To: [email protected]
Cc: [email protected]
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="=-9G8gCZS3S+XQhQ+1kLqUog=="
--=-9G8gCZS3S+XQhQ+1kLqUog==
Content-Type: text/plain; charset=utf-8
Please see attached invoice.<br><br>Thank you for your business,<br>Test<br>Person<br>222-999-1111<br>[email protected]<br><img height="50px" width="auto" src="data:image/svg+xml;base64,PD94b...>">
--=-9G8gCZS3S+XQhQ+1kLqUog==
Content-Type: text/html; charset=utf-8
Please see attached invoice.<br><br>Thank you for your business,<br>Test<br>Person<br>222-999-1111<br>[email protected]<br><img height="50px" width="auto" src="data:image/svg+xml;base64,PD94...">
--=-9G8gCZS3S+XQhQ+1kLqUog==--
I've followed this blog post: https://gsexdev.blogspot.com/2021/08/sending-mimemessage-via-microsoft-graph.html
It partially works; emails received by clients without o365/outlook do not render the html email and include winmail.dat attachment. So, I still have the same issue.
Upvotes: 1
Views: 1776
Reputation: 1024
In my case, I needed to disable TNEF formatting by disabling RTF. This is done through the online Exchange Admin Center:
Now both Graph SDK Messages and MIME messages send emails via graph as expected!
Upvotes: 1