如何發送內嵌圖片的 E-mail ( Inline Attachment )

我們在發送 HTML 郵件時,通常會將網頁中的圖片都全部上傳到網站,然後在 E-Mail 內的所有圖片就直接設定絕對網址(Absolute URL)就可以了,但這有個缺點,就是當使用者「看信」的時候如果沒有上網,將會無法看到完整的郵件內容,因為沒網路無法下載圖片。

我們也有另一種選擇,就是將圖片、CSS直接內嵌到郵件中,使用者收到信的時候不會看到有個「附件」在郵件裡,而是直接將圖片顯示在郵件本文裡,又不用上網下載圖片,但唯一的缺點就是每一封郵件的 KB 數會比較大,發送的時候比較暫頻寬。

我在【System.Web.Mail 太陽春、System.Net.Mail 有問題】這篇文章已經有提到在使用 System.Net.Mail 時最好採用 UTF-8 編碼,雖然不會影響附件檔案的發送,但還是提醒各位要注意字集的問題。

以下是範例程式:

[code:c#] 

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace SystemNetMailTest
{
   class Program
   {
      static void Main(string[] args)
      {
         string myMailEncoding = "utf-8";

         string myFromEmail = "testtest.com";
         string myFromName = "測試寄件者";

         string myToEmail = "testtest.com";
         string myToName = "測試收件者";

         MailAddress from = 
            new MailAddress(myFromEmail, myFromName, 
                Encoding.GetEncoding(myMailEncoding));
         MailAddress to = 
            new MailAddress(myToEmail, myToName, 
                Encoding.GetEncoding(myMailEncoding));

         MailMessage myMessage = new MailMessage(from, to);

         myMessage.Subject = "郵件主旨";
         myMessage.SubjectEncoding = Encoding.GetEncoding(myMailEncoding);

         myMessage.Body = "<h1>這是郵件內容</h1><hr/><img src=\"Logo.gif\" />";
         myMessage.BodyEncoding = Encoding.GetEncoding(myMailEncoding);

         myMessage.IsBodyHtml = true;

         myMessage.Priority = MailPriority.High;

         // 設定附件檔案(Attachment)

         string strFilePath = @"C:\Logo.gif";

         System.Net.Mail.Attachment attachment1 =
            new System.Net.Mail.Attachment(strFilePath);
         attachment1.Name = System.IO.Path.GetFileName(strFilePath);
         attachment1.NameEncoding = Encoding.GetEncoding(myMailEncoding);
         attachment1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

         // 設定該附件為一個內嵌附件(Inline Attachment)
         attachment1.ContentDisposition.Inline = true;
         attachment1.ContentDisposition.DispositionType =
            System.Net.Mime.DispositionTypeNames.Inline;

         myMessage.Attachments.Add(attachment1);

         SmtpClient smtp = new SmtpClient("localhost");

         try
         {
             smtp.Send(myMessage);
             Console.WriteLine(DateTime.Now.ToString() + " 寄信成功");
         }
         catch
         {
             Console.WriteLine(DateTime.Now.ToString() + " 寄信失敗!!!");
         }

         Console.ReadKey();
     }
  }
}

以上的範例特別要注意的地方就是在使用 <img src="logo.gif" /> 的時候,必須要跟在 C# 中設定 attachment1.Name 的設定一模一樣才會正確顯示圖片!

如下圖示,郵件中看的到圖片,但沒有附件的檔案,使用者幾乎沒辦法辨識「內嵌圖檔」與「設定圖片網址」這兩種方式的差異。

 內嵌圖片的 E-mail 圖示

相關網址如下:

  

此文章由 will 發表於 2008/2/6 下午 03:04:00

永久連結 | 評論 (5) | 此文章的RSSRSS comment feed |

分類: .Net | C#

標籤: , , ,

收藏:

相關文章

評論

七月 31. 2008 12:36

willym

請問您在附件檔案寄出時,附件檔案是否有檔案的限制?
因為先前是初如果大於1MB檔案時,附件信件無法寄出!

非常感謝您!!

willym tw

七月 31. 2008 12:44

Will 保哥

檔案大小的限制是在「郵件伺服器」(Mail Server)設定的,跟 .NET 沒關係。

如果你發信不出去,你可以看你用哪一台 SMTP Server 發信,然後去查那一台主機的「單一郵件最大限制」的相關設定。

Will 保哥 tw

七月 31. 2008 12:51

willym

非常感謝您

willym tw

九月 22. 2008 15:33

Peggy

我想請問~~
若我程式完整了
收件者但是卻遲遲收不到信件~~
是遺漏掉何處ㄋ

Peggy tw

九月 22. 2008 15:38

will

有許多可能耶,例如:
1. 本機的 SMTP 伺服器沒發信,或因為網路問題暫時沒發信
2. 收件者端的 SMTP 沒收信或無法收信
3. 收了信被丟棄(例如被辨識為垃圾信)
4. ... ...

will tw

新增評論


(將顯示您的Gravatar圖示)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



線上預覽

十月 14. 2008 06:10