List<string> To = new List<string>(); To.Add("jake_ge@askey.com.tw"); List<string> CC = new List<string>(); CC.Add("jake_ge@askey.com.tw"); MailMessage Message = new MailMessage(); Message.IsBodyHtml = true; Message.BodyEncoding = UTF8Encoding.UTF8; Message.SubjectEncoding = UTF8Encoding.UTF8; Message.Priority = MailPriority.Normal; Message.Sender = new MailAddress("b2b@askey.com.tw"); Message.Subject = "Test"; Message.From = new MailAddress("b2b@askey.com.tw"); 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寄送至Huiqin_zhang@askey.com.tw, sqe_team@askey.com.tw<br/>"); sb.Append("<img alt='' src=cid:ewen>"); return sb.ToString(); }