Three ways to automate send mail

First, sql trigger to send

Second, use task schedule to send after winform program

page 

//Reference
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using System.Data ;
using System.Net.Mail;

//The mail to be sent
public static DataSet SendEmailList()
{
DataSet ds = new DataSet ();
try
{
string str = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(str);
conn.Open( );
SqlCommand comm = new SqlCommand("SendEmailList", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter ();
da.SelectCommand = comm;
da.Fill(ds);
conn.Close();

}
catch (Exception ex) {}
return ds;
}
public static bool SendMail(string form, string toMail, string title, string body, string SendEmailID)
{
bool result = false;

try
{
MailMessage email = new MailMessage();
email.From = new MailAddress(form);
//Recipient
email.To.Add(toMail);
//Subject
email.Subject = title;
//Content
email.Body = body;
//Priority
email.Priority = MailPriority.Normal;
//Content type
email.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Send(email);
/ /Change the status again
string str = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
SqlConnection conn = new SqlConnection(str);
conn.Open();< br />SqlCommand comm = new SqlCommand("SendEmailDelete", conn);
//Parameter SendEmailID
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter(" @SendEmailID", SqlDbType.NVarChar, 50));
comm.Parameters["@SendEmailID"].Value = SendEmailID.ToString();

comm.ExecuteNonQuery();

result = true;
}
catch (Exception ex)
{
result = false;
}

return result;
}

webconf ig page configuration

configuration program

start----program----accessories—system tools—task plan, double click to add task, select program file---- -(*.Exe generated by the program), the administrator enters the password as the login password, just keep clicking Next

3. Global

Disadvantages, global will only run when someone visits the page

webconfig page configuration

global page

void Application_Start(object sender, EventArgs e)
{
// Code that runs when the application starts
Timer t = new Timer(60000);//Design time interval, if it is executed once an hour, it will be changed to 3600000, here is called once a minute
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
t.AutoReset = true ;
t.Enabled = true;

}
private void t_Elapsed(object sender, ElapsedEventArgs e)
{
Response.Write("Successful execution ”);
//Query the database
DataSet ds = Common.SendEmailList();
string FormEmail = ConfigurationManager.AppSettings["UserEmail"];
string title = "Membership application Online registration failed";

for (int i = 0; i {
string ToEmail = ds.Tables [0].Rows[i]["EmailAddress"].ToString();
string b ody = "Your online registration failed, the reason for the error:";
body += ds.Tables[0].Rows[i]["Cause"].ToString() + "
";< br />body += "Please check the data for correction";
string SendEmailID = ds.Tables[0].Rows[i]["SendEmailID"].ToString();
Common.SendMail(FormEmail , ToEmail, title, body, SendEmailID);
}
}

winform page

//reference
using System.Data. Common;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Net.Mail;

//Email to be sent
public static DataSet SendEmailList()
{
DataSet ds = new DataSet();
try
{

string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand comm = new SqlCommand("SendEmailList ”, conn);
comm.CommandType = CommandType.StoredProcedure;
comm.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = comm;< br />da.Fill(ds);
conn.Close( );
}
catch (Exception ex) {}
return ds;
}
public static bool SendMail(string form, string toMail, string title, string body ,string SendEmailID)
{
bool result = false;

try
{
MailMessage email = new MailMessage();
email. From = new MailAddress(form);
//Recipient
email.To.Add(toMail);
//Subject
email.Subject = title;
//Content
email.Body = body;
//Priority
email.Priority = MailPriority.Normal;
//Content type
email.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Send(email);
//Change the status again
string str = ConfigurationManager.ConnectionStrings["ConnectionString" ].ConnectionString;
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand comm = new SqlCommand("SendEmailDelete", conn);
// Parameter SendEmailID
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@SendEmailID", SqlDbType.NVarChar, 50));
comm.Parame ters["@SendEmailID"].Value = SendEmailID.ToString();

comm.ExecuteNonQuery();

result = true;
}
catch (Exception ex)
{
result = false;
}

return result;
}

For more details, please follow the official website of Kewail: (www.kewail.com)

Leave a Comment

Your email address will not be published.