html邮件发送gmail,向Gmail邮件发送包含html的smtp邮件

像这样的东西应该工作:

注意MailMessage指System.Net.MailMessage。也有System.Web.MailMessage,我从来没有用过,据我所知,它已经过时了。

MailMessage message = new MailMessage();

// Very basic html. HTML should always be valid, otherwise you go to spam

message.Body = "

test

";

// QuotedPrintable encoding is the default, but will often lead to trouble,

// so you should set something meaningful here. Could also be ASCII or some ISO

message.BodyEncoding = Encoding.UTF8;

message.IsBodyHtml = true;

// No Subject usually goes to spam, too

message.Subject = "Some Subject";

// Note that you can add multiple recipients, bcc, cc rec., etc. Using the

// address-only syntax, i.e. w/o a readable name saves you from some issues

message.To.Add("[email protected]");

// SmtpHost, -Port, -User, -Password must be a valid account you can use to

// send messages. Note that it is very often required that the account you

// use also has the specified sender address associated!

// If you configure the Smtp yourself, you can change that of course

SmtpClient client = new SmtpClient(SmtpHost, SmtpPort) {

Credentials = new NetworkCredential(SmtpUser, SmtpPassword),

EnableSsl = enableSsl;

};

try {

// It might be necessary to enforce a specific sender address, see above

if (!string.IsNullOrEmpty(ForceSenderAddress)) {

message.From = new MailAddress(ForceSenderAddress);

}

client.Send(message);

}

catch (Exception ex) {

return false;

}

对于更复杂的模板解决方案,使身体的HTML而不是硬打号呢,有,例如,在MvcContrib的EMailTemplateService它可以作为一个准则使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值