asp.net發送郵件時在本文中插入圖片

來源:互聯網
上載者:User

#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.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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.