asp.net基于基于windows服务实现定时发送邮件的方法服务实现定时发送邮件的方法
主要介绍了asp.net基于windows服务实现定时发送邮件的方法,结合实例形式较为详细的分析了asp.net调用Windows系统服务的相关技巧,具有一定参考借鉴价值,需要
的朋友可以参考下
本文实例讲述了asp.net基于windows服务实现定时发送邮件的方法。分享给大家供大家参考,具体如下:
//定义组件
private System.Timers.Timer time;
public int nowhour;
public int minutes;
public string sendTime;
public Thread th;
public string isOpen;//是否启用定时发送
public string strToEUser;
public static int index = -1;
public static ArrayList mulitaddress;
//服务开始
protected override void OnStart(string[] args)
{
time = new System.Timers.Timer();
time.Enabled = true;
time.AutoReset = true;
//定时执行方法
time.Elapsed+=new System.Timers.ElapsedEventHandler(time_Elapsed);
UserEmail toUser =new UserEmail();
mulitaddress = GetMailAdressName();
// LogHelper.SaveNote("当前索引的值" + index.ToString());
Thread.Sleep(2000);
time.Start();
}
public class LogHelper
{
public static void SaveNote(string note)
{
FileStream stream = new FileStream(GetLogDirectory("Common") + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append, FileAccess.Write, FileShare.Delete | FileShare.ReadWrite);
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
writer.WriteLine("================================================================");
writer.WriteLine(string.Format("Note:/t{0}", note));
writer.WriteLine(string.Format("DateTime:/t{0}/r/n/r/n",DateTime.Now.ToString()));
stream.Flush();
writer.Close();
stream.Close();
stream.Dispose();
writer.Dispose();
}
public static void SaveNote(string note, string txtname)
{
FileStream stream = new FileStream(GetLogDirectory(txtname) + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append, FileAccess.Write, FileShare.Delete | FileShare.ReadWrite);
StreamWriter writer = new StreamWriter(stream, Encoding.GetEncoding("gb2312"));
writer.WriteLine("================================================================");
writer.WriteLine(string.Format("Note:/t{0}", note));
writer.WriteLine(string.Format("DateTime:/t{0}/r/n/r/n", DateTime.Now.ToString("yyyyMMddHHmmss")));
stream.Flush();
writer.Close();
stream.Close();
stream.Dispose();
writer.Dispose();
}
public static void SaveException(Exception e)
{
SaveException(e, string.Empty);
}
public static void SaveException(Exception e, string memo)
{
FileStream stream = new FileStream(GetLogDirectory("Common") + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append, FileAccess.Write, FileShare.Delete | FileShare.ReadWrite);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("================================================================");
writer.WriteLine(string.Format("Memo:/t{0}", memo));
writer.WriteLine(string.Format("DateTime:/t{0}", DateTime.Now.ToShortTimeString()));
writer.WriteLine(string.Format("Message:/t{0}", e.Message));
writer.WriteLine(string.Format("StackTrace:/r/n----------/r/n{0}/r/n----------/r/n/r/n/r/n", e.StackTrace));
stream.Flush();
writer.Close();
stream.Close();
stream.Dispose();
writer.Dispose();
}
public static string GetLogDirectory(string category)
{
string baseDirectory = string.Empty;
if ((HttpContext.Current != null) && (HttpContext.Current.Server != null))
{
baseDirectory = HttpContext.Current.Server.MapPath("~");
}
else
{
baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
}
if ((baseDirectory[baseDirectory.Length - 1] != '/') && (baseDirectory[baseDirectory.Length - 1] != '//'))
{
baseDirectory = baseDirectory + @"/";
}
baseDirectory = string.Format(@"{0}Log/{1}/", baseDirectory, category);
if (!Directory.Exists(baseDirectory))
{
Directory.CreateDirectory(baseDirectory);
}
return baseDirectory;
}
}
void time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
if (mulitaddress != null)
{
LogHelper.SaveNote("进入time_Elapsed");
//获取定时发送时间
sendTime = Convert.ToString(GetConfigValue("sendTime"));
LogHelper.SaveNote("sendTime" + sendTime);
//是否开启定时发送功能
isOpen = GetConfigValue("isOpen");
int sleeptime = Convert.ToInt32(GetConfigValue("SleepTime"));
int stoptime = Convert.ToInt32(GetConfigValue("stoptime"));
//LogHelper.SaveNote("数组长度" + mulitaddress.Count);
// LogHelper.SaveNote("是否开启定时发送功能" + isOpen + "定时时间" + sendTime + "定时小时数:" + Convert.ToInt32(sendTime.Split(new char[] { ':' })[0]) + "定时分钟数:" + Convert.ToInt32(sendTime.Split(new char[] { ':' })[1]));
if (isOpen == "true")