我正在尝试使用 C# VS2012 .NET 4.5 System.Net.Mail.MailMessage 向 gmail 地址发送电子邮件。
电子邮件已发送,但始终有:
Content-Transfer-Encoding: quoted-printable
当我在邮件客户端(gmail Web 邮件客户端)中查看电子邮件的来源时。
我已经尝试了 BodyEncoding、BodyTransferEnconding 和 DeliveryFormat 等我能想到的所有不同组合……但没有什么能摆脱这个引用打印的。
问题是 CSS 在电子邮件中不起作用。HTML 呈现正常,但未应用 CSS,当我查看电子邮件源时,我看到它在所有 = 符号后插入了这个 3D,这阻止了我的 CSS 工作。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir=3D"ltr" xml:lang=3D"en" xmlns=3D"http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=3D"Content-Type" content=3D"Type=3Dtext/html; charset=3Dut=f-8" />
<style type=3D"text/css">
...
我用来发送电子邮件的代码如下:
using (MailMessage _MailMessage = new MailMessage("[SENDER_EMAIL_REMOVED]", "[RECIPIENT_EMAIL_REMOVED]"))
{
SmtpClient _SmtpClient = new SmtpClient("smpt.gmail.com", 587);
_SmtpClient.EnableSsl = true;
_SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
_SmtpClient.UseDefaultCredentials = false;
_SmtpClient.Credentials = new System.Net.NetworkCredential("[USERNAME_REMOVED]","[PASSWORD_REMOVED]");
_MailMessage.Subject = ""Test Email";
_MailMessage.BodyEncoding = System.Text.Encoding.UTF8;
_MailMessage.BodyTransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
_SmtpClient.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
_MailMessage.Body = _MessageBody;
}
我发送的 HTML 是从序列化到 web.config 的字符串中加载的,如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="Type=text/html; charset=utf-8" />
<title>Page Title</title>
<style type="text/css">
.style1 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; }
.style2 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight:bold; }
.tblRowHdrCol {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight:bold; background-color:#f0f0f0; width:180px; }
.tblRowValCol {font-family: Arial, Helvetica, sans-serif; font-size: 12px; padding-left:10px; width:100%; }
</style>
</head>
<body>
<p class="style1"><img src="http://www.domain.comn/image.jpg" alt="Some Image" /></p>
<p class="style1">
Some text here...
</p>
<table width="100%">
<tr>
<td class="tblRowHdrCol">Field One:</td>
<td class="tblRowValCol">[FIELD_ONE]</td>
</tr>
<tr>
<td class="tblRowHdrCol">Field Two:</td>
<td class="tblRowValCol">[FIELD_TWO]</td>
</tr>
<tr>
<td class="tblRowHdrCol">Field Three:</td>
<td class="tblRowValCol">[FIELD_THREE]</td>
</tr>
<tr>
<td class="tblRowHdrCol">Field Four:</td>
<td class="tblRowValCol">[FIELD_FOUR]</td>
</tr>
<tr>
<td class="tblRowHdrCol" colspan="2">Exception Details:</td>
</tr>
<tr>
<td class="tblRowValCol" colspan="2" style="padding-left:1px">[EXCEPTION_DETAILS]</td>
</tr>
</table>
</body>
</html>
因此,我将其加载并在 [] 占位符上进行查找和替换。
我该如何解决这个问题?
我什至尝试添加一个标题以将其设置为 7BIT:
_MailMessage.Headers.Add("Content-Transfer-Encoding", "7BIT");
它添加了这个,但当我在 gmail 中查看电子邮件的来源时,它上面仍然有引用的可打印内容。
问候,
斯科特