C# 通過QQ郵箱和阿裡雲郵箱自動發送郵件(對System.Web.Mail與System.Net.Mail的測試)

来源:https://www.cnblogs.com/mbcxm/archive/2022/07/11/16467039.html
-Advertisement-
Play Games

第三回 萬文疑謀生思緒 璃月港口見清玉 雲溪愣了下,在他的認知中,神之眼正常而言不會有黑色的存在,就像在實數的體系內不會有i一樣,他搖了搖頭。而紀存初的眼中有閃過一次迷茫和失望,很快恢復過來,笑道:“算了,反正也只是個坊間傳說而已,對了,小子,有興趣入我萬文集舍麽?” 話題跳轉的如此之快,讓雲溪有點 ...


1. 實現功能:

  通過程式自動發送郵件。使用QQ郵箱(個人/企業)或阿裡雲郵箱(企業)。

 

2. 編碼過程中的嘗試結果:

  2.1 測試配置:見3.1 app.config配置

  2.2 測試結果

 

  

3. 代碼實現

  3.1 app.config配置

    3.1.1 QQ郵箱配置

1 <connectionStrings>    
2   <add name="MailServerIP" connectionString="smtp.exmail.qq.com" />
3     <add name="MailServerPort" connectionString="465" />
4     <add name="fromMailAddress" connectionString="[email protected]" />
5     <add name="toMailAddress" connectionString="[email protected]"/>
6     <add name="mailUsername" connectionString="usernamexxx" />
7     <add name="mailPassword" connectionString="passwordxxx" />
8     <add name="ccMailAddress" connectionString="[email protected]" />
9 </connectionStrings>
註:若是QQ個人郵箱,則mailPassword需配置成授權碼

    3.1.2 阿裡雲郵箱配置

1 <connectionStrings>  
2   <add name="MailServerIP" connectionString="smtp.qiye.aliyun.com" />
3   <add name="MailServerPort" connectionString="25" />
4   <add name="fromMailAddress" connectionString="[email protected]" />
5   <add name="toMailAddress" connectionString="[email protected]"/>
6   <add name="mailUsername" connectionString="usernamexxx" />
7   <add name="mailPassword" connectionString="passwordxxx" />
8   <add name="ccMailAddress" connectionString="[email protected]" />
9 </connectionStrings>

 

  3.2 調用

1 public void EventOccured(string EventID)
2 {
3     string subjectInfo = "一些要發送郵件的事件發生咯";
4     string bodyInfo = "<p style =\"font-size: 10pt\">Hi,all</p>";
5     bodyInfo += "<p style =\"font-size: 10pt;color:red\">事件:" + EventID + "已經發生,請註意監控。</p>";
6     bodyInfo += "<p style =\"font-size: 10pt\">以上內容為系統自動發送,請勿直接回覆,謝謝。</p>";
7 
8     SendMailHelper.Sendmail_(subjectInfo, bodyInfo);
9 }
 1 public static void Sendmail_(string subjectInfo, string bodyInfo)
 2 {
 3     try
 4     {
 5         string senderServerIp = ConfigurationManager.ConnectionStrings["MailServerIP"].ConnectionString;
 6         string toMailAddress = ConfigurationManager.ConnectionStrings["toMailAddress"].ConnectionString;
 7         string fromMailAddress = ConfigurationManager.ConnectionStrings["fromMailAddress"].ConnectionString;
 8         string mailUsername = ConfigurationManager.ConnectionStrings["mailUsername"].ConnectionString;
 9         string mailPassword = ConfigurationManager.ConnectionStrings["mailPassword"].ConnectionString;
10         string mailPort = ConfigurationManager.ConnectionStrings["MailServerPort"].ConnectionString;
11         string cc = ConfigurationManager.ConnectionStrings["ccMailAddress"].ConnectionString;
12 
13         MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, cc, true, false);  //註: aliyun需將最後參數改為:false,true
14         email.Send();
15     }
16     catch (Exception ex)
17     {
18         Console.WriteLine(ex.ToString());
19     }
20 }

 

  3.3 具體實現

  3.3.1 QQ郵箱:使用System.Web.Mail

 1 private System.Web.Mail.MailMessage mMessage;
 2 
 3 public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port,string cc, bool sslEnable, bool pwdCheckEnable)
 4 {
 5     try
 6     {
 7         //for System.Web.Mail: QQ MAIL
 8         mMessage = new System.Web.Mail.MailMessage();
 9         mMessage.Priority = System.Web.Mail.MailPriority.Normal;
10         mMessage.From = fromMail;
11         mMessage.To = toMail;
12         mMessage.Cc = cc;
13         mMessage.Subject = subject;
14         mMessage.BodyFormat = System.Web.Mail.MailFormat.Html;
15         mMessage.BodyEncoding = Encoding.UTF8;
16         mMessage.Body = emailBody;
17         mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
18         mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", fromMail); //set your username here
19         mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); //set your password here
20         mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);//set port
21         mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", sslEnable);//set is ssl
22         System.Web.Mail.SmtpMail.SmtpServer = server;
23     }
24     catch (Exception ex)
25     {
26     }
27 }
 1 public void Send()
 2 {
 3     try
 4     {
 5         //for System.Web.Mail
 6         if (mMessage != null)
 7         {
 8             System.Web.Mail.SmtpMail.Send(mMessage);
 9         }
10     }
11     catch (Exception ex)
12     {
13         Console.WriteLine(ex.ToString());
14     }
15 }

 

  3.3.2 阿裡雲郵箱:使用System.Net.Mail

 1 private MailMessage mMailMessage;    
 2 private SmtpClient mSmtpClient;    
 3 private int mSenderPort;         
 4 private string mSenderServerHost;
 5 private string mSenderPassword;
 6 private string mSenderUsername;
 7 private bool mEnableSsl;
 8 private bool mEnablePwdAuthentication;
 9 
10 public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port,string cc, bool sslEnable, bool pwdCheckEnable)
11 {
12     try
13     {
14         //for System.Net.Mail: Aliyun
15         mMailMessage = new MailMessage();
16         mMailMessage.To.Add(toMail);
17         mMailMessage.From = new MailAddress(fromMail);
18         mMailMessage.Subject = subject;
19         mMailMessage.Body = emailBody;
20         mMailMessage.IsBodyHtml = true;
21         mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
22         mMailMessage.Priority = MailPriority.Normal;
23         if (!string.IsNullOrEmpty(cc))
24             mMailMessage.CC.Add(cc);
25         this.mSenderServerHost = server;
26         this.mSenderUsername = fromMail;
27         this.mSenderPassword = password;
28         this.mSenderPort = Convert.ToInt32(port);
29         this.mEnableSsl = sslEnable;
30         this.mEnablePwdAuthentication = pwdCheckEnable;
31     }
32     catch (Exception ex)
33     {
34         Console.WriteLine(ex.ToString());
35     }
36 }
 1 public void Send()
 2 {
 3     try
 4     {
 5         //for System.Net.Mail
 6         if (mMailMessage != null)
 7         {
 8             mSmtpClient = new SmtpClient();
 9             //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
10             mSmtpClient.Host = this.mSenderServerHost;
11             mSmtpClient.Port = this.mSenderPort;
12             mSmtpClient.UseDefaultCredentials = false;
13             mSmtpClient.EnableSsl = this.mEnableSsl;
14             if (this.mEnablePwdAuthentication)
15             {
16                 System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
17                 //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
18                 //NTLM: Secure Password Authentication in Microsoft Outlook Express
19                 mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
20             }
21             else
22             {
23                 mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
24             }
25             mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
26             mSmtpClient.Send(mMailMessage);
27         }
28     }
29     catch (Exception ex)
30     {
31         Console.WriteLine(ex.ToString());
32     }
33 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前言 嗨嘍,大家好呀~這裡是愛看美女的茜茜吶 代碼提供者:青燈教育-巳月 知識點: 動態數據抓包 requests發送請求 結構化+非結構化數據解析 準備工作 下麵的儘量跟我保持一致哦~不然有可能會發生報錯 💕 開發環境: python 3.8運行代碼 pycharm 2021.2輔助敲代碼 re ...
  • 一、String類及常用方法 String :字元串,使用一對""引起來表示。 1.理解String的不可變性 (1)String實現了Serializable介面:表示字元串是支持序列化的 (2)實現了Comparable介面:可以比較大小 (3)String內部定義了final char[] v ...
  • 面向對象 在學習面向對象之前我們先來看一下麵向過程 面向過程思想 面向過程的步驟清晰簡單,第一步做什麼,第二步做什麼 面向對象過程適合處理一些簡單的問題 面向對象的過程可以用把大象放進冰箱舉例 ​ 面向對象的分析: ​ 第一步:把冰箱門打開 ​ 第二步:把大象放進冰箱 ​ 第三步:把冰箱門關上 面向 ...
  • 1、SpringCloud技術棧 開發分散式系統可能具有挑戰性,複雜性已從應用程式層轉移到網路層,並要求服務之間進行更多的交互。將代碼設為“cloud-native”就需要解決12-factor,例如外部配置,服務無狀態,日誌記錄以及連接到備份服務之類的問題,Spring Cloud項目套件包含使您 ...
  • 問題 當我使用kubeadm部署成功k8s集群時在想預設生成的證書有效期是多久,如下所示 /etc/kubernetes/pki/apiserver.crt #1年有效期 /etc/kubernetes/pki/front-proxy-ca.crt #10年有效期 /etc/kubernetes/p ...
  • 首先我們來瞭解一下什麼是CRM客戶管理系統? CRM系統包括一些核心的客戶關係管理業務功能,如:潛在客戶、客戶管理、拜訪管理、商機管理、訂單管理等模塊,滿足企業客戶關係信息化的要求,並幫助企業提高客戶資源的管理效率,能夠通過項目視圖清晰的瞭解每個項目的進展情況,通過豐富的統計報表掌握全局的項目數據。 ...
  • Java面向對象(四) 十一、package 關鍵字(拓展) 11.1 package 關鍵字的使用: 為了更好的實現項目中類的管理,提供包的概念 使用 package 聲明類或介面所屬的包,聲明在源文件的首行 包,屬於標識符,遵循標識符的命名規則、規範(xxxyyyzzz)、“見名知意” 一般包通 ...
  • 市場上輕量級ORM有很多,比如Dapper、Chloe 本篇文章就介紹一下 功能比較多並且全的ORM 1、Entity Framework(重量級) 2、SqlSugar(重量級) 3、NHibernate(重量級) 4、PetaPoco (介於EF和Dapper之間) 對比:操作的難易程度、執行效 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...