C# 發送郵件到指定郵箱


  • C#使用 System.Net.Mail發送郵件功能


  1. 注意要引用 System.Net.Mail
  2. 使用163的SMTP服務器發送郵件,要提前申請一個授權碼
  3.    
    public class EmailHelper
        {
            private readonly static string SmtpServer = "smtp.163.com";//smtp服務器
            private readonly static int SmtpServerPort = 25;
            private readonly static bool SmtpEnableSsl = false;
            private readonly static string SmtpUsername = "138******@163.com";//發件人郵箱地址
            private readonly static string SmtpDisplayName = "***科技";//發件人昵稱
            private readonly static string SmtpUserPassword = "655***";//授權碼
    
            /// <summary>
            /// 發送郵件到指定收件人
            /// </summary>
            /// <param name="to">收件人地址</param>
            /// <param name="subject">主題</param>
            /// <param name="mailBody">正文內容(支持HTML)</param>
            /// <param name="copyTos">抄送地址列表</param>
            /// <returns>是否發送成功</returns>
            public static bool Send(string to, string subject, string mailBody, params string[] copyTos)
            {
                return Send(new[] { to }, subject, mailBody, copyTos, new string[] { }, MailPriority.Normal);
    
            }
    
            /// <summary>
            /// 發送郵件到指定收件人
            /// </summary>
            /// <remarks>
            ///  2013-11-18 18:55 Created By iceStone
            /// </remarks>
            /// <param name="tos">收件人地址列表</param>
            /// <param name="subject">主題</param>
            /// <param name="mailBody">正文內容(支持HTML)</param>
            /// <param name="ccs">抄送地址列表</param>
            /// <param name="bccs">密件抄送地址列表</param>
            /// <param name="priority">此郵件的優先級</param>
            /// <param name="attachments">附件列表</param>
            /// <returns>是否發送成功</returns>
            /// <exception cref="System.ArgumentNullException">attachments</exception>
            public static bool Send(string[] tos, string subject, string mailBody, string[] ccs, string[] bccs, MailPriority priority, params Attachment[] attachments)
            {
                if (attachments == null)
                    throw new ArgumentNullException("attachments");
                if (tos.Length == 0)
                    return false;
                //創建Email實體
                var message = new MailMessage();
                message.From = new MailAddress(SmtpUsername, SmtpDisplayName);
                message.Subject = subject;
                message.Body = mailBody;
                message.BodyEncoding = Encoding.UTF8;
                message.IsBodyHtml = true;
                message.Priority = priority;
    
                //插入附件
                foreach (var attachent in attachments)
                {
                    message.Attachments.Add(attachent);
                }
                //插入收件人地址,抄送地址和密件抄送地址
                foreach (var to in tos.Where(t => !string.IsNullOrEmpty(t)))
                {
                    message.To.Add(new MailAddress(to));
                }
                foreach (var cc in ccs.Where(c => !string.IsNullOrEmpty(c)))
                {
                    message.CC.Add(new MailAddress(cc));
                }
                foreach (var bcc in bccs.Where(bc => !string.IsNullOrEmpty(bc)))
                {
                    message.Bcc.Add(new MailAddress(bcc));
                }
    
                //創建Smtp客戶端
                var client = new SmtpClient
                {
                    Host = SmtpServer,
                    Credentials = new NetworkCredential(SmtpUsername, SmtpUserPassword),
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    EnableSsl = SmtpEnableSsl,
                    Port = SmtpServerPort
                };
    
                //發送郵件
                client.Send(message);
    
                return true;
            }
    
        }

     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM