2010-08-11 38 views
20

發送HTML和文本電子郵件是否是最佳做法?C#發送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("[email protected]", "Sender"); 

    MailAddress recipient = 
     new MailAddress("[email protected]", "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); 
} 
+4

有關您的普通內容的一件事是我認爲如果我收到某人的郵件時說「您必須使用支持HTML郵件的電子郵件客戶端」。如果一個陌生人告訴我我應該運行什麼軟件,我不能詳細說明我的想法,因爲我不知道關於褻瀆行爲的stackoverflow政策,但它不利於我的繼續定製。 – 2010-08-11 07:59:45

回答

18

我要說的是,在今天的世界裏,「最佳實踐」的方法是,以確保您發送消息純文本和HTML(如果你真的想發送HTML電子郵件)。

哦,並確保你確實發送了內容純文本視圖,而不是一句「你必須使用支持HTML郵件的電子郵件客戶端」。 Google Mail採用這種方法,它看起來完美無缺,在完整的個人電腦客戶端上提供「豐富」的視圖,同時在更多受限設備(即移動/手機)上實現「最小化」視圖。

如果你想採取純粹的觀點,你根本不會發送HTML電子郵件,也不會將二進制文件「附加」到電子郵件中。原始電子郵件標準的兩次損壞都是純文本原本只有。 (看到這個herehere的一些人的意見)

但是,在實用的現代現實世界中,HTML電子郵件是非常真實的,並且非常可以接受。發送HTML電子郵件的主要缺點是收件人是否會按照您希望他們看到的方式看到電子郵件。這與網頁設計師多年來一直在努力的問題大致相同;讓他們的網站在所有可能的瀏覽器中看起來「恰到好處」(儘管今天比許多年前要容易得多)。

正確確保網站的功能,而不需要使用Javascript通過發送您的電子郵件既是HTML和純文本相似,你會確保你的電子郵件優雅降級讓人們閱讀(例如他們的電子郵件)小型移動設備(現在變得越來越流行 - 並且可能或不可能呈現完整的HTML電子郵件)仍可以毫無問題地閱讀您的電子郵件內容。

+1

非常感謝所有已回答的人 - 謝謝!並感謝保持褻瀆喬恩:-) – 2010-08-12 09:33:15

+0

對於未來的發現者:RFC 822和2822規定標準是使用文本或(文本和HTML),而不僅僅是HTML。正確的網絡禮節是發送純文本電子郵件(谷歌netiquette作爲參考)。 – 2012-09-11 13:45:05

4

如果只發送HTML,那麼任何人都純文本設備上閱讀電子郵件會遇到麻煩。

例如,我懷疑很多低端移動設備都能夠閱讀電子郵件,但不能顯示完整的HTML。

我會說最好的做法是要麼發送純文本或文本和HTML。

順便說一句,我不明白爲什麼你要捕捉一堆異常,只是爲了重新拋出具有不同消息的相同異常類型 - 原始消息可能更具描述性。

+0

重新捕捉異常並拋出新異常 - 更不用說堆棧跟蹤正在被拆除,從而降低了「可調試性」。如果有什麼被捕獲的異常應該被重新拋出。 – erlando 2011-02-17 19:31:51

3

我認爲是的,最好的做法是發送兩個。原因(來自wikipedia的c & p):

根據RFC 2822的默認電子郵件格式是純文本。因此 電子郵件軟件不需要支持HTML格式。發送 如果HTML格式的電子郵件是不支持它的客戶端之一,則HTML格式的電子郵件可能會在接收方的末端導致問題。在最糟糕的情況下,收件人將看到HTML代碼而不是 預期消息。

3

發送這兩個郵件的另一個原因是,許多郵件服務器將僅包含HTML內容的電子郵件標記爲垃圾郵件。您不希望所有的電子郵件都放在垃圾文件夾中。

+0

信息來源? (這裏沒有無禮) – 2013-02-13 08:19:13

+0

我知道這是非常古老的,但我會信任郵件黑猩猩作爲一個值得信賴的來源:[垃圾郵件過濾器](http://templates.mailchimp.com/concepts/spam-filters/)滾動所有通往底部的路。最後一點要點:**「不要單獨發送HTML電子郵件,而是始終包含該純文本替代郵件。」** – hylander0 2015-04-08 19:14:56

0

幾個電子郵件客戶端將使用添加到AlternateViews的最後一個AlternateView。

因此,如果您希望將郵件顯示爲HTML,請確保最後添加該郵件。

我注意到這個IOS郵件和OSX郵件,而Android電子郵件似乎更喜歡HTML,如果它可用。我不確定這是哪個版本,行爲通常是用戶可配置的,但根據我的經驗,這些都是默認設置。