Edd*_*eyo 17 .net email html-email
我测试通过C#发送了一些邮件,但我不能告诉特效设置IsBodyHtml到true了.无论价值如何,我在Body中发送的内容都显示内容类型为"text/plain",而我的HTML显示标签以及所有在我的电子邮件客户端(gmail)中.那个标志究竟应该做什么?
注意:我可以通过创建AlternateView内容类型为"text/html" 来发送HTML电子邮件,我只想了解设置正文的工作方式.
Zac*_*ary 18
这是我每天使用的SMTP助手的摘录....
public bool SendMail(string strTo, string strFrom, string strCc, string strBcc, string strBody, string strSubject)
{
bool isComplete = true;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
//Default port will be 25
smtpClient.Port = 25;
message.From = new MailAddress(smtpEmailSource);
message.To.Add(strTo);
message.Subject = strSubject;
if (strCc != "") { message.Bcc.Add(new MailAddress(strCc)); }
if (strBcc != "") { message.Bcc.Add(new MailAddress(strBcc)); }
message.IsBodyHtml = true;
string html = strBody; //I usually use .HTML files with tags (e.g. {firstName}) I replace with content. This allows me to edit the emails in VS by opening a .HTML file and it's easy to do string replacements.
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, new ContentType("text/html"));
message.AlternateViews.Add(htmlView);
// Send SMTP mail
smtpClient.Send(message);
}
catch
{
isComplete = false;
}
return isComplete;
}
Run Code Online (Sandbox Code Playgroud)
[UPDATE]
我最初离开的关键点......
IsBodyHtml声明您的邮件是HTML格式的.如果您只发送一个HTML视图,这就是您所需要的.
AlternateView用于存储我的HTML,这不是发送HTML消息所必需的,但如果您想发送包含HTML和纯文本的消息,则需要它,以防接收者无法呈现HTML.
我拿出了上面的plainView,所以这不明显,对不起......
这里的关键是,如果要发送HTML格式的消息,则需要使用IsBodyHtml = true(默认为false)将内容呈现为HTML.
Jer*_*eid 15
我只是在解决同样的问题.我最好的解决方案是避免设置对象的Body属性MailMessage.相反,只需添加两个AlternateViews,首先是纯文本,然后是HTML.确保首先添加纯文本版本,因为MIME标准说:
这些格式是按照他们对原作的忠诚度来排序的,其中最忠实的第一个和最忠实的最后一个.
这意味着,您首先放置纯文本版本,因此客户端应尽可能使用HTML版本.