如何在c#中发送多部分MIME消息? [英] How to send multi-part MIME messages in c#?

查看:101
本文介绍了如何在c#中发送多部分MIME消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送带有HTML组件的多部分MIME消息以及电子邮件客户端无法处理HTML的人的纯文本组件。 System.Net.Mail.MailMessage 类似乎不支持此功能。你如何做?

I want to send multi-part MIME messages with an HTML component plus a plain text component for people whose email clients can't handle HTML. The System.Net.Mail.MailMessage class doesn't appear to support this. How do you do it?

推荐答案

D'哦,这真的很简单...但我会在这里离开答案对于任何像我这样的人来说,在Google搜索之前找到答案...:)

D'oh, this is really simple... but I'll leave the answer here for anyone who, like me, came looking on SO for the answer before Googling... :)

信用到本文

使用 AlternateViews ,像这样:

//create the mail message
var mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";

//first we create the Plain Text part
var plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
//then we create the Html part
var htmlView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", null, "text/html");
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);

//send the message
var smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);

这篇关于如何在c#中发送多部分MIME消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆