Mentioned code is working fine .Email is sending after running the website and run the page but i want without running the website or page ,mail should sent .Please help in automation .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Threading;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
public partial class email : System.Web.UI.Page
{
DBConn db1 = new DBConn();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
SendEmailTOAllUser();
}
protected void SendEmailTOAllUser()
{
Collection<string> EmailAddresses = new Collection<string>();
SqlCommand cmd = new SqlCommand("select P.ContractNo, P.SPOC,P.SPOCEmail, I.ProjectID as 'ProjectName',day(I.InvoiceDtTo) as Datedueday,month(I.InvoiceDtTo) as monthdue,year(I.InvoiceDtTo) as yeardue,I.InvoiceDtFrom 'Invoicefromdate',I.InvoiceDtTo 'InvoiceEnddate' from tbl_PaymentTracker P inner join tbl_InvoiceTrackerAuto I on P.ProjectName = I.ProjectID order by projectid, SPOC", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
for (int i = 1; i<=ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows.Count > 0)
{
lblOwnername.Text = ds.Tables[0].Rows[0]["SPOC"].ToString();
lblcontractno.Text = ds.Tables[0].Rows[0]["ContractNo"].ToString();
lblprojectname.Text = ds.Tables[0].Rows[0]["ProjectName"].ToString();
lblfromdate.Text = ds.Tables[0].Rows[0]["Invoicefromdate"].ToString();
lbltodate.Text = ds.Tables[0].Rows[0]["InvoiceEnddate"].ToString();
lbldatedueday.Text= ds.Tables[0].Rows[0]["Datedueday"].ToString();
lbldateduemonth.Text = ds.Tables[0].Rows[0]["monthdue"].ToString();
lbldatedueyear.Text = ds.Tables[0].Rows[0]["yeardue"].ToString();
lbltodayday.Text =Convert.ToString(DateTime.Today.Day);
lbltodaymonth.Text = Convert.ToString(DateTime.Today.Month);
lbltodayyear.Text = Convert.ToString(DateTime.Today.Year);
if ((Convert.ToInt16(lbldatedueday.Text) - Convert.ToInt16(lbltodayday.Text)) <= 7)
{
if(DateTime.Today <= Convert.ToDateTime(lbltodate.Text))
{
string txtMessageBody = "Hi <b>'" + lblOwnername.Text + "' </b>,<br/> Your invoice<b> '" + lblprojectname.Text + "'</b> will be sent by vendor.Plase update status on invoice tracker on time.<br/><br/><b>Contract No:</b> '" + lblcontractno.Text + "',<br/><b>Contract Start Date:</b> '" + lblfromdate.Text + "',<br/><b>Contract End Date:</b> '" + lbltodate.Text + "',<br/><b>Contract Due Date:</b> '" + lbltodate.Text + "'";
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string email = (string)reader["SPOCEmail"];
EmailAddresses.Add(email);
}
}
reader.Close();
cmd.Dispose();
con.Close();
foreach (string email in EmailAddresses)
{
SendEmail(email, "[email protected]", "Invoice Tracker Reminder Testing", txtMessageBody, true);
}
}
else
{
string txtMessageBody = "Hi <b>'" + lblOwnername.Text + "' </b>,<br/> Your invoice<b> '" + lblprojectname.Text + "'</b> is pending to upload on invoice tracker.<br/><br/><b>Contract No:</b> '" + lblcontractno.Text + "',<br/><b>Contract Start Date:</b> '" + lblfromdate.Text + "',<br/><b>Contract End Date:</b> '" + lbltodate.Text + "',<br/><b>Contract Due Date:</b> '" + lbltodate.Text + "'";
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string email = (string)reader["SPOCEmail"];
EmailAddresses.Add(email);
}
}
reader.Close();
cmd.Dispose();
con.Close();
foreach (string email in EmailAddresses)
{
SendEmail(email, "[email protected]", "Invoice Tracker Reminder Testing", txtMessageBody, true);
}
}
}
}
}
}
protected static void SendEmail(string toAddress, string fromAddress, string MailSubject, string Messagebody, bool isBodyHtml)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromAddress, "Invoice");
mail.To.Add(toAddress);
mail.Subject = MailSubject;
mail.Body = Messagebody;
mail.IsBodyHtml = isBodyHtml;
//send the message
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("[email protected]", "password");
smtp.Port = 587;
smtp.Send(mail);
}
catch (System.Exception ex)
{
}
}