.net MailMessage.IsBodyHtml 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2555813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:07:30  来源:igfitidea点击:

What does MailMessage.IsBodyHtml do?

.netemailhtml-email

提问by Eddie Deyo

I'm testing sending out some emails via C#, but I can't tell what effect setting IsBodyHtmlto truehas. 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?

我测试通过C#发送了一些邮件,但我不能告诉效果设置IsBodyHtmltrue了。无论值如何,我在正文中发送的任何内容都会以“文本/纯文本”的内容类型显示,而我的 HTML 会在我的电子邮件客户端 (gmail) 中显示标签和所有内容。那面旗帜实际上应该做什么?

NOTE: I can send an HTML email just fine by creating an AlternateViewwith a content type of "text/html", I just want to understand how setting the body is supposed to work.

注意:我可以通过创建AlternateView内容类型为“text/html”的 HTML 电子邮件来发送电子邮件,我只想了解设置正文应该如何工作。

回答by Zachary

Here is an excerpt for my SMTP helper I use everyday....

这是我每天使用的 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;
}

[UPDATE]

[更新]

The key points as I originally left off...

我最初离开的关键点......

  1. IsBodyHtml states that your message is HTML formatted. If you were only sending a single view of HTML, this is all you need.

  2. 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.

  1. IsBodyHtml 声明您的消息是 HTML 格式的。如果您只发送一个 HTML 视图,这就是您所需要的。

  2. AlternateView 用于存储我的 HTML,这不是发送 HTML 消息所必需的,但如果您想发送包含 HTML 和纯文本的消息,则它是必需的,以防接收方无法呈现 HTML。

I took out my plainView above so this isn't obvious, sorry...

我取出了上面的plainView,所以这不明显,对不起...

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.

这里的关键是,如果您想发送 HTML 格式的消息,您需要使用 IsBodyHtml = true(默认为 false)将您的内容呈现为 HTML。

回答by JerSchneid

I just wrestled with this same problem. My best solution was to avoid setting the Bodyproperty of the MailMessageobject at all. Instead just add two AlternateViews, first a plain text then an HTML. Make sure to add the plain text version first because the MIME standard says that:

我只是在与同样的问题搏斗。我最好的解决方案是完全避免设置对象的Body属性MailMessage。相反,只需添加两个AlternateViews,首先是纯文本,然后是 HTML。确保首先添加纯文本版本,因为 MIME 标准说:

The formats are ordered by how faithful they are to the original, with the least faithful first and the most faithful last.

格式按照它们对原作的忠实程度排序,最不忠实的在前,最忠实的在后。

That means, that you put the plain text version first, so the clients should use the HTML version if possible.

这意味着,您将纯文本版本放在首位,因此客户端应尽可能使用 HTML 版本。

回答by RAMESH Ravichandran

IsBodyHtml – Specify whether body contains text or HTML mark up.

IsBodyHtml – 指定正文是包含文本还是 HTML 标记。

Body contain text or html markup that should be identify by IsBodyHtml.

正文包含应由 IsBodyHtml 识别的文本或 html 标记。