我正在尝试使用SMTP
发送带格式的文本。我将其作为HTML
发送,但是当发送电子邮件时,它会以字符串普通文本的形式发送。
foreach (var emailAddress in toEmailAddress)
{
message.To.Add(emailAddress);
}
//remove special chars
//emailMessage = emailMessage.Replace("\r\n", "<br/>");
//emailMessage = emailMessage.Replace("\t", "");
message.Subject = emailSubject;
message.Body = emailMessage;
message.From = sender;
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
//testing out with gmail
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("email", "Password"),
EnableSsl = true
})
{
await smtpClient.SendMailAsync(message);
}
这是消息正文我无法格式化。
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<br/>
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<br/>
<head>
<br/>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>
<br/>
</title>
<br/>
<style type=\"text/css\">
<br/>
.cs95E872D0{text-align:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt} <br/>
.cs46E558AC{color:#F00000;background-color:transparent;font-family:Calibri;font-size:11pt;font-weight:normal;font-style:normal;text-decoration: underline;}
<br/>
</style><
br/>
</head>
<br/>
<body>
<br/>
<p class=\"cs95E872D0\">
<span class=\"cs46E558AC\">This is red text that is underlined</span>
</p>
</body>
<br/>
</html>
<br/>"
答案 0 :(得分:1)
它以html格式发送。清理你的br标签并发送
这是gmail收到的消息
Return-Path: <xxxxxxxx@gmail.com>
Received: from D2205 ([198.182.213.4])
by smtp.gmail.com with ESMTPSA id e21sm6307299iof.23.2016.10.13.13.05.01
for <xxxxxxxx@gmail.com>
(version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128);
Thu, 13 Oct 2016 13:05:01 -0700 (PDT)
Message-ID: <57ffe8ed.15b56b0a.4b5ca.81a7@mx.google.com>
Date: Thu, 13 Oct 2016 13:05:01 -0700 (PDT)
X-Google-Original-Date: 13 Oct 2016 16:05:01 -0400
MIME-Version: 1.0
From: xxxxxxxx@gmail.com
To: xxxxxxxx@gmail.com
Subject: Test email
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h=
ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htm=
l xmlns=3D"http://www.w3.org/1999/xhtml"> <head> <meta http-equ=
iv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8" /> <ti=
tle> </title> <style type=3D"text/css"> .cs95E872D0{text-align=
:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt} .cs46E558AC{color:=
#F00000;background-color:transparent;font-family:Calibri;font-siz=
e:11pt;font-weight:normal;font-style:normal;text-decoration: unde=
rline;} </style></head> <body> <p class=3D"cs95E872D0"> <spanc=
lass=3D"cs46E558AC">This is red text that is underlined</span> </=
p> </body></html>
答案 1 :(得分:0)
需要从system.io中读取body。
messageBody = System.IO.File.ReadAllText("D:\\Demo\\test.html");
对于smtp prop,请确保isHtml为true并发送.html文件的字符串
IsHtml = true,
Body = messageBody,
你的html必须像
<!DOCTYPE html>
<style type="text/css">
.cs95E872D0 {
text-align: left;
text-indent: 0pt;
margin: 0pt 0pt 0pt 0pt;
}
.cs46E558AC {
color: #F00000;
background-color: transparent;
font-family: Calibri;
font-size: 11pt;
font-weight: normal;
font-style: normal;
text-decoration: underline;
}
</style>
<br />
<p class="cs95E872D">
<span class="cs46E558AC">This is red text that is underlined</span>
</p>
答案 2 :(得分:0)
问题在于style
标记。 Gmail不支持。