LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#发送邮件内嵌图片的两种方法

admin
2017年11月8日 0:5 本文热度 6018

C#发邮件内嵌图片例子一

邮件内容调用图片格式为:

发送邮件的服务端代码为:

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);

C#发邮件内嵌图片例子二

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 = "如果你的看到<b>这个</b>, 说明你是在以 <span style=\"color:red\">HTML</span> 格式查看邮件<br><br>";
htmlBodyContent += "<a href=\"http://outofmemory.cn/\">为程序员服务</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", "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);
      }
  }
}


该文章在 2017/11/8 0:08:42 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved