[英]How to prevent HTML Email from having quoted printable when sending with C# using System.Net.Mail.MaiMessage
I am attempting to send an email to a gmail address using C# VS2012 .NET 4.5 System.Net.Mail.MailMessage. 我正在尝试使用C#VS2012 .NET 4.5 System.Net.Mail.MailMessage将电子邮件发送到gmail地址。
The email get's sent but always has: 电子邮件已发送,但始终包含:
Content-Transfer-Encoding: quoted-printable
when I view the source of the email in the mail client (gmail web mail client). 当我在邮件客户端(gmail网络邮件客户端)中查看电子邮件的来源时。
I have tried every different combination I can think of with BodyEncoding, and BodyTransferEnconding and DeliveryFormat etc... but nothing can get rid of this quoted-printable. 我尝试了我能想到的与BodyEncoding,BodyTransferEnconding和DeliveryFormat等的每一种不同组合,但是没有什么可以摆脱此quoted-printable的。
The problem is that the CSS is not working in the email. 问题是CSS在电子邮件中不起作用。 The HTML renders ok, but not CSS is applied, and when I view the email source, I see that it has this 3D inserted after all the = signs and this is preventing my CSS from working.
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">
...
The code I am using to send the email is as follows: 我用来发送电子邮件的代码如下:
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;
}
The HTML I am sending is loaded from a string that was serialized into the web.config and is as follows: 我发送的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>
So I load this in and do a find and replace on the [] place holders. 因此,我将其装入并查找并替换[]占位符。
How can I resolve this issue? 我该如何解决这个问题?
I even tried adding a header to set it to 7BIT: 我什至尝试添加标头将其设置为7BIT:
_MailMessage.Headers.Add("Content-Transfer-Encoding", "7BIT");
It added this but still had the quoted-printable above it when I viewed the source of the email in gmail. 它添加了此内容,但是当我查看gmail中的电子邮件来源时,它上面仍带有带引号的可打印内容。
Regards, 问候,
Scott 史考特
From what I understand, you can't get the css to work. 据我了解,您无法使CSS正常工作。 I think you have to convert the html so that the style are taken out of the style tag and placed inline to the html tags.
我认为您必须转换html,以便将样式从style标签中取出并内联到html标签中。 There are many tool that do this, if you want to do this dynamically in C# take a look at this: Inlining CSS in C#
有许多工具可以执行此操作,如果您想在C#中动态执行此操作,请看一下: 在C#中内联CSS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.