使用 mailkit 发送的多部分电子邮件的文本/纯文本版本未正确接收
Posted
技术标签:
【中文标题】使用 mailkit 发送的多部分电子邮件的文本/纯文本版本未正确接收【英文标题】:text/plain version of multipart email sent with mailkit not being received correctly 【发布时间】:2020-01-21 20:08:08 【问题描述】:我正在使用 mimekit 和 mailkit 构建包含 html 正文和纯文本正文的多部分电子邮件。 html版本工作正常。纯文本工作正常,但仅当不包含 html 时。
当我构建包含文本和 html 正文的多部分电子邮件时,似乎 Outlook 和 gmail 都只收到了 html。如果我将邮件设置为纯文本,那么我会从 html 正文中解析出文本。我查看了 gmail 中的原始文件并对其进行了解码,发现它是解析后的 html,而不是我尝试使用 mailkit 发送的单独的纯文本版本。
这是构建和发送电子邮件的代码。
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);
当我将消息写入文件时,它是 mime 格式,并且有单独的 html 和纯文本部分,其中包含我期望每个部分的内容。
From: ************
Date: Mon, 23 Sep 2019 09:10:35 -0600
Subject: Welcome To ************
Message-Id: <FVVQHEHRM8U4.J4JDCO9VD6OS2@localhost.localdomain>
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: <6MLWHEHRM8U4.ZO5T7EVTEFZ42@localhost.localdomain>
<!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" >
<!-- LOGO -->
<tr>
<td bgcolor="#539be2" align="center">
<!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" >
<tr>
<td align="center" valign="top" >
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" 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" >
<tr>
<td align="center" valign="top" >
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" 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" >
<tr>
<td align="center" valign="top" >
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" 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 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" >
<tr>
<td align="center" valign="top" >
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" 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==--
【问题讨论】:
你会说你遇到了这个错误吗? github.com/jstedfast/MimeKit/issues/506 @jstedfast no html 正文包含正确的 html 并且不为空 如果将消息写入文件并在文本编辑器中打开,它是什么样的? @jstedfast 当我将消息写入文件时,它有单独的纯文本和 html 部分,内容是我所期望的 您能否将电子邮件的内容粘贴到您的问题中,以便我和其他人自己查看?试图在无法看到您所看到的内容的情况下调试您的问题是一项不可能完成的任务。 【参考方案1】:听起来像是 GMail 和 Outlook 中的错误。原始 MIME 看起来不错。绝对没有问题。
【讨论】:
以上是关于使用 mailkit 发送的多部分电子邮件的文本/纯文本版本未正确接收的主要内容,如果未能解决你的问题,请参考以下文章