1

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: fromemail@fromemail.com
Date: Tue, 06 Sep 2022 21:39:47 -0400
Subject: Invoice #0000003
Message-Id: <2FWZXJIFTHU4.JPGM32IG50SI2@person1234>
To: Newperson@new.com
Cc: cc@cc.cc
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@email.com<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@email.com<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.

GisMofx
  • 982
  • 9
  • 27
  • It sounds like an issue with encoding of the Mime message if you create the smallest pdf (with dummy content) possible and post the MIME content you then trying to send that might help with working out what is going wrong or at least reproducing a bug etc. – Glen Scales Sep 05 '22 at 23:31
  • @GlenScales I've added the code I'm using and a sample MIME output. When there are no attachments there is no difference; I still get the same result. Thanks for taking the time to look at this. – GisMofx Sep 07 '22 at 01:51
  • I tried sending your sample email (just with email address changes) and I can receive it okay with no winmail.dat. I noticed in your code your adding html to the text body which you don't want to be doing. Is there anything in you Exchange Online org that adds automatic signatures ? it could be that is what's causing the issue if you look at the Message Trace logs you should be able to see everything that happens to the message within the Transport Pipeline. Have you tried just sending a message with a normalized body to test ? – Glen Scales Sep 07 '22 at 03:13
  • @GlenScales I tried with plain text in `.TextBody` property, same result. I am trying to get exchange admin access in order to see the Message Trace logs and to see if there are any Mail Flow Rules...Also sending a plain email from outlook web client UI includes webmail.dat attachment... I think something in exchange is the culprit as you suggest.... more to come. – GisMofx Sep 08 '22 at 00:00
  • @GlenScales Problem solved! Your solution for sending MIME messages now works and my code above works fine! Answer below about disabling TNEF via disabling RTF text format. – GisMofx Sep 10 '22 at 01:21
  • possible and post the MIME content you then trying to send that might help with working out what is going wrong or at least reproducing a bug etc. – Hediye_seza Sep 10 '22 at 06:42
  • @Hediye_seza I answered the fix to my problem(See below). Also, I have posted the MIME content above. That said, could there be another answer? – GisMofx Sep 10 '22 at 21:48

1 Answers1

1

In my case, I needed to disable TNEF formatting by disabling RTF. This is done through the online Exchange Admin Center:

  1. Mail Flow
  2. Remote domains
  3. Choose 'Default'(or whatever you have set)
  4. Edit text and character set
  5. List item
  6. Use rich-text format: Never.
  7. Save.

Disable RTF

Now both Graph SDK Messages and MIME messages send emails via graph as expected!

GisMofx
  • 982
  • 9
  • 27