C#傳送郵件
阿新 • • 發佈:2021-07-13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Timers;
using System.Xml;
using System.Text.RegularExpressions;
using System.Net;
namespace SendEmail
{
class Program
{
#region 傳送郵件
static void Main(string[] args)
{
Program p = new Program();
string FromMial = ""; string ToMial = ""; string AuthorizationCode = ""; // string ReplyTo=""; // string CCMial=""; string File_Path = ""; p.SendMail(FromMial, ToMial, AuthorizationCode, null, null, File_Path); } /// <summary> /// 傳送郵件方法 /// </summary> /// <param name="FromMial">發件人郵箱</param> /// <param name="ToMial">收件人郵箱(多個收件人地址用";"號隔開)</param> /// <param name="AuthorizationCode">發件人授權碼</param> /// <param name="ReplyTo">對方回覆郵件時預設的接收地址(不設定也是可以的)</param> /// <param name="CCMial">//郵件的抄送者(多個抄送人用";"號隔開)</param> /// <param name="File_Path">附件的地址</param> public void SendMail(string FromMial, string ToMial, string AuthorizationCode, string ReplyTo, string CCMial, string File_Path) { try { //例項化一個傳送郵件類。 MailMessage mailMessage = new MailMessage(); //郵件的優先順序,分為 Low, Normal, High,通常用 Normal即可 mailMessage.Priority = MailPriority.Normal; //發件人郵箱地址。 mailMessage.From = new MailAddress(FromMial); //收件人郵箱地址。需要群發就寫多個 //拆分郵箱地址 List<string> ToMiallist = ToMial.Split(';').ToList(); for (int i = 0; i < ToMiallist.Count; i++) { mailMessage.To.Add(new MailAddress(ToMiallist[i])); //收件人郵箱地址。 } if (ReplyTo == "" || ReplyTo == null) { ReplyTo = FromMial; } //對方回覆郵件時預設的接收地址(不設定也是可以的喲) mailMessage.ReplyTo = new MailAddress(ReplyTo); if (CCMial != "" && CCMial != null) { List<string> CCMiallist = ToMial.Split(';').ToList(); for (int i = 0; i < CCMiallist.Count; i++) { //郵件的抄送者,支援群發 mailMessage.CC.Add(new MailAddress(CCMial)); } } //如果你的郵件標題包含中文,這裡一定要指定,否則對方收到的極有可能是亂碼。 //mailMessage.SubjectEncoding = Encoding.GetEncoding(587); //郵件正文是否是HTML格式 mailMessage.IsBodyHtml = false; //郵件標題。 mailMessage.Subject = "TEST SENDING EMAIL"; //郵件內容。 mailMessage.Body = "THIS IS A TEST OF SENDING EMAIL"; //設定郵件的附件,將在客戶端選擇的附件先上傳到伺服器儲存一個,然後加入到mail中 if (File_Path != "" && File_Path != null) { //將附件新增到郵件 mailMessage.Attachments.Add(new Attachment(File_Path)); //獲取或設定此電子郵件的傳送通知。 mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; } //例項化一個SmtpClient類。 SmtpClient client = new SmtpClient(); #region 設定郵件伺服器地址 //在這裡我使用的是163郵箱,所以是smtp.163.com,如果你使用的是qq郵箱,那麼就是smtp.qq.com。 // client.Host = "smtp.163.com"; if (FromMial.Length != 0) { //根據發件人的郵件地址判斷髮件伺服器地址 預設埠一般是25 string[] addressor = FromMial.Trim().Split(new Char[] { '@', '.' }); switch (addressor[1]) { case "163": client.Host = "smtp.163.com"; break; case "126": client.Host = "smtp.126.com"; break; case "qq": client.Host = "smtp.qq.com"; break; case "gmail": client.Host = "smtp.gmail.com"; break; case "hotmail": client.Host = "smtp.live.com";//outlook郵箱 //client.Port = 587; break; case "foxmail": client.Host = "smtp.foxmail.com"; break; case "sina": client.Host = "smtp.sina.com.cn"; break; default: client.Host = "smtp.exmail.qq.com";//qq企業郵箱 break; } } #endregion //使用安全加密連線。 client.EnableSsl = true; //不和請求一塊傳送。 client.UseDefaultCredentials = false; //驗證發件人身份(發件人的郵箱,郵箱裡的生成授權碼); client.Credentials = new NetworkCredential(FromMial, AuthorizationCode); //如果傳送失敗,SMTP 伺服器將傳送 失敗郵件告訴我 mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; //傳送 client.Send(mailMessage); Console.WriteLine("傳送成功"); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("傳送失敗"); } } #endregion }
}