I’m having trouble with email display in Gmail. I created a system that sends emails with both HTML and plain text versions to avoid spam filters. The problem is Gmail keeps showing the plain text version instead of the HTML one.
Here’s my current setup:
EmailMessage email = new EmailMessage(
new EmailAddress(sender, notification.SenderName),
new EmailAddress(notification.RecipientEmail, notification.RecipientName));
email.Subject = notification.Title;
email.Body = notification.HtmlContent;
email.IsBodyHtml = true;
email.Priority = MessagePriority.Normal;
ContentType textContentType = new ContentType("text/plain");
AlternateView textView = AlternateView.CreateAlternateViewFromString(notification.PlainContent, textContentType);
email.AlternateViews.Add(textView);
When I check the raw email, I see two content sections. Both show Content-Type: text/plain even though I set IsBodyHtml = true. The first section is bigger and should be the HTML version, but Gmail still picks the plain text.
How can I make Gmail prioritize the HTML version? What am I missing in my email structure?
You’re mixing the traditional email body property with alternate views - that’s what’s causing the problem. Setting email.Body with IsBodyHtml = true and then adding an alternate view creates conflicting MIME parts. The mail system can’t figure out which content type to use for the main body. I hit this same issue when migrating our legacy email system. Skip email.Body completely when you’re working with multipart messages. Instead, create both HTML and plain text as separate AlternateView objects using AlternateView.CreateAlternateViewFromString(). Set the content types explicitly to text/html and text/plain. Gmail’s rendering engine needs this clean multipart structure to make proper content type decisions. Once I switched to this approach, Gmail consistently showed the HTML version and fell back to plain text for older clients.
check your content-type headers in the raw email again - sounds like both parts are getting marked as text/plain, which explains why gmail’s confused. when you use IsBodyHtml=true with alternate views, the framework sometimes screws up the mime types. try removing the main body assignment completely and just use two alternateviews with explicit content types.
Had the same issue last year building an email notification system. Your problem is you’re setting IsBodyHtml = true but then adding only a plain text alternate view - Gmail sees two plain text parts and picks one randomly. Skip the email.Body = notification.HtmlContent line entirely. Instead, create two separate AlternateView objects - one HTML (text/html) and one plain text. Gmail needs the proper MIME structure to default to HTML for supported clients. Order matters too - add plain text first, then HTML. Fixed my display issues across Gmail and other clients.