如何在发送电子邮件时显示正确的电子邮件格式

时间:2016-08-25 08:35:26

标签: c# html-email

我正在从我的应用程序发送电子邮件,但是当我在收件箱中收到电子邮件时,格式会中断。如何使用Tittle,Body和Signature显示或构建电子邮件模板。

我的html电子邮件设计代码,

            var _mail = new MailMessage();
            {

                SmtpClient smtp = new SmtpClient();
                smtp.Port = 25;
                smtp.EnableSsl = false;
                smtp.Host = host;
                smtp.Timeout = 8900000;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(Username, Password);
                _mail.From = new MailAddress(FromEmailAddress);
                _mail.To.Add(ToEmailAddress);
                _mail.Subject = "New Claim Booked";
                _mail.Body = EmailBody(EmailClaimBookedClass.EmailClaimBookedText("", ""));
                smtp.Send(_mail);
            }

   public static string EmailBody(string EmailBody)
    {
        int i = 0;

        StringBuilder EB = new StringBuilder();
        EB.Length = 0;
        EB.AppendLine("<html><head>");
        EB.AppendLine(GetHtmlStyle());
        EB.AppendLine("</head><body>");
        EB.AppendLine("<table class='tksa_table' width='100%' border='0'>");
        EB.AppendLine("<tr><td colspan='3'><font color='#000000'>" + EmailBody.ToString() + "</font></td></tr>");

        EB.AppendLine("<tr><td colspan='3'>&nbsp;</td></tr>");
        EB.AppendLine("</table>");
        EB.AppendLine("<table class='tksa_table' width='100%' border='0'>");
        EB.AppendLine("<tr><td colspan='3'>&nbsp;</td></tr>");
        EB.AppendLine("<tr><td colspan='3' valign='top'>Kind Regards</td></tr>");
        EB.AppendLine("<tr><td colspan='3' valign='top'>DD International</td></tr>");
        EB.AppendLine("<tr><td colspan='3' valign='top'>Customer Services Department</td></tr>");
        EB.AppendLine("<tr><td colspan='3' valign='top'> phone:+ (015) 00000 00</td></tr>");
        EB.AppendLine("</table>");
        EB.AppendLine("</body></html>");

        return EB.ToString();
    }

Html风格

   private static string GetHtmlStyle()
    {
        StringBuilder HtmlStyle = new StringBuilder();
        HtmlStyle.Length = 0;
        HtmlStyle.AppendLine("<style type='text/css'>");
        HtmlStyle.AppendLine(".tksa_table tr td");
        HtmlStyle.AppendLine("{");
        HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;");
        HtmlStyle.AppendLine("font-size: 13px;");
        HtmlStyle.AppendLine("color: black;");
        HtmlStyle.AppendLine("}");
        HtmlStyle.AppendLine(".tksa_table tr th");
        HtmlStyle.AppendLine("{");
        HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;");
        HtmlStyle.AppendLine("font-size: 13px;");
        HtmlStyle.AppendLine("color: black;");
        HtmlStyle.AppendLine("font-weight: bold;");
        HtmlStyle.AppendLine("}");
        HtmlStyle.AppendLine("a:link, a:visited");
        HtmlStyle.AppendLine("{");
        HtmlStyle.AppendLine("font-size: 13px;");
        HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;");
        HtmlStyle.AppendLine("}");
        HtmlStyle.AppendLine("a:hover");
        HtmlStyle.AppendLine("{");
        HtmlStyle.AppendLine("font-size: 13px;");
        HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;");
        HtmlStyle.AppendLine("}");
        HtmlStyle.AppendLine("a:active");
        HtmlStyle.AppendLine("{");
        HtmlStyle.AppendLine("font-size: 13px;");
        HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;");
        HtmlStyle.AppendLine("}");
        HtmlStyle.AppendLine("</style>");
        return HtmlStyle.ToString();
    }

我的收件箱上的结果

  <html><head>
  <style type='text/css'>
  .tksa_table tr td
  {
     font-family: Arial, Verdana, MS Sans Serif;
     font-size: 13px;
     color: black;
 }
 .tksa_table tr th
 {
  font-family: Arial, Verdana, MS Sans Serif;
  font-size: 13px;
  color: black;
  font-weight: bold;
  }
 a:link, a:visited
 {
 font-size: 13px;
 font-family: Arial, Verdana, MS Sans Serif; } a:hover {
 font-size: 13px;
 font-family: Arial, Verdana, MS Sans Serif; } a:active {
 font-size: 13px;
 font-family: Arial, Verdana, MS Sans Serif; } </style>

 </head><body>
 <table class='tksa_table' width='100%' border='0'> <tr>                                           <td colspan='3'>    <font color='#000000'>This is my testing email from my App</font></td></tr> <tr>    <td colspan='3'>&nbsp;</td></tr> </table> <table class='tksa_table' width='100%'   border='0'> <tr><td colspan='3'>&nbsp;</td></tr> <tr><td colspan='3' valign='top'>Kind Regards</td></tr> <tr><td colspan='3' valign='top'>DD International</td></tr> <tr><td colspan='3' valign='top'>Customer Services Department</td></tr> <tr><td colspan='3' valign='top'> phone:+ (015) 00000 00</td></tr> </table> </body>
 </html>

1 个答案:

答案 0 :(得分:2)

您发送的信息是纯文本。

您需要启用Html标志:

_mail.IsBodyHtml = true;