首先要在Global.asax写个定时方法
protected void Application_Start()
{
//在应用程序启动时运行的代码
//定义定时器
System.Timers.Timer myTimer = new System.Timers.Timer(180000);//3分钟
// myTimer_Elapsed定时方法
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
myTimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
然后在定时方法里面写事件
void myTimer_Elapsed(object source, ElapsedEventArgs e)
{
try
{
XHSHEEntities db = new XHSHEEntities();
var list = new SEmailServices().LoadEntities(a => a.state == 0).OrderBy(a => a.Cretatime);
int times = (int)new EmailConcentServices().ViewEntity(a => a.Id == 1).timer;
var title = new EmailConcentServices().ViewEntity(a => a.Id == 1).Title;
var concent = new EmailConcentServices().ViewEntity(a => a.Id == 1).Concent;
if (list.Count() != 0)
{
foreach (var item in list)
{
DateTime dt1 = (DateTime)item.Cretatime;
DateTime dt3 = dt1.AddMinutes(times);
DateTime dt2 = DateTime.Now;
if (dt2 > dt3)
{
var flist = new SendMailServices().LoadEntities().OrderBy(a => a.FCount).FirstOrDefault();
var faemail = flist.FaMailAddress;
var fapwd = flist.pwd;
var smtp = flist.SMTP;
var fcount = flist.FCount;
string str = "23456789qwertyuipasddfghjklzxcvbnm";
Random r = new Random();
string ecrode = "";
for (int i = 0; i < 4; i++)
{
var idx = r.Next(0, str.Length);
ecrode += str.ToArray()[idx];
}
var filepath = new FilesServices().ViewEntity(a => a.Id == item.FileId).FilePath;
TiEmail ti = new TiEmail();
ti.Createime = DateTime.Now.AddHours(24);
ti.FilePath = filepath;
ti.ercode = ecrode;
new TiEmailServices().AddEntity(ti);
var concents = concent + "【您的提取码是" + ecrode + "】<a href ='http://118.126.18.31:8022/BBS/TQ'>【请点击下载全文.......】</a>";
//发送邮件 其他根据需求写
ps:参数,Semailadress:收件人,faemail:发件人,fapwd:发件人密码,smtp:邮箱服务,faemail:邮件名称,title:邮件标题,concents:邮件内容
SendMail(item.Semailadress, faemail, fapwd, smtp, faemail, title, concents);
new SEmailServices().Update(a => a.Id == item.Id, b => new SEmail
{
state = 1,
});
new SendMailServices().Update(a => a.Id == flist.Id, b => new SendMail
{
FCount = flist.FCount + 1,
});
int? uploadis = new FilesServices().ViewEntity(a => a.Id == item.FileId).IsDownload;
new FilesServices().Update(a => a.Id == item.FileId, b => new Files()
{
IsDownload = uploadis + 1,
});
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
//发邮件的方法
ps:我写的是可以切换不同的发件人来发邮件,所以加了个判断
public void SendMail(string toEmailAddress, string faEmailAddress, string emailpwd, string smtp, string faname, string emailTitle, string emailBody)
{
try
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtp);
client.UseDefaultCredentials = false;
//不同发件人的判断
if (smtp.Contains("qq"))
{
client.EnableSsl = true;
}
else
{
client.EnableSsl = false;
}
client.Credentials = new System.Net.NetworkCredential(faEmailAddress, emailpwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress(faEmailAddress, faname, Encoding.UTF8);
//群发对象
string[] emails = toEmailAddress.Split(';');
for (int i = 0; i < emails.Length; i++)
{
message.To.Add(emails[i]);
}
//标题
message.Subject = emailTitle;
//内容
message.Body = emailBody;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}