【问题标题】:How to send multi-part MIME messages in c#?如何在 C# 中发送多部分 MIME 消息?
【发布时间】:2011-07-25 22:09:43
【问题描述】:

我想为电子邮件客户端无法处理 HTML 的用户发送包含 HTML 组件和纯文本组件的多部分 MIME 消息。 System.Net.Mail.MailMessage 类似乎不支持这一点。你是怎么做到的?

【问题讨论】:

    标签: c# email mime


    【解决方案1】:

    哦,这真的很简单......但我会把答案留给像我一样在谷歌搜索之前来寻找答案的人...... :)

    归功于this article

    使用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);
    

    【讨论】:

    • 如果您想要一个稍微强类型的系统,您可以使用 MediaTypeNames.Text.Plain 或 MediaTypeNames.Text.Html 代替“text/plain”和“text/html”
    猜你喜欢
    • 2014-09-07
    • 2023-03-22
    • 2012-03-25
    • 2012-02-04
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    相关资源
    最近更新 更多