C# 發送郵件

來源:互聯網
上載者:User

標籤:

Email類

 public class Email    {        /// <summary>        /// 寄件者        /// </summary>        public string mailFrom { get; set; }        /// <summary>        /// 收件者        /// </summary>        public string[] mailToArray { get; set; }        /// <summary>        /// 抄送        /// </summary>        public string[] mailCcArray { get; set; }        /// <summary>        /// 標題        /// </summary>        public string mailSubject { get; set; }        /// <summary>        /// 本文        /// </summary>        public string mailBody { get; set; }        /// <summary>        /// 寄件者密碼        /// </summary>        public string mailPwd { get; set; }        /// <summary>        /// SMTP郵件伺服器        /// </summary>        public string host { get; set; }        /// <summary>        /// 本文是否是html格式        /// </summary>        public bool isbodyHtml { get; set; }        /// <summary>        /// 附件        /// </summary>        public string[] attachmentsPath { get; set; }        public bool Send()        {            //使用指定的郵件地址初始化MailAddress執行個體            MailAddress maddr = new MailAddress(mailFrom);            //初始化MailMessage執行個體            MailMessage myMail = new MailMessage();            //向收件者地址集合添加郵件地址            if (mailToArray != null)            {                for (int i = 0; i < mailToArray.Length; i++)                {                    myMail.To.Add(mailToArray[i].ToString());                }            }            //向抄送收件者地址集合添加郵件地址            if (mailCcArray != null)            {                for (int i = 0; i < mailCcArray.Length; i++)                {                    myMail.CC.Add(mailCcArray[i].ToString());                }            }            //寄件者地址            myMail.From = maddr;            //電子郵件的標題            myMail.Subject = mailSubject;            //電子郵件的主題內容使用的編碼            myMail.SubjectEncoding = Encoding.UTF8;            //電子郵件本文            myMail.Body = mailBody;            //電子郵件本文的編碼            myMail.BodyEncoding = Encoding.Default;            myMail.Priority = MailPriority.High;            myMail.IsBodyHtml = isbodyHtml;            //在有附件的情況下添加附件            try            {                if (attachmentsPath != null && attachmentsPath.Length > 0)                {                    Attachment attachFile = null;                    foreach (string path in attachmentsPath)                    {                        attachFile = new Attachment(path);                        myMail.Attachments.Add(attachFile);                    }                }            }            catch (Exception err)            {                throw new Exception("在添加附件時有錯誤:" + err);            }            SmtpClient smtp = new SmtpClient();            //設定SMTP郵件伺服器            smtp.Host = host;            // smtp.EnableSsl = true;            smtp.Port = 25;            //smtp.UseDefaultCredentials = true;              //指定寄件者的郵件地址和密碼以驗證寄件者身份            smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);            try            {                //將郵件發送到SMTP郵件伺服器                smtp.Send(myMail);                return true;            }            catch (System.Net.Mail.SmtpException ex)            {                return false;            }        }    }

發送代碼

 static void Main(string[] args)        {            Email email = new Email();            email.mailFrom = "[email protected]";            email.mailPwd = "jie0628";            email.mailSubject = "程式員的生活";            email.mailBody = "很多程式員的其實非常艱苦的一個事情";            email.isbodyHtml = true;            email.host = "smtp.qq.com";            email.mailToArray = new string[] { "[email protected]" };            //email.mailToArray = new string[] { "[email protected]", "[email protected]" };            //email.mailCcArray = new string[] { "[email protected]" };            email.attachmentsPath = new string[] { };            if (email.Send())            {                Console.WriteLine("郵件發送成功~");                Console.ReadKey();            }            else            {                Console.WriteLine("郵件發送失敗~");                Console.ReadKey();                        }        }

 

C# 發送郵件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.