C# 在发送包含 **Plain-text** 和 **Html** 内容的电子邮件时遇到问题

标签 c# smtpclient

我在发送包含纯文本Html 内容的电子邮件时遇到问题。 在 Outlook 2007 中,永远不会显示纯文本 消息。 在 Gmail 电子邮件客户端中,默认显示 plain-text。我需要 HTML 消息在 Outlook 和 Gmail 中显示为默认消息,但如果在该系统上进行此类设置,用户可以更改为纯文本。

我正在使用以下 C# 代码发送电子邮件:

    private void SendEmail(string server, string from, string userName, string password, int port , string recipients)
{

    MailMessage message = new MailMessage(from,recipients);
    message.Subject = "This email message has multiple views.";
    message.From = new MailAddress(from);
    message.To.Add(recipients);
    message.IsBodyHtml = false;
    message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>This is some HTML text2</h1></body></html>", null, MediaTypeNames.Text.Html));
    message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("This is some plain text2", null, MediaTypeNames.Text.Plain));

    SmtpClient client = new SmtpClient(server);
    client.Port = port;
    NetworkCredential SMTPUserInfo = new NetworkCredential(userName, password);
    client.UseDefaultCredentials = false;
    client.Credentials = SMTPUserInfo;
    client.Send(message);
}

此代码的问题在于(我不确定),在 Outlook 2007 中,电子邮件内容最初显示如下图(默认将 HTML 正文呈现为纯文本):

enter image description here

当我右键单击并选择“显示为 HTML”时,电子邮件内容显示如下图(呈现 HTML 正文):

enter image description here

当我使用 gmail.com 查看电子邮件时,电子邮件内容显示如下图(默认呈现纯文本):

enter image description here

编辑 1: 根据建议,我对代码进行了更改,现在我可以在 Gmail 中看到 HTML 电子邮件内容。但是在 Outlook 2007 中,将纯文本设置为默认 View 后,我看到 HTML 标记为字符串,它不显示我起草的与 html 略有不同的纯文本。另外,我如何在 Gmail 中测试纯文本电子邮件,就像现在在 Gmail 中一样,我只收到 HTML View ,但我没有找到任何方法来查看纯 TextView ,尽管我已经单击了查看原件并将 view=om 更改为 view=dom我在谷歌某处读到的网址。

private void SendEmail(string server, string from, string userName, string password, int port , string recipients)
{

    MailMessage message = new MailMessage(from,recipients);
    message.Subject = "This email message has multiple views.";
    message.From = new MailAddress(from);
    message.To.Add(recipients);
    message.Body = "This is some plain text2";
    message.IsBodyHtml = true;
    message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>This is some HTML text2</h1></body></html>", null, MediaTypeNames.Text.Html));
    //message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("This is some plain text2", null, MediaTypeNames.Text.Plain));

    SmtpClient client = new SmtpClient(server);
    client.Port = port;
    NetworkCredential SMTPUserInfo = new NetworkCredential(userName, password);
    client.UseDefaultCredentials = false;
    client.Credentials = SMTPUserInfo;
    client.Send(message);
}

最佳答案

我会尝试在纯 TextView 之后添加 html View 。

此外,我通常将 IsBodyHtml 标志设置为 true,即使我已经定义了两个 View 也是如此。

关于C# 在发送包含 **Plain-text** 和 **Html** 内容的电子邮件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52314700/

相关文章:

c# - 为 Windows Phone 8 枢轴应用程序设计 ViewModel 很热门

asp.net - 如何使用 SMTP 通过 Exchange Server 发送邮件

c# - System.Net.Mail 和 MailMessage 不立即发送消息

c# - 发送应用程序电子邮件的方法 - SMTP 与 Exchange Web 服务

c# - .NET framework 中位标志的实际使用

c# - 如何更新 Android 按钮辅助功能特性

c# - 如何绑定(bind)与 DataTable 不同的文本和不同的值?

c# - 由于线程退出或 WCF 中的应用程序请求,I/O 操作已中止

c# - 我可以从 SmtpClient.SendAsync 的 userToken 对象中获得什么好处?