C# 发送邮件中包含图片
Posted jake
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 发送邮件中包含图片相关的知识,希望对你有一定的参考价值。
List<string> To = new List<string>(); To.Add("[email protected]"); List<string> CC = new List<string>(); CC.Add("[email protected]"); MailMessage Message = new MailMessage(); Message.IsBodyhtml = true; Message.BodyEncoding = UTF8Encoding.UTF8; Message.SubjectEncoding = UTF8Encoding.UTF8; Message.Priority = MailPriority.Normal; Message.Sender = new MailAddress("[email protected]"); Message.Subject = "Test"; Message.From = new MailAddress("[email protected]"); for (int i = 0; i < To.Count; i++) { Message.To.Add(new MailAddress(To[i])); } for (int i = 0; i < CC.Count; i++) { Message.CC.Add(new MailAddress(CC[i])); } Message.Body = GenMailBody(); //添加附件 Message.Attachments.Add(new Attachment("C:\\aa.jpg")); //设置附件类型 Message.Attachments[0].ContentType.Name = "image/jpg"; //设置附件 Id Message.Attachments[0].ContentId = "ewen"; //设置附件为 inline-内联 Message.Attachments[0].ContentDisposition.Inline = true; //设置附件的编码格式 Message.Attachments[0].TransferEncoding = System.Net.Mime.TransferEncoding.Base64; SmtpClient client = new SmtpClient(); client.Host = "smtp.local"; client.DeliveryMethod = SmtpDeliveryMethod.Network; bool IsSend = false; try { client.Send(Message); } catch (Exception e1) { //MailLog(To, To, e1.Message.ToString()); }
主要是
Message.Attachments[0].ContentId = "ewen"
中设置的id要和mail body中img标签中的src要对应起来
public static string GenMailBody() { StringBuilder sb = new StringBuilder(); sb.Append("Dear Suppliers:<br/>"); sb.Append("近期Askey頻發亞旭進料標籤異常,主要因標籤Serial ID的編碼重複造成Askey不能上線,特此郵件通知各Suppliers特別注意此項!<br/>"); sb.Append("如有任何疑問請E-Mail寄送至[email protected], [email protected]<br/>"); sb.Append("<img alt=‘‘ src=cid:ewen>"); return sb.ToString(); }
以上是关于C# 发送邮件中包含图片的主要内容,如果未能解决你的问题,请参考以下文章