郵件HTML

System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();      mailMessage.From="發送者郵箱";      mailMessage.To.Add("收件人郵件列表");      mailMessage.CC.Add("抄送人郵件列表");      mailMessage.Subject = subject;      AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(content,null,"text/html");      LinkedResource lrImage = new LinkedResource("a.jpg","image/gif");      lrImage.ContentId = "Email001";      htmlBody.LinkedResources.Add(lrImage);      mailMessage.AlternateViews.Add(htmlBody);      SmtpClient.Send(mailMessage);    

例程:

摺疊C# 代碼
  1.             SmtpClient smtp = new SmtpClient();  
  2.             smtp.DeliveryMethod = SmtpDeliveryMethod.Network;  
  3.             smtp.Host = "smtp.163.com";  
  4.             smtp.Credentials = new NetworkCredential("renzhijie1111", "**");  
  5.  
  6.             MailMessage mm = new MailMessage();  
  7.             mm.From = new MailAddress("[email protected]", "test");  
  8.             mm.To.Add("[email protected]");  
  9.  
  10.             mm.Subject = "測試圖片郵件";  
  11.  
  12.             string plainTextBody = "如果你郵件客戶端不支持HTML格式,或者你切換到“普通文本”視圖,將看到此內容";  
  13.             mm.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain"));  
  14.  
  15.             ////HTML格式郵件的內容  
  16.             string htmlBodyContent = "如果你的看到<b>這個</b>, 說明你是在以 <span style=\"color:red\">HTML</span> 格式查看郵件<br><br>";  
  17.             htmlBodyContent += "<a href=\"http://www.fenbi360.net粉筆編程網</a> <img src=\"cid:weblogo\">";   //注意此處嵌入的圖片資源  
  18.             AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");  
  19.  
  20.              
  21.             LinkedResource lrImage = new LinkedResource(@"d:\1.jpg", "image/gif");  
  22.             lrImage.ContentId = "weblogo"; //此處的ContentId 對應 htmlBodyContent 內容中的 cid: ,如果設置不正確,請不會顯示圖片  
  23.             htmlBody.LinkedResources.Add(lrImage);  
  24.  
  25.             mm.AlternateViews.Add(htmlBody);  
  26.  
  27.             ////要求回執的標誌  
  28.             mm.Headers.Add("Disposition-Notification-To", "[email protected]");  
  29.  
  30.             ////自定義郵件頭  
  31.             mm.Headers.Add("X-Website", "http://www.fenbi360.net");  
  32.  
  33.             ////針對 LOTUS DOMINO SERVER,插入回執頭  
  34.             mm.Headers.Add("ReturnReceipt", "1");  
  35.  
  36.             mm.Priority = MailPriority.Normal; //優先級  
  37.             mm.ReplyTo = new MailAddress("[email protected]", "我自己");  
  38.  
  39.             ////如果發送失敗,SMTP 服務器將發送 失敗郵件告訴我  
  40.             mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;  
  41.  
  42.             ////異步發送完成時的處理事件  
  43.             smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);  
  44.  
  45.             ////開始異步發送  
  46.             smtp.SendAsync(mm, null);  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e)  
  53.         {  
  54.             if (e.Cancelled)  
  55.             {  
  56.                 MessageBox.Show("發送被取消");  
  57.             }  
  58.             else 
  59.             {  
  60.                 if (e.Error == null)  
  61.                 {  
  62.                     MessageBox.Show("發送成功");  
  63.                 }  
  64.                 else 
  65.                 {  
  66.                     MessageBox.Show("發送失敗: " + e.Error.Message);  
  67.                 }  
  68.             }  
  69.         }  
發佈了14 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章