MailMessage.IsBodyHtml做什么? [英] What does MailMessage.IsBodyHtml do?
问题描述
IsBodyHtml
到 true
有。不管价值如何,无论我在Body中发送的内容类型为text / plain,我的HTML都显示了我的电子邮件客户端(gmail)中的所有标签。这个标志实际上应该做什么? 注意:我可以通过创建一个 AlternateView
内容类型为text / html,我只是想了解如何设置身体应该工作。
这是我每天使用的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
{
//默认端口将为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; //我通常用标签的HTML文件(例如{firstName})替换为内容。这允许我通过打开.HTML文件来编辑VS中的电子邮件,并且很容易进行字符串替换。
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html,new ContentType(text / html));
message.AlternateViews.Add(htmlView);
//发送SMTP邮件
smtpClient.Send(message);
}
catch
{
isComplete = false;
}
return isComplete;
}
[更新]
我最初离开的关键点...
-
IsBodyHtml声明您的消息是HTML格式的。如果你只发送一个HTML视图,这就是你需要的。
-
AlternateView用于存储我的HTML,这不是发送HTML消息,但如果要发送包含HTML和纯文本的消息,如果接收方无法呈现HTML,则需要。
我拿出了我的plainView,所以这并不明显,对不起...
这里的关键是,如果你想发送HTML格式的消息,您需要使用IsBodyHtml = true(默认为false)将您的内容呈现为HTML。
I'm testing sending out some emails via C#, but I can't tell what effect setting IsBodyHtml
to true
has. Regardless of the value, whatever I send in my Body shows up with a content type of "text/plain", and my HTML shows up tags and all in my email client (gmail). What is that flag actually supposed to do?
NOTE: I can send an HTML email just fine by creating an AlternateView
with a content type of "text/html", I just want to understand how setting the body is supposed to work.
Here is an excerpt for my SMTP helper I use everyday....
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;
}
[UPDATE]
The key points as I originally left off...
IsBodyHtml states that your message is HTML formatted. If you were only sending a single view of HTML, this is all you need.
AlternateView is used to store my HTML, this is not required for sending a HTML message but it's required if you want to send a message that includes HTML and Plain Text, in case the receiver is unable to render the HTML.
I took out my plainView above so this isn't obvious, sorry...
The key here is that if you want to send a HTML formatted message you need to use IsBodyHtml = true (default is false) to have your content rendered as HTML.
这篇关于MailMessage.IsBodyHtml做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!