.NET實現(xiàn)定時發(fā)送郵件代碼(兩種方式)
有時候我們或許會遇到想在某一個時刻給別人發(fā)送一封郵件,就像是在生日的時候,但是我們又怕到時候忘記了,這時就應該
使用發(fā)送定時郵件的功能,但是這個定時發(fā)送郵件功能是怎么實現(xiàn)的呢?下面用兩種方式實現(xiàn).net定時發(fā)送郵件代碼,具體請看下面內容。
實現(xiàn)思路、需求添加一個全局應用程序類Global.asax
代碼會在訪問網站時運行
Global.asax代碼:
void Application_Start(object sender, EventArgs e)
{
// 在應用程序啟動時運行的代碼
System.Timers.Timer timer = new System.Timers.Timer();//設計時間間隔,如果一個小時執(zhí)行一次就改為
timer.Elapsed += new System.Timers.ElapsedEventHandler(Send);
timer.AutoReset = true;
timer.Enabled = true;
}
void Application_End(object sender, EventArgs e)
{
// 在應用程序關閉時運行的代碼
System.Threading.Thread.Sleep();
string strUrl = "服務器地址";
System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
System.Net.HttpWebResponse _HttpWebResponse = (System.Net.HttpWebResponse)_HttpWebRequest.GetResponse();
System.IO.Stream _Stream = _HttpWebResponse.GetResponseStream();//得到回寫的字節(jié)流
_HttpWebResponse.Close();
}
void Application_Error(object sender, EventArgs e)
{
// 在出現(xiàn)未處理的錯誤時運行的代碼
}
void Session_Start(object sender, EventArgs e)
{
// 在新會話啟動時運行的代碼
}
void Session_End(object sender, EventArgs e)
{
// 在會話結束時運行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設置為
// InProc 時,才會引發(fā) Session_End 事件。如果會話模式設置為 StateServer
// 或 SQLServer,則不引發(fā)該事件。
}
private void Send(object sender, System.Timers.ElapsedEventArgs e)
{
switch (DateTime.Now.Hour)
{
case :
case :
SendEMail();
break;
//default:
// SendEMail();
// break;
}
}
private void SendEMail()
{
string mailFrom = System.Configuration.ConfigurationManager.AppSettings["MailFrom"].ToString();
string mailUser = System.Configuration.ConfigurationManager.AppSettings["MailUser"].ToString();
string mailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"].ToString();
string hostIP = System.Configuration.ConfigurationManager.AppSettings["MailHost"].ToString();
List<string> mailAddress = new List<string>();
string mailSubjct = "郵件主題";
string mailBody = "郵件內容:";
mailAddress.Add("郵件地址");string strReturn = sendMail(mailSubjct, mailBody, mailFrom, mailAddress, hostIP, mailUser, mailPassword, false);
}
sendMail方法
public static string sendMail(string mailSubjct, string mailBody, string mailFrom, List<string> mailAddress, string hostIP, string username, string password, bool ssl)
{
string str = "";
try
{
MailMessage message = new MailMessage
{
IsBodyHtml = true,
Subject = mailSubjct,
Body = mailBody,
From = new MailAddress(mailFrom)
};
for (int i = ; i < mailAddress.Count; i++)
{
message.To.Add(mailAddress[i]);
}
SmtpClient client = new SmtpClient
{
EnableSsl = ssl,
UseDefaultCredentials = false
};
NetworkCredential credential = new NetworkCredential(username, password);
client.Credentials = credential;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = hostIP;
client.Port = x;
client.Send(message);
}
catch (Exception exception)
{
str = exception.Message;
}
return str;
}
第二種方式:
定時發(fā)郵件可以用Timer來設置時間,放在了Global.asax的Application_Start里面
using System.Net.Mail;
using System.Timers;
protected void Application_Start(object sender, EventArgs e)
{
Timer t = new Timer(60000);//設計時間間隔,如果一個小時執(zhí)行一次就改為3600000 ,這里一分鐘調用一次
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
t.AutoReset = true;
t.Enabled = true;
}
private void t_Elapsed(object sender, ElapsedEventArgs e)
{
MailMessage message = new MailMessage();
message.From = Messagefrom;
message.To.Add(MessageTo); //收件人郵箱地址可以是多個以實現(xiàn)群發(fā)
message.Subject = MessageSubject;
message.Body = MessageBody;
message.IsBodyHtml = true; //是否為html格式
message.Priority = MailPriority.High; //發(fā)送郵件的優(yōu)先等級
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.sina.com"; //指定發(fā)送郵件的服務器地址或IP
sc.Port = 25; //指定發(fā)送郵件端口
//sc.UseDefaultCredentials = true;
//sc.EnableSsl = true;
sc.Credentials = new System.Net.NetworkCredential(“**@**”, "密碼"); //指定登錄服務器的用戶名和密碼
sc.Send(message); //發(fā)送郵件
}
到此全部代碼就寫完了。
創(chuàng)建一個控制臺程序,生成一個exe 采用windows的計劃任務程序指定每天的某個時間點發(fā)送思路就是這個思路比服務簡單

以上采用了兩種方式分別實現(xiàn)了采用.NET技術實現(xiàn)郵件定時發(fā)送功能,需要的朋友可以參考下。
相關文章
Mongodb在CSharp里實現(xiàn)Aggregate實例
本篇文章主要介紹了Mongodb在CSharp里實現(xiàn)Aggregate實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

