C# smtp郵件發送


第一種方式發送郵件,不讀取配置文件發送郵件,(本地測試可以,但是服務器上不行)

/// <summary>
/// 發送郵件
/// </summary>
/// <param name="emailAddressList">收件人地址集合</param>
/// <param name="Subject">郵件標題</param>
/// <param name="Body">郵件體(可以為html)</param>

public void email(List<string> emailAddressList, string Subject, string Body)

{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Host = "smtp.163.com";//使用163的SMTP服務器發送郵件
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            client.Credentials = new System.Net.NetworkCredential("15221591234@163.com", "1321510477");//163的SMTP服務器需要用163郵箱的用戶名和smtp授權碼作認證,如果沒有需要去163申請個,
            //這里假定你已經擁有了一個163郵箱的賬戶,用戶名為abc,密碼為*******
            System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
            Message.From = new System.Net.Mail.MailAddress("15221591234@163.com");//這里需要注意,163似乎有規定發信人的郵箱地址必須是163的,而且發信人的郵箱用戶名必須和上面SMTP服務器認證時的用戶名相同
            //因為上面用的用戶名abc作SMTP服務器認證,所以這里發信人的郵箱地址也應該寫為abc@163.com
            if (emailAddressList == null || emailAddressList.Count <= 0)
                return;
            foreach (string email in emailAddressList)
            {
                Message.To.Add(email);//將郵件發送給Gmail
            }
            //Message.To.Add("123456@gmail.com");//將郵件發送給Gmail
            //Message.To.Add("123456@qq.com");//將郵件發送給QQ郵箱
            Message.Subject = Subject;
            Message.Body = Body;
            Message.SubjectEncoding = System.Text.Encoding.UTF8;
            Message.BodyEncoding = System.Text.Encoding.UTF8;
            Message.Priority = System.Net.Mail.MailPriority.High;
            Message.IsBodyHtml = true;  //可以為html
            try
            {
                client.Send(Message);
            }
            catch(Exception ex)
            {
                string path = Server.MapPath("~") + "\\mail.txt";
                if (!System.IO.File.Exists(path))
                {
                    FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//創建寫入文件 
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//開始寫入值
                    sw.Close();
                    fs1.Close();
                }
                else
                {
                    StreamWriter sr = System.IO.File.AppendText(path);
                    sr.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//追加寫入值
                    sr.Close();
                }
            }
}

第二種方式,通配置web.config發送郵件,(服務器上也能成功,推薦這種方式,方便以后修改smtp服務)

配置文件如下

<configuration>
     <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="15221591234@163.com" >
        <network host="smtp.163.com" userName="15221591234@163.com" password="1321510477" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

代碼如下

/// <summary>
        /// 發送郵件
        /// </summary>
        /// <param name="emailAddressList">收件人地址集合</param>
        /// <param name="Subject">郵件標題</param>
        /// <param name="Body">郵件體(可以為html)</param>
        public void email(List<string> emailAddressList, string Subject, string Body)
        {
            try
            {
                SmtpSection cfg = NetSectionGroup.GetSectionGroup(WebConfigurationManager.OpenWebConfiguration("~/web.config")).MailSettings.Smtp;
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(cfg.Network.Host);
                client.UseDefaultCredentials = true;
                client.Credentials = new System.Net.NetworkCredential(cfg.Network.UserName, cfg.Network.Password);

                client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
                Message.From = new System.Net.Mail.MailAddress(cfg.From);//這里需要注意,163似乎有規定發信人的郵箱地址必須是163的,而且發信人的郵箱用戶名必須和上面SMTP服務器認證時的用戶名相同
                //因為上面用的用戶名abc作SMTP服務器認證,所以這里發信人的郵箱地址也應該寫為abc@163.com
                if (emailAddressList == null || emailAddressList.Count <= 0)
                    return;
                foreach (string email in emailAddressList)
                {
                    Message.To.Add(email);//將郵件發送給Gmail
                }
                //Message.To.Add("123456@gmail.com");//將郵件發送給Gmail
                //Message.To.Add("123456@qq.com");//將郵件發送給QQ郵箱
                Message.Subject = Subject;
                Message.Body = Body;
                Message.SubjectEncoding = System.Text.Encoding.UTF8;
                Message.BodyEncoding = System.Text.Encoding.UTF8;
                Message.Priority = System.Net.Mail.MailPriority.High;
                Message.IsBodyHtml = true;      //可以為html

                client.Send(Message);
            }
            catch (Exception ex)
            {
                string path = Server.MapPath("~") + "\\mail.txt";
                if (!System.IO.File.Exists(path))
                {
                    FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//創建寫入文件 
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//開始寫入值
                    sw.Close();
                    fs1.Close();
                }
                else
                {
                    StreamWriter sr = System.IO.File.AppendText(path);
                    sr.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//追加寫入值
                    sr.Close();
                }
            }
        }

smtp服務可以去163申請一個郵箱,然后開通smtp服務(其中15221591234@163.com是你郵箱地址,1321510477是你授權碼)

http://jingyan.baidu.com/article/4e5b3e19266fee91901e2489.html

 


免責聲明!

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



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