Reputation: 180
I am using mimekit and mailkit to build a multi part email that contains an html body and a plain text body. The html version works fine. The plain text works fine but only when the html is not included.
When I build a multi part email that contains both text and html bodies it seems like only the html is received on both outlook and gmail. If I set my mail to only plain-text then I get text parsed out of the html body. I have looked at the original in gmail and decoded it to find it is the parsed html not the separate plain text version I am trying to send with mailkit.
Here is the code that is building and sending the email.
public void SendEmail(List<string> toAddresses, string fromAddress, string subject, string htmlBody, string textBody)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(fromAddress));
foreach (var to in toAddresses)
{
message.To.Add(new MailboxAddress(to));
}
message.Subject = subject;
var bodyBuilder = new BodyBuilder
{
HtmlBody = htmlBody,
TextBody = textBody
};
message.Body = bodyBuilder.ToMessageBody();
_smtpClient.Connect(_settings.SmtpHostServer, _settings.SmtpPortNumber);
_smtpClient.Authenticate(_settings.SmtpUserName, _settings.SmtpUserPassword);
_smtpClient.Send(message);
_smtpClient.Disconnect(true);
}
When I write the message to a file it is in mime format, and has separate sections for html and plain text with the content I expect for each section.
From: ************
Date: Mon, 23 Sep 2019 09:10:35 -0600
Subject: Welcome To ************
Message-Id: <[email protected]>
To: ************
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="=-YWTR3eTYkSy366wQ3CLhlw=="
--=-YWTR3eTYkSy366wQ3CLhlw==
Content-Type: text/plain; charset=utf-8
=================================================================================
Welcome to ************
=================================================================================
You are receiving this email because you have registered with *****.
Please copy the following URL below into your web browser to login to *******.
https://************/login
Sincerely,
The ******* Team
=================================================================================
******** Contact Information
=================================================================================
Email: ************
Phone: ***********
--=-YWTR3eTYkSy366wQ3CLhlw==
Content-Type: text/html; charset=utf-8
Content-Id: <[email protected]>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<style type="text/css">
/* FONTS */
@media screen {
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 400;
src: local('Lato Regular'), local('Lato-Regular'), url(https://fonts.gstatic.com/s/lato/v11/qIIYRU-oROkIk8vfvxw6QvesZW2xOQ-xsNqO47m55DA.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 700;
src: local('Lato Bold'), local('Lato-Bold'), url(https://fonts.gstatic.com/s/lato/v11/qdgUG4U09HnJwhYI-uK18wLUuEpTyoUstqEm5AMlJo4.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: italic;
font-weight: 400;
src: local('Lato Italic'), local('Lato-Italic'), url(https://fonts.gstatic.com/s/lato/v11/RYyZNoeFgb0l7W3Vu1aSWOvvDin1pK8aKteLpeZ5c0A.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: italic;
font-weight: 700;
src: local('Lato Bold Italic'), local('Lato-BoldItalic'), url(https://fonts.gstatic.com/s/lato/v11/HkF_qI1x_noxlxhrhMQYELO3LdcAZYWl9Si6vvxL-qU.woff) format('woff');
}
}
/* CLIENT-SPECIFIC STYLES */
body, table, td, a {
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
table, td {
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
}
img {
-ms-interpolation-mode: bicubic;
}
/* RESET STYLES */
img {
border: 0;
height: auto;
line-height: 100%;
outline: none;
text-decoration: none;
}
table {
border-collapse: collapse !important;
}
body {
height: 100% !important;
margin: 0 !important;
padding: 0 !important;
width: 100% !important;
}
/* iOS BLUE LINKS */
a[x-apple-data-detectors] {
color: inherit !important;
text-decoration: none !important;
font-size: inherit !important;
font-family: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
}
/* MOBILE STYLES */
@media screen and (max-width:600px) {
h1 {
font-size: 32px !important;
line-height: 32px !important;
}
}
/* ANDROID CENTER FIX */
div[style*="margin: 16px 0;"] {
margin: 0 !important;
}
</style>
</head>
<body style="background-color: #f4f4f4; margin: 0 !important; padding: 0 !important;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<!-- LOGO -->
<tr>
<td bgcolor="#539be2" align="center">
<!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr>
<td align="center" valign="top" width="600">
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
<tr>
<td align="center" valign="top" style="padding: 50px 20px 50px 20px;">
</td>
</tr>
</table>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
<!-- HERO -->
<tr>
<td bgcolor="#539be2" align="center" style="padding: 0px 10px 0px 10px;">
<!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr>
<td align="center" valign="top" width="600">
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
<tr>
<td bgcolor="#ffffff" align="center" valign="top" style="padding: 40px 20px 20px 20px; border-radius: 4px 4px 0px 0px; color: #111111; font-family: 'Lato', Helvetica, Arial, sans-serif; font-size: 48px; font-weight: 400; letter-spacing: 4px; line-height: 48px;">
<h1 style="font-size: 48px; font-weight: 400; margin: 0;">Welcome to ********</h1>
</td>
</tr>
</table>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
<!-- COPY BLOCK -->
<tr>
<td bgcolor="#f4f4f4" align="center" style="padding: 0px 10px 0px 10px;">
<!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr>
<td align="center" valign="top" width="600">
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
<!-- COPY -->
<tr>
<td bgcolor="#ffffff" align="center" style="padding: 20px 30px 10px 30px; color: #666666; font-family: 'Lato', Helvetica, Arial, sans-serif; font-size: 18px; font-weight: 400; line-height: 25px;">
<p>
You are receiving this email because you have registered with *********.
</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#ffffff" align="center" style="padding: 20px 30px 30px 30px;">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" bgcolor="#539be2">
<a href="https://**********/login" target="_blank" style=" border-radius: 3px; font-size: 20px; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; color: #ffffff; text-decoration: none; padding: 15px 25px; border-radius: 2px; border: 1px solid #539be2; display: inline-block;">
Login To *********
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p>Sincerely,</p>
<p>The ******** Team</p>
</td>
</tr>
</table>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
<!-- FOOTER -->
<tr>
<td bgcolor="#f4f4f4" align="center" style="padding: 0px 10px 0px 10px;">
<!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr>
<td align="center" valign="top" width="600">
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
<!-- NAVIGATION -->
<tr>
<td bgcolor="#ffffff" align="center" style="padding: 30px 30px 30px 30px; color: #666666; font-family: 'Lato', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: 400; line-height: 18px;">
<p style="margin: 0;">
<a href="mailto:*********" style="color: #039be5; font-weight: 700;">**********</a>
</p>
<p style="margin: 0;">
<a href="tel:8865715014" style="color: #039be5; font-weight: 700;">(866) 571-5014</a>
</p>
</td>
</tr>
</table>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</table>
</body>
</html>
--=-YWTR3eTYkSy366wQ3CLhlw==--
Upvotes: 1
Views: 1972
Reputation: 38528
Sounds like bugs in GMail and Outlook. The raw MIME looks fine. There's absolutely nothing wrong with it.
Upvotes: 0