C# 郵件發送和接收實現代碼

來源:互聯網
上載者:User
郵件發送
方法一:使用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# 郵件發送和接收實現代碼的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.