C# 郵件發送和接收實現代碼
郵件發送
方法一:使用System.Web.Mail命名空#region 發送郵件:此方法失敗
protected void SendFailed() { System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); mail.From = "test@ gmail.com"; mail.To = " test@ gmail.com "; mail.Subject = "For Test"; mail.Priority = System.Web.Mail.MailPriority.Normal; mail.BodyEncoding = Encoding.Default; mail.BodyFormat = MailFormat.Html; mail.Body = "this is a Email!<input type='button' value='ok'/>"; mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "test"); //set your username here mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "****"); //set your password here mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com"); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "587"); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); SmtpMail.SmtpServer = "smtp.gmail.com"; SmtpMail.Send(mail); } #endregion间(此方法我测试没有成功过)
方法二:使用System.Net.Mail命名空間(此方法測試成功)
我使用的gmail的信箱,以及他提供免費smtp服務,之前試了好幾個郵箱都不成功。 Gmail的smtp服務必須經過ssl加密,才可以驗證成功。
#region 发送邮件:此方法可行 protected void SendSuccess() { System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.From = new MailAddress("test@gmail.com", "someone");//必须是提供smtp服务的邮件服务器 message.To.Add(new MailAddress("test@yahoo.com.cn")); message.Subject = "测试邮件" ; message.CC.Add(new MailAddress("test@126.com")); message.Bcc.Add(new MailAddress("test@126.com")); message.IsBodyHtml = true; message.BodyEncoding = System.Text.Encoding.UTF8; message.Body = "邮件发送测试"; message.Priority = System.Net.Mail.MailPriority.High; SmtpClient client = new SmtpClient("smtp.gmail.com", 587); // 587;//Gmail使用的端口 client.Credentials = new System.Net.NetworkCredential("test@gmail.com", "password"); //这里是申请的邮箱和密码 client.EnableSsl = true; //必须经过ssl加密 try { client.Send(message); Response.Write("邮件已经成功发送到" + message.To.ToString() + "<br>"); } catch (Exception ee) { Response.Write(ee.Message + "<br>" /* + ee.InnerException.Message*/ ); } } #endregion
郵件接收
我使用的是LumiSoft.Net這個開源的項目,也是從一個網友哪裡看到的下載地址,然後自己看了下代碼,寫了個簡單的接收方法。首先將程式碼中relrease目錄下的dll檔案引用到專案中。
using LumiSoft.Net.POP3.Client; using LumiSoft.Net.Mail; …… public IList<Mail_Message> ReceiveMail() { IList<Mail_Message> mailList = new List<Mail_Message>(); using (POP3_Client client = new POP3_Client()) { client.Connect("pop.gmail.com",995,true); client.Authenticate("zw.seaman", "zw_seaman", false); POP3_ClientMessageCollection coll = client.Messages; for (int i = 0; i < coll.Count; i++) { POP3_ClientMessage message = coll[i]; Mail_Message mm = Mail_Message.ParseFromByte(coll[i].MessageToByte()); mailList.Add(mm); } } return mailList; } protected void Page_Load(object sender, EventArgs e) { IList<Mail_Message> mailList = new ZMail.Mail().ReceiveMail(); foreach (Mail_Message mail in mailList) { StringBuilder sb = new StringBuilder(); sb.Append(mail.From.ToString()).Append(" 发送给 "); sb.Append(mail.To.ToString()).Append("<br/>") ; sb.Append(mail.Subject).Append("<br/>"); sb.Append(mail.BodyHtmlText).Append("<hr/>"); Response.Write(sb.ToString()); } }
以上是C# 郵件發送和接收實現代碼的內容,更多相關內容請關注PHP中文網(www.php.cn)!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

可以採用多種方法修改 XML 格式:使用文本編輯器(如 Notepad )進行手工編輯;使用在線或桌面 XML 格式化工具(如 XMLbeautifier)進行自動格式化;使用 XML 轉換工具(如 XSLT)定義轉換規則;或者使用編程語言(如 Python)進行解析和操作。修改時需謹慎,並備份原始文件。
