【C#】C#實現郵件發送


1、一個簡單的郵件發送程序

using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace TestExerciser.Logic
{
    public class SendMail
    {
        /// <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.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);


            //設置SMTP郵件服務器
            smtp.Host = host;

            try
            {
                //將郵件發送到SMTP郵件服務器
                smtp.Send(myMail);
                return true;

            }
            catch (System.Net.Mail.SmtpException exception)
            {
                MessageBox.Show(exception.Message, "異常消息提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;              
            }

        }
    }
}

 

private void sendLogMail()
        {
            if (ManageDB.userEmailAddress != null)
            {
                SendMail mail = new SendMail();
                mail.mailFrom = "TestExerciser@163.com";
                mail.mailPwd = "admin123";
                mail.mailSubject = "用例執行結果";
                mail.mailBody = MainAutoTesting.rootFolder + @"\.log";
                mail.isbodyHtml = false;
                mail.host = "smtp.163.com";
                mail.mailToArray = new string[] {ManageDB.userEmailAddress };
                mail.mailCcArray = new string[] { };
                if (mail.Send())
                {

                }
            }           
        }

 


免責聲明!

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



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