C# 含有图片的html邮件发送

在做这种邮件发送的时候,我一般是用html编写一个邮件模板

例:

<html>
<head>
    <title></title>
</head>
<body>
   <table>
      <thead>
         <tr>
           <th>Name</th>
             <th>Age</th>
         </tr>
      </thead>
      <tbody>
        <tr>
           <td>$Name$<img src="cid:ico02" /></td>
           <td>$Age$<img src="cid:ico24" /></td>
        </tr>
      </tbody>
   </table>
</body>
</html>

这个是邮件模板 ,img标签的图片我和这个html页面放在一个文件夹里面.

再来后台代码

 public void Send(string UserAccountMailAddress,string Pwd,List<string> ToUserMailAddress)
        {
            string tempfile = Server.MapPath("/mails/MasterHtml.htm");
            string strIO = string.Empty;
            Encoding code = Encoding.GetEncoding("gb2312");
            StreamReader sr;
            try
            {
                sr = new StreamReader(tempfile, code);
                strIO = sr.ReadToEnd();
                sr.Close();
            }
            catch(Exception ex)
            {
                Response.Write(ex.Message);
                return;
            }
            strIO = strIO.Replace("$Name$","张三");  //这里是对html中的替换
            strIO = strIO.Replace("$Age$", "22"); //这里是对html中的替换
            MailModel msg = new MailModel();
            string Content = strIO; // StrIO替换后的html字符串
           string Address = "zhangsan@qq.com";
           string Subject = "test";
            SendMailMsg(Address,Subject,Content,"123");
        }

        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="Address"></param>

  /// <param name="Subject"></param>

  /// <param name="Content"></param>

  /// <param name="Pwd"></param>
        public  void SendMailMsg(string AddressSend,string Subject,string Content,string Pwd,string AddressReceive)
        {
            try
            {
                SmtpClient client = new SmtpClient("mail.sina.com", 25);
                MailAddress address = new MailAddress(, "mailsend", Encoding.UTF8);
                client.Credentials = new NetworkCredential(AddressSend,Pwd);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                MailMessage message = new MailMessage();
                message.From = AddressSend;
                string[] ToAddress = AddressReceive.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < ToAddress.Length; i++)
                {
                    message.To.Add(new MailAddress(ToAddress[i]));
                }
                message.Body = msg.Content;
                message.BodyEncoding = Encoding.UTF8;
                message.Subject = msg.Subject + "(系统自动邮件,请勿答复)";
                #region 邮件发图
                
                message.Attachments.Add(new Attachment(Server.MapPath("/mails/ico02.gif")));
                message.Attachments[0].ContentId = "ico02";
                message.Attachments[0].ContentDisposition.Inline = true;
                message.Attachments.Add(new Attachment(Server.MapPath("/mails/ico24.gif")));
                message.Attachments[1].ContentId = "ico24";
                message.Attachments[1].ContentDisposition.Inline = true;

                #endregion
                message.SubjectEncoding = Encoding.UTF8;
                message.IsBodyHtml = true;
                message.Sender = new MailAddress("XX", "XX", Encoding.GetEncoding("GB2312"));
                message.Priority = MailPriority.High;
                client.Send(message);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

总结一下,其实邮件发送图片就是控制内容的格式和附件的发送

原文地址:https://www.cnblogs.com/cindyOne/p/2669981.html