C#发送带图片邮件

C#有专门用于邮件发送的类MailMessage,在设置邮件正文为html类型后可以发送带图片的邮件
图片有两种附带方式

  1. 使用base64编码
    将图片转化为base64的形式放在img标签的src里,这种方式有一个弊端,正文数据量太大,字符串太长了,如果有多张图片,正文长度会让人崩溃的,速度也会特别慢,因为我的服务器特别烂,不知道是电脑原因还是自己代码原因,在代码生成base64然后替换图片路径的时候,会卡好久

  2. 附件图片,然后通过设置ID引用
    MailMessage自带了添加附件的类LinkedResource,在设置AlternateView为MediaTypeNames.Text.Html后,添加LinkedResource实例,然后正文图片的src设置地址为cid:ID即可引用附件里的图片,下面为代码

     /// 
     /// C#发送邮件函数
     /// 
     /// 发送者邮箱
     /// 发送人
     /// 接受者邮箱
     /// 收件人
     /// 主题
     /// 内容
     /// 附件
     /// smtp服务器
     /// 邮箱
     /// 密码
     /// 
     public bool ExecSend(string sfrom, string sfromer, string password, string sto, string stoer, string sSubject, string sBody, string sfile)
     {
         MailMessage oMail = new MailMessage();
         oMail.From = new MailAddress(sfrom, sfromer);
         string[] list = sto.Split(','); //多个收件人 ,隔开
         list = RemoveDuplicateString(list); //去除重复的收件人
         foreach (var item in list)
         {
             oMail.To.Add(item);
         }
         //// 添加附件
         if (sfile != "")
         {
             string[] Files = sfile.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
             for (int i = 0; i < Files.Length; i++)
             {
                 oMail.Attachments.Add(new Attachment(Files[i]));
             }
         }
         sBody = ReplaceImg(sBody, oMail);  // 处理图片
         oMail.Subject = sSubject;  //邮件标题
         oMail.Body = sBody;   //邮件内容
         oMail.IsBodyHtml = true;  //邮件格式
         oMail.BodyEncoding = Encoding.GetEncoding("GB2312");  //邮件采用的编码
         oMail.Priority = MailPriority.High;  //设置邮件的优先级为高
         SmtpClient client = new SmtpClient();  //发送邮件
         client.Host = "10.236.254.15";
         client.Credentials = new NetworkCredential(sfrom, password);
         client.DeliveryMethod = SmtpDeliveryMethod.Network;
         client.Port = 25;
         try
         {
             client.Send(oMail);
             return true;
         }
         catch (Exception e)
         {
             ILogs il = new LogsEx();
             il.AddLog("发送邮件失败:" + e.Message.ToString());
             return false;
         }
         finally
         {
             oMail.Dispose();  //释放资源
         }
     }
    
     /// 
     /// 去除重复项
     /// 
     /// 
     /// 
     private string[] RemoveDuplicateString(string[] items)
     {
         List ret = new List();
         for (int i = 0; i < items.Length; i++)
         {
             if (!ret.Contains(items[i]))
                 ret.Add(items[i]);
         }
         return ret.ToArray();
     }
    
     /// 
     /// 处理邮件内容中的图片
     /// 将图片改为附件形式在body中显示
     /// 
     /// 
     /// 
     private string ReplaceImg(string body, MailMessage m)
     {
         try
         {
             Dictionary imgDic = new Dictionary();
             body = GetImgStrem(body, imgDic);
             AlternateView avHtml = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
             if (imgDic.Count != 0) // 内容添加图片
             {
                 foreach (var dic in imgDic)
                 {
                     LinkedResource lrImage = new LinkedResource(dic.Value, "image/gif");
                     lrImage.ContentId = dic.Key;
                     avHtml.LinkedResources.Add(lrImage);
                 }
             }
             m.AlternateViews.Add(avHtml);
             return body;
         }
         catch(Exception)
         {
             return body;
         }
     }
    
     private string GetImgStrem(string body, Dictionary imgDic)
     {
         string mactch;
         Regex reg = new Regex(@"(?i)]*?\ssrc\s*=\s*(['""]?)(?[^'""\s>]+)\1[^>]*>");
         MatchCollection mc = reg.Matches(body);
         for(int i = 0;i < mc.Count;i++)
         {
             string key = "pic" + i;
             mactch = mc[i].Groups["src"].Value;
             body = body.Replace(mactch, "cid:" + key);
             FileStream fs = new FileStream(mactch, FileMode.Open);
             imgDic.Add(key, fs);
         }
         return body;
     }
    

你可能感兴趣的:(C#语言)