C# 发送电子邮件时如何显示正确的电子邮件格式
我正在从我的应用程序发送电子邮件,但当我在收件箱中收到电子邮件时,格式会中断。如何显示或构建带有标题、正文和签名的电子邮件模板 下面是我的html电子邮件设计代码C# 发送电子邮件时如何显示正确的电子邮件格式,c#,html-email,C#,Html Email,我正在从我的应用程序发送电子邮件,但当我在收件箱中收到电子邮件时,格式会中断。如何显示或构建带有标题、正文和签名的电子邮件模板 下面是我的html电子邮件设计代码 var _mail = new MailMessage(); { SmtpClient smtp = new SmtpClient(); smtp.Port = 25; smtp.Enabl
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();
}
var\u mail=new MailMessage();
{
SmtpClient smtp=新SmtpClient();
smtp.Port=25;
smtp.EnableSsl=false;
smtp.Host=Host;
smtp.Timeout=8900000;
smtp.DeliveryMethod=SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials=false;
smtp.Credentials=新系统.Net.NetworkCredential(用户名、密码);
_mail.From=新邮件地址(FromEmailAddress);
_mail.To.Add(ToEmailAddress);
_mail.Subject=“新预订的索赔”;
_mail.Body=EmailBody(EmailClaimBookedClass.EmailClaimBookedText(“,”);
smtp.Send(_-mail);
}
公共静态字符串EmailBody(字符串EmailBody)
{
int i=0;
StringBuilder EB=新的StringBuilder();
EB.长度=0;
EB.附录行(“”);
EB.AppendLine(GetHtmlStyle());
EB.附录行(“”);
EB.附录行(“”);
EB.AppendLine(“+EmailBody.ToString()+”);
EB.附录行(“”);
EB.附录行(“”);
EB.附录行(“”);
EB.附录行(“”);
EB.附录(“问候”);
EB.附录行(“DD国际”);
EB.附录行(“客户服务部”);
EB.附录行(“电话:+(015)0000000”);
EB.附录行(“”);
EB.附录行(“”);
返回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();
}
私有静态字符串GetHtmlStyle()
{
StringBuilder HtmlStyle=新的StringBuilder();
HtmlStyle.Length=0;
HtmlStyle.AppendLine(“”);
HtmlStyle.AppendLine(“.tksa_table tr td”);
HtmlStyle.AppendLine(“{”);
AppendLine(“字体系列:Arial、Verdana、MS Sans Serif;”);
AppendLine(“字体大小:13px;”);
AppendLine(“颜色:黑色;”);
HtmlStyle.AppendLine(“}”);
HtmlStyle.AppendLine(“.tksa_table tr th”);
HtmlStyle.AppendLine(“{”);
AppendLine(“字体系列:Arial、Verdana、MS Sans Serif;”);
AppendLine(“字体大小:13px;”);
AppendLine(“颜色:黑色;”);
AppendLine(“字体大小:粗体;”);
HtmlStyle.AppendLine(“}”);
AppendLine(“a:link,a:visted”);
HtmlStyle.AppendLine(“{”);
AppendLine(“字体大小:13px;”);
AppendLine(“字体系列:Arial、Verdana、MS Sans Serif;”);
HtmlStyle.AppendLine(“}”);
HtmlStyle.AppendLine(“a:悬停”);
HtmlStyle.AppendLine(“{”);
AppendLine(“字体大小:13px;”);
AppendLine(“字体系列:Arial、Verdana、MS Sans Serif;”);
HtmlStyle.AppendLine(“}”);
HtmlStyle.AppendLine(“a:活动”);
HtmlStyle.AppendLine(“{”);
AppendLine(“字体大小:13px;”);
AppendLine(“字体系列:Arial、Verdana、MS Sans Serif;”);
HtmlStyle.AppendLine(“}”);
HtmlStyle.AppendLine(“”);
返回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>
.tksa_表tr td
{
字体系列:Arial、Verdana、MS无衬线字体;
字体大小:13px;
颜色:黑色;
}
.tksa_表tr th
{
字体系列:Arial、Verdana、MS无衬线字体;
字体大小:13px;
颜色:黑色;
字体大小:粗体;
}
a:链接,a:已访问
{
字体大小:13px;
字体系列:Arial、Verdana、MS Sans Serif;}a:悬停{
字体大小:13px;
字体系列:Arial、Verdana、MS Sans Serif;}a:活动{
字体大小:13px;
字体系列:Arial、Verdana、MS Sans Serif;}
这是我的测试电子邮件,来自我的应用程序DD国际客户服务部电话:+(015)0000000
您发送的消息是纯文本的
您需要启用Html标志:
_mail.IsBodyHtml = true;
附带问题:为什么在使用
StringBuilder
s之前要将其长度修剪为0?谢谢,这很好。现在我的问题是如何添加问候语,如(亲爱的用户),最后,它会延迟一点接收电子邮件,或者可能是我的outlook。@mavhungukhodani这应该是一个不同的问题