微佩服務號推送模板新聞接口。本站提示廣大學習愛好者:(微佩服務號推送模板新聞接口)文章只能為提供參考,不一定能成為您想要的結果。以下是微佩服務號推送模板新聞接口正文
有時刻我們也許會碰到想在某一個時辰給他人發送一封郵件,就像是在誕辰的時刻,然則我們又怕到時刻忘卻了,這時候就應當
應用發送准時郵件的功效,然則這個准時發送郵件功效是怎樣完成的呢?上面用兩種方法完成.net准時發送郵件代碼,詳細請看上面內容。
完成思緒、需求添加一個全局運用法式類Global.asax
代碼會在拜訪網站時運轉
Global.asax代碼:
void Application_Start(object sender, EventArgs e)
{
// 在運用法式啟動時運轉的代碼
System.Timers.Timer timer = new System.Timers.Timer();//設計時光距離,假如一個小時履行一次就改成
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();//獲得回寫的字撙節
_HttpWebResponse.Close();
}
void Application_Error(object sender, EventArgs e)
{
// 在湧現未處置的毛病時運轉的代碼
}
void Session_Start(object sender, EventArgs e)
{
// 在新會話啟動時運轉的代碼
}
void Session_End(object sender, EventArgs e)
{
// 在會話停止時運轉的代碼。
// 留意: 只要在 Web.config 文件中的 sessionstate 形式設置為
// InProc 時,才會激發 Session_End 事宜。假如會話形式設置為 StateServer
// 或 SQLServer,則不激發該事宜。
}
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;
}
第二種方法:
准時發郵件可以用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);//設計時光距離,假如一個小時履行一次就改成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); //收件人郵箱地址可所以多個以完成群發
message.Subject = MessageSubject;
message.Body = MessageBody;
message.IsBodyHtml = true; //能否為html格局
message.Priority = MailPriority.High; //發送郵件的優先品級
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.sina.com"; //指定發送郵件的辦事器地址或IP
sc.Port = 25; //指定發送郵件端口
//sc.UseDefaultCredentials = true;
//sc.EnableSsl = true;
sc.Credentials = new System.Net.NetworkCredential(“**@**”, "暗碼"); //指定登錄辦事器的用戶名和暗碼
sc.Send(message); //發送郵件
}
到此全體代碼就寫完了。
創立一個掌握台法式,生成一個exe 采取windows的籌劃義務法式指定天天的某個時光點發送思緒就是這個思緒比辦事簡略

以上采取了兩種方法分離完成了采取.NET技巧完成郵件准時發送功效,須要的同伙可以參考下。