Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将richtext格式的电子邮件发送到Outlook?_C#_Email_Outlook_Richtext - Fatal编程技术网

C# 如何将richtext格式的电子邮件发送到Outlook?

C# 如何将richtext格式的电子邮件发送到Outlook?,c#,email,outlook,richtext,C#,Email,Outlook,Richtext,通过指定text/HTML内容类型字符串(如下所示),以HTML格式发送电子邮件(到Outlook)非常有效: using (MailMessage message = new MailMessage()) { message.From = new MailAddress("--@---.com"); message.ReplyTo = new MailAddress("--@---.com"); message.To.Add(new MailAddress("---@---.com

通过指定text/HTML内容类型字符串(如下所示),以HTML格式发送电子邮件(到Outlook)非常有效:

using (MailMessage message = new MailMessage())
{
  message.From = new MailAddress("--@---.com");
  message.ReplyTo = new MailAddress("--@---.com");
  message.To.Add(new MailAddress("---@---.com"));
  message.Subject = "This subject";
  message.Body = "This content is in plain text";
  message.IsBodyHtml = false;

  string bodyHtml = "<p>This is the HTML <strong>content</strong>.</p>";

  using (AlternateView altView = AlternateView.CreateAlternateViewFromString(bodyHtml,
    new ContentType(MediaTypeNames.Text.Html)))
  {
    message.AlternateViews.Add(altView);
    SmtpClient smtp = new SmtpClient(smtpAddress);
    smtp.Send(message);
  }
}
Outlook没有检测到这一点,它会返回纯文本。

如何以富文本格式发送电子邮件?

底线是,使用System.Net.Mail无法轻松做到这一点

Outlook中的富文本在SMTP世界(Exchange之外)中作为winmail.dat文件发送

winmail.dat文件是一条TNEF消息。因此,您需要在winmail.dat文件(格式化为TNEF规则)中创建richtext

然而,这还不是全部。Outlook使用压缩RTF的特殊版本,因此,在将RTF添加到winmail.dat文件之前,您还需要压缩RTF

底线是,这很难做到,除非客户真的,真的需要这个功能,否则我会重新考虑它


这不是用.NET中的几行代码就能做到的。

您也可以通过在日历视图之前添加另一个备选视图来实现,如下所示:

var body = AlternateView.CreateAlternateViewFromString(bodyHtml, new System.Net.Mime.ContentType("text/html"));
mailMessage.AlternateViews.Add(body);
这对我很有用

public void sendUsersMail(string recipientMailId, string ccMailList, string body, string subject)
    {
        try
        {  
            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress("norepl@xyz.com", "Tracker Tool");
            Msg.To.Add(recipientMailId);
            if (ccMailList != "")
                Msg.CC.Add(ccMailList);
            Msg.Subject = subject;
            var AltBody = AlternateView.CreateAlternateViewFromString(body, new System.Net.Mime.ContentType("text/html"));
            Msg.AlternateViews.Add(AltBody);
            Msg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient("mail.xyz.com");
            smtp.Send(Msg);
            smtp.Dispose();
        }
        catch (Exception ex)
        { 
        }
    }

请显示邮件的来源,以便我们确定这是Outlook中的问题还是.net framework中的问题:@Heinzi,很抱歉,这是Exchange中的问题,因此您链接到的说明无效。是否有其他方式查看邮件来源?您是否有其他非Exchange邮件地址可以将邮件发送到?大多数网络邮件提供商提供了一些查看邮件“来源”的方法。或者,你可以发送给我(我的邮箱地址在我的主页顶部,链接在我的SO个人资料上),我会将来源添加到你的问题中。我不认为你能做到这一点,因为我认为你需要创建一个winmail.dat来了解一些方法。是的,我已经对此进行了进一步的调查,并得出了相同的结论。Exchange SDK中有一些帮助程序方法,但这些方法依赖于托管Exchange程序集,这些程序集不可自由分发。谢谢,现在我可以使用此方法发送富文本邮件。
public void sendUsersMail(string recipientMailId, string ccMailList, string body, string subject)
    {
        try
        {  
            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress("norepl@xyz.com", "Tracker Tool");
            Msg.To.Add(recipientMailId);
            if (ccMailList != "")
                Msg.CC.Add(ccMailList);
            Msg.Subject = subject;
            var AltBody = AlternateView.CreateAlternateViewFromString(body, new System.Net.Mime.ContentType("text/html"));
            Msg.AlternateViews.Add(AltBody);
            Msg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient("mail.xyz.com");
            smtp.Send(Msg);
            smtp.Dispose();
        }
        catch (Exception ex)
        { 
        }
    }