C#發(fā)郵件內(nèi)嵌圖片例子一
郵件內(nèi)容調(diào)用圖片格式為:
發(fā)送郵件的服務(wù)端代碼為:
SmtpClient 發(fā)送郵件的對(duì)象 //代碼省略
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From="發(fā)送者郵箱";
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#發(fā)郵件內(nèi)嵌圖片例子二
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", "無(wú)敵outofmemory測(cè)試");
mm.To.Add("outofmemory@vip.qq.com");
mm.Subject = "發(fā)送帶圖片郵件";
string plainTextBody = "如果你郵件客戶(hù)端不支持HTML格式,或者你切換到“普通文本”視圖,將看到此內(nèi)容";
mm.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain"));
////HTML格式郵件的內(nèi)容
string htmlBodyContent = "如果你的看到<b>這個(gè)</b>, 說(shuō)明你是在以 <span style=\"color:red\">HTML</span> 格式查看郵件<br><br>";
htmlBodyContent += "<a href=\"
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");
LinkedResource lrImage = new LinkedResource(@"d:\1.jpg", "image/gif");
lrImage.ContentId = "weblogo"; //此處的ContentId 對(duì)應(yīng) htmlBodyContent 內(nèi)容中的 cid: ,如果設(shè)置不正確,請(qǐng)不會(huì)顯示圖片
htmlBody.LinkedResources.Add(lrImage);
mm.AlternateViews.Add(htmlBody);
////要求回執(zhí)的標(biāo)志
mm.Headers.Add("Disposition-Notification-To", "abc@163.com");
////自定義郵件頭
mm.Headers.Add("X-Website", "////針對(duì) LOTUS DOMINO SERVER,插入回執(zhí)頭
mm.Headers.Add("ReturnReceipt", "1");
mm.Priority = MailPriority.Normal; //優(yōu)先級(jí)
mm.ReplyTo = new MailAddress("outofmemory@163.com", "我自己");
////如果發(fā)送失敗,SMTP 服務(wù)器將發(fā)送 失敗郵件告訴我
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
////異步發(fā)送完成時(shí)的處理事件
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
////開(kāi)始異步發(fā)送
smtp.SendAsync(mm, null);
void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("發(fā)送被取消");
}
else
{
if (e.Error == null)
{
MessageBox.Show("發(fā)送成功");
}
else
{
MessageBox.Show("發(fā)送失?。?" + e.Error.Message);
}
}
}
該文章在 2017/11/8 0:08:42 編輯過(guò)