任何人都可以解释为什么这段代码不起作用:
async Task Main()
{
using (var smtpClient = new SmtpClient(@"127.0.0.1", 25))
{
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
var from = new MailAddress(@"sender@mydomain.com");
var to = new MailAddress(@"receiver@mydomain.com");
using (var message = new MailMessage())
{
message.Subject = "Email Subject";
message.Body = "<html><head></head><body><h1>Hello World!</h1></body></html>";
message.IsBodyHtml = true;
message.From = from;
message.To.Add(to);
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Hello World!", null, MediaTypeNames.Text.Plain));
await smtpClient.SendMailAsync(message);
}
}
}
输出信息是:
MIME-Version:1.0 From:sender@mydomain.com To:receiver@mydomain.com 日期:2016年5月12日14:51:30 +0200主题:电子邮件主题内容类型: 多部分/替代; 边界= - boundary_2_be88a42a-4d48-4096-b4e0-71fb7857809f
---- boundary_2_be88a42a-4d48-4096-b4e0-71fb7857809f内容类型:text / plain; charset = us-ascii Content-Transfer-Encoding: 引号的可打印
Hello World!
---- boundary_2_be88a42a-4d48-4096-b4e0-71fb7857809f内容类型:text / plain; charset = us-ascii Content-Transfer-Encoding: 引号的可打印
Hello World! ---- boundary_2_be88a42a-4d48-4096-b4e0-71fb7857809f -
此代码正常运行:
async Task Main()
{
using (var smtpClient = new SmtpClient(@"127.0.0.1", 25))
{
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
var from = new MailAddress(@"sender@mydomain.com");
var to = new MailAddress(@"receiver@mydomain.com");
using (var message = new MailMessage())
{
message.Subject = "Email Subject";
message.Body = "Hello World!";
message.IsBodyHtml = false;
message.From = from;
message.To.Add(to);
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>Hello World!</h1></body></html>", null, MediaTypeNames.Text.Html));
await smtpClient.SendMailAsync(message);
}
}
}
输出很好
MIME-Version:1.0 From:sender@mydomain.com To:receiver@mydomain.com 日期:2016年5月12日14:46:52 +0200主题:电子邮件主题内容类型: 多部分/替代; 边界= - boundary_1_2d888597-e405-40cb-8bd4-1bfcba04fb44
---- boundary_1_2d888597-e405-40cb-8bd4-1bfcba04fb44 Content-Type:text / plain; charset = us-ascii Content-Transfer-Encoding: 引号的可打印
Hello World! ---- boundary_1_2d888597-e405-40cb-8bd4-1bfcba04fb44内容类型:text / html; charset = us-ascii Content-Transfer-Encoding: 引号的可打印
Hello World!
---- boundary_1_2d888597-E405
-40cb-8bd4-1bfcba04fb44 -
这段代码正在运行:
async Task Main()
{
using (var smtpClient = new SmtpClient(@"127.0.0.1", 25))
{
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
var from = new MailAddress(@"sender@mydomain.com");
var to = new MailAddress(@"receiver@mydomain.com");
using (var message = new MailMessage())
{
message.Subject = "Email Subject";
message.From = from;
message.To.Add(to);
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>Hello World!</h1></body></html>", null, MediaTypeNames.Text.Html));
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Hello World!", null, MediaTypeNames.Text.Plain));
await smtpClient.SendMailAsync(message);
}
}
}
输出也很好:
MIME-Version:1.0 From:sender@mydomain.com To:receiver@mydomain.com 日期:2016年5月12日15:07:24 +0200主题:电子邮件主题内容类型: 多部分/替代; 边界= - boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2
---- boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2 Content-Type:text / html; charset = us-ascii Content-Transfer-Encoding: 引号的可打印
Hello World!
---- boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2内容类型:text / plain; charset = us-ascii Content-Transfer-Encoding: 引号的可打印
Hello World! ---- boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2 -
编辑:任何人都可以帮助在输出中解释html标签吗?
答案 0 :(得分:1)
昨天我遇到了同样的问题; 这是我理解的内容,这要归功于评论中指出的MSDN阅读。
当您发送消息时,Body属性专用于此处所述的电子邮件的文本内容link
使用Body属性指定文本版本并使用 AlternateViews集合用于指定具有其他MIME类型的视图
无论出于何种原因,如果在设置Body内容后添加任何AlternateView,Body将重置为Plain / Text。
对我来说,你的最后一个例子似乎是让事情变得更有活力的方式;
message.Subject = "Email Subject";
message.From = from;
message.To.Add(to);
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>Hello World!</h1></body></html>", null, MediaTypeNames.Text.Html));
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Hello World!", null, MediaTypeNames.Text.Plain));
await smtpClient.SendMailAsync(message);