如何在发送邮件时显示正确的邮件格式
本文关键字:格式 显示 | 更新日期: 2023-09-27 18:04:07
我正在从我的应用程序发送电子邮件,但是当我收到我的收件箱上的电子邮件时,格式中断了。我如何显示或建立电子邮件模板与标题,正文和签名。
我的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'> </td></tr>");
EB.AppendLine("</table>");
EB.AppendLine("<table class='tksa_table' width='100%' border='0'>");
EB.AppendLine("<tr><td colspan='3'> </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'> </td></tr> </table> <table class='tksa_table' width='100%' border='0'> <tr><td colspan='3'> </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>
您发送的消息为明文。
你需要启用Html标志:
_mail.IsBodyHtml = true;