郵件內容調用圖片格式爲:<img src=\"cid:Email001\">
html
發送郵件的服務端代碼爲:程序員
SmtpClient 發送郵件的對象 //代碼省略web
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("outofmemory", "**"); MailMessage mm = new MailMessage(); mm.From = new MailAddress("outofmemory@163.com", "無敵outofmemory測試"); mm.To.Add("outofmemory@vip.qq.com"); mm.Subject = "發送帶圖片郵件"; string plainTextBody = "若是你郵件客戶端不支持HTML格式,或者你切換到「普通文本」視圖,將看到此內容"; mm.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain")); ////HTML格式郵件的內容 string htmlBodyContent = "若是你的看到這個, 說明你是在以 HTML 格式查看郵件 "; htmlBodyContent += "爲程序員服務 "; //注意此處嵌入的圖片資源 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", "abc@163.com"); ////自定義郵件頭 mm.Headers.Add("X-Website", "http://outofmemory.cn/"); ////針對 LOTUS DOMINO SERVER,插入回執頭 mm.Headers.Add("ReturnReceipt", "1"); mm.Priority = MailPriority.Normal; //優先級 mm.ReplyTo = new MailAddress("outofmemory@163.com", "我本身"); ////若是發送失敗,SMTP 服務器將發送 失敗郵件告訴我 mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; ////異步發送完成時的處理事件 smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted); ////開始異步發送 smtp.SendAsync(mm, null); void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Cancelled) { MessageBox.Show("發送被取消"); } else { if (e.Error == null) { MessageBox.Show("發送成功"); } else { MessageBox.Show("發送失敗: " + e.Error.Message); } } }