.net中 发送邮件内容嵌入图片的具体实例

时间,渐渐带走了年少轻狂,也慢慢沉淀了冷暖自知。成功的道路上,肯定会有失败;对于失败,我们要正确地看待和对待,不怕失败者,则必成功;怕失败者,则一无是处,会更失败。

例程一


邮件内容调用图片格式为:<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("renzhijie1111@163.com", "无敌任志杰测试");
mm.To.Add("renzhijie1990@vip.qq.com");

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 rel="nofollow noopener noreferrer" href=\"//www.haodaima.com//%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", "renzhijie1111@163.com");

////自定义邮件头
mm.Headers.Add("X-Website", "//www.haodaima.com/");

////针对 LOTUS DOMINO SERVER,插入回执头
mm.Headers.Add("ReturnReceipt", "1");

mm.Priority = MailPriority.Normal; //优先级
mm.ReplyTo = new MailAddress("renzhijie1111@163.com", "我自己");

////如果发送失败,SMTP 服务器将发送 失败邮件告诉我
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

////异步发送完成时的处理事件
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);

////开始异步发送

smtp.SendAsync(mm, null);

本文.net中 发送邮件内容嵌入图片的具体实例到此结束。既然上了生活的贼船,那就做一个快乐的海盗。小编再次感谢大家对我们的支持!

标签: 发送邮件 net