mail message 支持html,.net - What does MailMessage.IsBodyHtml do? - Stack Overflow

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值