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("renzhijie1111@163.com", "無敵任志杰測試");
mm.To.Add("renzhijie1990@vip.qq.com");
mm.Subject = "發(fā)送帶圖片郵件";
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=\"https://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);
////要求回執(zhí)的標志
mm.Headers.Add("Disposition-Notification-To", "renzhijie1111@163.com");
////自定義郵件頭
mm.Headers.Add("X-Website", "https://www.jb51.net/");
////針對 LOTUS DOMINO SERVER,插入回執(zhí)頭
mm.Headers.Add("ReturnReceipt", "1");
mm.Priority = MailPriority.Normal; //優(yōu)先級
mm.ReplyTo = new MailAddress("renzhijie1111@163.com", "我自己");
////如果發(fā)送失敗,SMTP 服務器將發(fā)送 失敗郵件告訴我
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
////異步發(fā)送完成時的處理事件
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
////開始異步發(fā)送
smtp.SendAsync(mm, null);