2016-08-25 67 views
-1

我从我的应用程序发送电子邮件,但是当我在收件箱中收到电子邮件时,格式会中断。我如何使用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> 
+0

方的问题:为什么你修剪你的'StringBuilder's长度为0,使用前? – Filburt

回答

2

您发送的信息是明文。

您需要启用的HTML标记:

_mail.IsBodyHtml = true; 
+0

谢谢你的工作正常。现在我的问题是如何添加问候(亲爱的用户),最后,它延迟了一点接收电子邮件或可能是我的前景。 –

+0

@mavhungukhodani这应该是一个不同的问题 –