2017-05-25 96 views
0

我正在尝试在Windows任务计划程序中使用ASP.NET。我想在特定时间发送电子邮件。 但是ASP.NET并没有像EXE那样运行,它有一个动态的IP地址。 我不知道在ASP.NET中使用Window Task Scheduler。 你可以给我任何解决方案吗?如何在ASP.NET中使用Windows任务计划程序

void SendEmail() 
{ 
    //get the data from database 
    DataTable data = GetData(); 
    DataTable email_data = GetEmailData(); 

    //set DataTable Name of Excel Sheet 
    data.TableName = "NSOList"; 

    //Create a New Workook 
    using (XLWorkbook wb = new XLWorkbook()) 
    { 
     //Add the DataTable as Excel Workhseet 
     wb.Worksheets.Add(data); 

     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      //Save the Excel Workbook to MemoryStream 
      wb.SaveAs(memoryStream); 

      //Convert MemoryStream to Byte array 
      byte[] bytes = memoryStream.ToArray(); 
      memoryStream.Close(); 

      //body with embedded image 
      AlternateView body = AlternateView.CreateAlternateViewFromString 
       ("Hi <br><br> <img src=cid:example>", null, "text/html"); 

      //create the LinkedResource (embedded image) 
      LinkedResource image = new LinkedResource("c:\\example.png"); 
      image.ContentId = "example"; 

      //add the LinkedResource to the appropriate view 
      body.LinkedResources.Add(image); 


      String from = "[email protected]"; 
      //bring Email data 
      for (int i = 0; i < email_data.Rows.Count; i++) 
      { 
       String to = email_data.Rows[i][0].ToString(); 


       using (MailMessage mm = new MailMessage(from, to)) 
       { 

        SmtpClient smtp = new SmtpClient(); 
        mm.Subject = "List"; 

        mm.AlternateViews.Add(body); 
        mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "NSOList.xlsx")); 
        mm.IsBodyHtml = true; 

        smtp.Host = "smtp.gmail.com"; 
        smtp.EnableSsl = true; 
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(); 
        credentials.UserName = "[email protected]"; 
        credentials.Password = "abcd"; 
        smtp.UseDefaultCredentials = true; 
        smtp.Credentials = credentials; 
        smtp.Port = 587; 
        smtp.Send(mm); 
       } 
      } 
     } 
    } 


} 
+0

如果您想在特定时间发送电子邮件,则必须在Windows任务计划程序中创建新的任务操作 – ISHIDA

回答

0

您应该使用任务计划程序类似Quartz.Net。它允许您将类定义为Jobs,然后根据时间表执行这些作业。我目前正在一些内部项目中使用它,并且按照所宣传的方式执行。

EDITED

检查answers here

相关问题