• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#发送邮件时在正文中插入图片(邮件中直接包含图片)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

#region 发送邮件
        /// <summary>
        /// 通过附件发送邮件
        /// </summary>
        /// <param name="emailFrom">发送人邮箱地址</param>

        /// <param name="files">图片地址</param>
        /// <param name="emailFromName">发送人姓名</param>
        /// <param name="zsrList">收件人集合</param>
        /// <param name="csrList">抄送人集合</param>
        /// <param name="arrFilePath">附件路径</param>
        /// <param name="mailSubject">主题</param>
        /// <param name="mailBody">正文内容</param>
        /// <returns></returns>
        public static bool SendEmail(string emailFrom, string[] files, string emailFromName, IList<CSR> zsrList, IList<CSR> csrList, ArrayList arrFilePath, string mailSubject, string mailBody)
        {
            MailMessage Email = new MailMessage();
            try
            {
                MailAddress EmailFrom = new MailAddress(emailFrom, emailFromName); //创建发件人邮箱地址对象
                Email.From = EmailFrom; //指定发件人
                Email.CC.Add(EmailFrom); //将发件人作为抄送人发一份
                for (int i = 0; i < zsrList.Count; i++) //循环添加主送人邮箱地址对象
                {
                    MailAddress zsrAddr = new MailAddress(zsrList[i].EMAIL, zsrList[i].NAME);
                    Email.To.Add(zsrAddr);
                }
                for (int i = 0; i < csrList.Count; i++) //循环添加抄送人邮箱地址对象
                {
                    MailAddress csrAddr = new MailAddress(csrList[i].EMAIL, csrList[i].NAME);
                    Email.CC.Add(csrAddr);
                }
            }
            catch
            {
                return false;
            }
            Email.IsBodyHtml = true;
            Email.BodyEncoding = System.Text.Encoding.UTF8;
            Email.Body += mailBody;
            for (int i = 0; i < files.Length; i++)
            {
                System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(files[i]);
                Email.Attachments.Add(attachment);
                Email.Body += "<img src=\"cid:" + attachment.ContentId + "\"/>";
  Email.Body +="<br><font color=red>400电话 受理中心欢迎您的光临,网址:http://www.my400800.cn </a>";
            }
            Email.Priority = MailPriority.High;
            //邮件主题
            Email.Subject = mailSubject;
            Email.SubjectEncoding = Encoding.GetEncoding(936);
            //邮件附件
            for (int i = 0; i < arrFilePath.Count; i++)
            {
                string file = arrFilePath[i].ToString(); //附件路径
                Attachment data = new Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
                // Add time stamp information for the file.
                System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(file);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
                // Add the file attachment to this e-mail message.
                Email.Attachments.Add(data);
            }
            SmtpClient Client = new SmtpClient("172.30.1.13");
            Client.UseDefaultCredentials = false;
            Client.Credentials = new System.Net.NetworkCredential("shoa", "shnetweb.1234");
            Client.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                Client.Send(Email);
            }
            catch (SmtpFailedRecipientsException ex)
            {
                for (int i = 0; i < ex.InnerExceptions.Length; i++)
                {
                    SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
                    if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable)
                    {
                        //Response.Write("Delivery failed - retrying in 5 seconds.");
                        System.Threading.Thread.Sleep(5000);
                        Client.Send(Email);
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                for (int i = 0; i < Email.Attachments.Count; i++) //释放占用excel资源
                {
                    Email.Attachments[i].Dispose();
                }
            }
            return true;
        }
        #endregion

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#ConfigurationManager类的使用发布时间:2022-07-10
下一篇:
asp.netC#打开word文件时命令失败发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap