例程一
郵件內容調用圖片格式為:<img src=\"cid:Email001\">
發送郵件的服務端代碼為:
SmtpClient 發送郵件的對象 //代碼省略
復制代碼 代碼如下:
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);
例程二
復制代碼 代碼如下:
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = "smtp.163.com";
smtp.Credentials = new NetworkCredential("renzhijie1111", "**");
MailMessage mm = new MailMessage();
mm.From = new MailAddress("[email protected]", "無敵任志傑測試");
mm.To.Add("[email protected]");
mm.Subject = "發送帶圖片郵件";
string plainTextBody = "如果你郵件客戶端不支持HTML格式,或者你切換到“普通文本”視圖,將看到此內容";
mm.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain"));
////HTML格式郵件的內容
string htmlBodyContent = "如果你的看到<b>這個</b>, 說明你是在以 <span style=\"color:red\">HTML</span> 格式查看郵件<br><br>";
htmlBodyContent += "<a href=\"http://www.jb51.net//%22%3EVA娛樂網</a> <img src=\"cid:weblogo\">"; //注意此處嵌入的圖片資源
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");
LinkedResource lrImage = new LinkedResource(@"d:\1.jpg", "image/gif");
lrImage.ContentId = "weblogo"; //此處的ContentId 對應 htmlBodyContent 內容中的 cid: ,如果設置不正確,請不會顯示圖片
htmlBody.LinkedResources.Add(lrImage);
mm.AlternateViews.Add(htmlBody);
////要求回執的標志
mm.Headers.Add("Disposition-Notification-To", "[email protected]");
////自定義郵件頭
mm.Headers.Add("X-Website", "http://www.jb51.net/");
////針對 LOTUS DOMINO SERVER,插入回執頭
mm.Headers.Add("ReturnReceipt", "1");
mm.Priority = MailPriority.Normal; //優先級
mm.ReplyTo = new MailAddress("[email protected]", "我自己");
////如果發送失敗,SMTP 服務器將發送 失敗郵件告訴我
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
////異步發送完成時的處理事件
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
////開始異步發送
smtp.SendAsync(mm, null);