C# 郵件發(fā)送和接收實(shí)現(xiàn)代碼
更新時(shí)間:2009年07月03日 15:28:14 作者:
這兩個(gè)方法很容易理解,只實(shí)現(xiàn)了最基本的功能,如果需要可以查看源代碼獲取更多信息。
郵件發(fā)送
方法一:使用System.Web.Mail命名空間(此方法我測(cè)試沒有成功過)
#region 發(fā)送郵件:此方法失敗
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命名空間(此方法測(cè)試成功)
我使用的gmail的郵箱,以及他提供免費(fèi)smtp服務(wù),之前試了好幾個(gè)郵箱都不成功。Gmail的smtp服務(wù)必須經(jīng)過ssl加密,才可以驗(yàn)證成功。
#region 發(fā)送郵件:此方法可行
protected void SendSuccess()
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("test@gmail.com", "someone");//必須是提供smtp服務(wù)的郵件服務(wù)器
message.To.Add(new MailAddress("test@yahoo.com.cn"));
message.Subject = "測(cè)試郵件" ;
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 = "郵件發(fā)送測(cè)試";
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"); //這里是申請(qǐng)的郵箱和密碼
client.EnableSsl = true; //必須經(jīng)過ssl加密
try
{
client.Send(message);
Response.Write("郵件已經(jīng)成功發(fā)送到" + message.To.ToString() + "<br>");
}
catch (Exception ee)
{
Response.Write(ee.Message + "<br>" /* + ee.InnerException.Message*/ );
}
}
#endregion
郵件接收
我使用的是LumiSoft.Net這個(gè)開源的項(xiàng)目,也是從一個(gè)網(wǎng)友哪里看到的下載地址,然后自己看了下代碼,寫了個(gè)簡單的接收方法。首先將代碼中relrease目錄下的dll文件引用到項(xiàng)目中。
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(" 發(fā)送給 ");
sb.Append(mail.To.ToString()).Append("<br/>") ;
sb.Append(mail.Subject).Append("<br/>");
sb.Append(mail.BodyHtmlText).Append("<hr/>");
Response.Write(sb.ToString());
}
}
這兩個(gè)方法很容易理解,只實(shí)現(xiàn)了最基本的功能,如果需要可以查看源代碼獲取更多信息。
方法一:使用System.Web.Mail命名空間(此方法我測(cè)試沒有成功過)
復(fù)制代碼 代碼如下:
#region 發(fā)送郵件:此方法失敗
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命名空間(此方法測(cè)試成功)
我使用的gmail的郵箱,以及他提供免費(fèi)smtp服務(wù),之前試了好幾個(gè)郵箱都不成功。Gmail的smtp服務(wù)必須經(jīng)過ssl加密,才可以驗(yàn)證成功。
復(fù)制代碼 代碼如下:
#region 發(fā)送郵件:此方法可行
protected void SendSuccess()
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("test@gmail.com", "someone");//必須是提供smtp服務(wù)的郵件服務(wù)器
message.To.Add(new MailAddress("test@yahoo.com.cn"));
message.Subject = "測(cè)試郵件" ;
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 = "郵件發(fā)送測(cè)試";
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"); //這里是申請(qǐng)的郵箱和密碼
client.EnableSsl = true; //必須經(jīng)過ssl加密
try
{
client.Send(message);
Response.Write("郵件已經(jīng)成功發(fā)送到" + message.To.ToString() + "<br>");
}
catch (Exception ee)
{
Response.Write(ee.Message + "<br>" /* + ee.InnerException.Message*/ );
}
}
#endregion
郵件接收
我使用的是LumiSoft.Net這個(gè)開源的項(xiàng)目,也是從一個(gè)網(wǎng)友哪里看到的下載地址,然后自己看了下代碼,寫了個(gè)簡單的接收方法。首先將代碼中relrease目錄下的dll文件引用到項(xiàng)目中。
復(fù)制代碼 代碼如下:
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(" 發(fā)送給 ");
sb.Append(mail.To.ToString()).Append("<br/>") ;
sb.Append(mail.Subject).Append("<br/>");
sb.Append(mail.BodyHtmlText).Append("<hr/>");
Response.Write(sb.ToString());
}
}
這兩個(gè)方法很容易理解,只實(shí)現(xiàn)了最基本的功能,如果需要可以查看源代碼獲取更多信息。
相關(guān)文章
c#中WinForm使用OpencvSharp4實(shí)現(xiàn)簡易抓邊
本文主要介紹了c#中WinForm使用OpencvSharp4實(shí)現(xiàn)簡易抓邊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C#中DataTable的創(chuàng)建與遍歷實(shí)現(xiàn)
這篇文章主要介紹了C#中DataTable的創(chuàng)建與遍歷實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Unity調(diào)用C++?dll實(shí)現(xiàn)打開雙目相機(jī)
這篇文章主要為大家詳細(xì)介紹了如何在Unity中調(diào)用C++?dll實(shí)現(xiàn)打開雙目相機(jī)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-05-05
c#使用EPPlus封裝excel表格導(dǎo)入功能的問題
這篇文章主要介紹了c#使用EPPlus封裝excel表格導(dǎo)入功能的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Unity實(shí)現(xiàn)10天簽到系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)10天簽到系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04

