代码之家  ›  专栏  ›  技术社区  ›  Dave Mateer

C#同时发送HTML和文本电子邮件-最优雅?

  •  21
  • Dave Mateer  · 技术社区  · 14 年前

    发送HTML和文本电子邮件的最佳实践是什么?

    如果我只发送HTML有什么危险?

    我在想下面这样的事情

    http://johnnycoder.com/blog/2009/04/15/net-mailmessage-linkedresources-alternateviews-and-exceptions/

    try
    {
        // Assign a sender, recipient and subject to new mail message
        MailAddress sender =
            new MailAddress("sender@johnnycoder.com", "Sender");
    
        MailAddress recipient =
            new MailAddress("recipient@johnnycoder.com", "Recipient");
    
        MailMessage m = new MailMessage(sender, recipient);
        m.Subject = "Test Message";
    
        // Define the plain text alternate view and add to message
        string plainTextBody =
            "You must use an email client that supports HTML messages";
    
        AlternateView plainTextView =
            AlternateView.CreateAlternateViewFromString(
                plainTextBody, null, MediaTypeNames.Text.Plain);
    
        m.AlternateViews.Add(plainTextView);
    
        // Define the html alternate view with embedded image and
        // add to message. To reference images attached as linked
        // resources from your HTML message body, use "cid:contentID"
        // in the <img> tag...
        string htmlBody =
            "<html><body><h1>Picture</h1><br>" +
            "<img src=\"cid:SampleImage\"></body></html>";
    
        AlternateView htmlView =
            AlternateView.CreateAlternateViewFromString(
                htmlBody, null, MediaTypeNames.Text.Html);
    
        // ...and then define the actual LinkedResource matching the
        // ContentID property as found in the image tag. In this case,
        // the HTML message includes the tag
        // <img src=\"cid:SampleImage\"> and the following
        // LinkedResource.ContentId is set to "SampleImage"
        LinkedResource sampleImage =
            new LinkedResource("sample.jpg",
                MediaTypeNames.Image.Jpeg);
        sampleImage.ContentId = "SampleImage";
    
        htmlView.LinkedResources.Add(sampleImage);
    
        m.AlternateViews.Add(htmlView);
    
        // Finally, configure smtp or alternatively use the
        // system.net mailSettings
        SmtpClient smtp = new SmtpClient
              {
                  Host = "smtp.example.com",
                  UseDefaultCredentials = false,
                  Credentials =
                      new NetworkCredential("username", "password")
              };
    
        //<system.net>
        //    <mailSettings>
        //        <smtp deliveryMethod="Network">
        //            <network host="smtp.example.com"
        //              port="25" defaultCredentials="true"/>
        //        </smtp>
        //    </mailSettings>
        //</system.net>
    
        smtp.Send(m);
    }
    catch (ArgumentException)
    {
        throw new
            ArgumentException("Undefined sender and/or recipient.");
    }
    catch (FormatException)
    {
        throw new
            FormatException("Invalid sender and/or recipient.");
    }
    catch (InvalidOperationException)
    {
        throw new
            InvalidOperationException("Undefined SMTP server.");
    }
    catch (SmtpFailedRecipientException)
    {
        throw new SmtpFailedRecipientException(
            "The mail server says that there is no mailbox for recipient");
    }
    catch (SmtpException ex)
    {
        // Invalid hostnames result in a WebException InnerException that
        // provides a more descriptive error, so get the base exception
        Exception inner = ex.GetBaseException();
        throw new SmtpException("Could not send message: " + inner.Message);
    }
    
    6 回复  |  直到 14 年前
        1
  •  19
  •   CraigTP    14 年前

    二者都 纯文本和HTML(如果您真的想发送HTML电子邮件)。

    哦,确保你真的发了 内容 在纯文本视图中,而不是一句话说“您必须使用支持HTML消息的电子邮件客户端”。googlemail采用了这种方法,它似乎工作得很完美,允许在成熟的PC客户端上查看“丰富的”视图,同时也允许在更受限制的设备(如移动/手机)上查看“最小的”视图。

    原来 (见一些人对此的看法 here here

    然而,在实用的现代现实世界中,HTML电子邮件是非常真实的,并且非常容易接受。发送HTML电子邮件的主要缺点是收件人是否会以您希望他们看到的方式看到电子邮件。这与网页设计师多年来一直在努力解决的问题大致相同;让他们的网站在所有可能的浏览器中看起来“恰到好处”(尽管现在比许多年前要容易得多)。

    类似于确保网站正常运行 无需 Javascript,通过以HTML和纯文本两种格式发送电子邮件,您将确保您的电子邮件 优雅地降格 因此,人们在(例如)小型移动设备上阅读他们的电子邮件(现在越来越流行的东西——可能或者可能无法呈现完整的HTML电子邮件)仍然可以毫无问题地阅读你的电子邮件内容。

        2
  •  4
  •   Jon Skeet    14 年前

    如果你只发送HTML,那么任何在纯文本设备上阅读电子邮件的人都会有麻烦。

    例如,我怀疑许多低端移动设备能够阅读电子邮件,但不能显示完整的HTML。

    我认为最好的做法是 任何一个

    我不明白为什么捕获一堆异常只是为了用不同的消息重新抛出相同的异常类型,顺便说一下,原始消息可能更具描述性。

        3
  •  3
  •   Tom Vervoort    14 年前

    发送这两者的另一个原因是,许多邮件服务器将只包含HTML内容的电子邮件标记为垃圾邮件。你不想把所有的邮件都放在垃圾文件夹里。

        4
  •  3
  •   AakashM    13 年前

    根据rfc2822,默认的电子邮件格式是纯文本。因此 电子邮件软件不需要支持HTML格式。发送 收件人的结束,如果它是不支持它的客户端之一。在 最坏的情况是,收件人将看到HTML代码而不是 预期消息。

        5
  •  2
  •   wave-rider    6 年前

    分享我在一封电子邮件中同时发送HTML和文本的经验: 我创建了一个电子邮件,它有两个视图:文本和html,使用C#AlternateView类。

    我得到了什么?

    在Mac上,在High Sierra上测试: 苹果邮件应用程序正在显示Html。如果邮件的顺序相反:Html-text,那么Apple Mail将显示文本视图。结论是:applemail使用最后一个视图作为默认值。

    在Windows中,Outlook 2010:

    如果出于某种原因,您选择了将传入邮件显示为文本的设置,则Outlook会将电子邮件的Html版本转换为文本。 即使你发送的是文本版本的邮件(可能与HTML版本略有不同,格式也很漂亮),它也不会被使用。

    邮件客户端

    希望有帮助

        6
  •  0
  •   Taggliatelli    8 年前

    多个电子邮件客户端将使用添加到AlternateView的最后一个AlternateView。

    因此,如果您希望您的邮件显示为HTML,请务必添加最后一个。