SMTP,不能发送格式化文本

本文关键字:格式化 文本 不能 SMTP | 更新日期: 2023-09-27 18:13:09

我正在尝试用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/>"

SMTP,不能发送格式化文本

以html格式发送。清理你的br标签和send

这是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>

问题是style标签。Gmail不支持

需要从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>