Pradeep Rao M
Pradeep Rao M

Reputation: 23

Send mail only once at a day/at a time

I am developing a code of,sending mail to the database email id's

This is the page load in aspx.cs file

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        loadEmp_Emailid();
    }
}

after in loadEmp_Emailid() function having this code in bellow

protected void loadEmp_Emailid()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("select email from emailtable", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    int size_arr = ds.Tables[0].Rows.Count;
    string[] arr = new string[size_arr];
    int i = 0;
    if (ds.Tables[0].Rows.Count > 0)
    {
        foreach (DataRow dr in ds_.Tables[0].Rows) 
        {
            arr[i] = dr["email"].ToString();
            if (dr["email"].ToString() == "")
            {

            }
            else
            {
                sendmail(arr[i]);
            }
            i++;
        }
    }
    else
    {

    }
}

The above code i am getting 10 email ids like

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
.............
..............

here is the send mail function

public  void sendmail(string emailTo)
{
    System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
    smtpClient.Host = "smtp.247headhunting.com";
    smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "xxxxx");
    System.Net.Mail.MailAddress From = new System.Net.Mail.MailAddress("[email protected]", "XXXXXX");
    System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
    Message.From = From;
    Message.To.Add(new System.Net.Mail.MailAddress(emailTo));
    Message.Subject = "Subject ";
    Message.Body = "Testing";
    Message.IsBodyHtml = true;
    try
    {
        smtpClient.Send(Message);
    }
    catch (Exception ex)
    {
    }
    finally
    {
        Message.Dispose();
    }
}

here is the problem is each page load time the mail is sending...

I don't want to send more more then one time in a day without using page load concept i want send automatically please give me the any suggestion please

Thanks pradeep

Upvotes: 0

Views: 1783

Answers (2)

yorah
yorah

Reputation: 2673

Don't execute batch jobs from your ASP.NET pages. You can instead use a specialized scheduler like the Quartz.NET enterprise scheduler (open-source).

You can have a look at the Quick start guide and the various tutorials.

Note: the basic idea is to host your quartz.net server as a windows service, alongside your asp.net website. Hosting quartz.net inside an asp.net website might be a bad idea (non-predictibility of app pool recycling may lead to unexpected behavior for instance).

Upvotes: 1

hebo
hebo

Reputation: 11

For ScheduleTask first build a simple Console-Application witch loads the Email from the database and sends the message over the smtpClient.

Than follow this.

I hope it helps.

Upvotes: 0

Related Questions