使用asp.net c#在指定日期前3天发送电子邮件 [英] To send Email before 3 days from the Specified date using asp.net c#

查看:116
本文介绍了使用asp.net c#在指定日期前3天发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我必须向客户发送电子邮件3天才能提醒他们的旅行时间。我正在使用ASP.NET 2.0,C#.net和SQL Server 2005.我正在使用此代码..



protected void Submit_Click(object sender, EventArgs e)

{

SmtpClient smtpClientUser = new SmtpClient();

MailMessage messageUser = new MailMessage();

messageUser.To.Add(txtemail.Text);

MailAddress fromAddressUser = new MailAddress(sumalatha69@gmail.com);

messageUser.From = fromAddressUser;

messageUser.Subject =欢迎来到HopAroundIndia;

messageUser.IsBodyHtml = true;

messageUser.Body =亲爱的+ txttriptitle。文字+,

+ txtdestination.Text +,+ txtdescription.Text +,+ txtdate.Text +,+ txtemail.Text;



smtpClientUser.Host =ASPMX.L.GOOGLE.COM;

smtpClientUser.UseDefaultCredentials = true;

smtpClient.DeliveryMet hod = SmtpDeliveryMethod.Network;

smtpClientUser.Port = 25;

smtpClientUser.Send(messageUser);

SqlConnection con = new SqlConnection(str );

SqlCommand com = new SqlCommand(Hai_insertnewtripplandetailes,con);

con.Open();

com.CommandType = CommandType .StoredProcedure;

com.Parameters.Add(@ userId,Request.Cookies [userId]。Value.ToString());

if(Request。 QueryString [TripId]!= null)

{

com.Parameters.Add(@ Id,Request.QueryString [TripId]。ToString() );

}

com.Parameters.Add(@ TripTitle,txttriptitle.Text);

com.Parameters.Add( @Destination,txtdestination.Text);

com.Parameters.Add(@ Description,txtdescription.Text);

com.Parameters.Add(@ TripStartDat e,Convert.ToDateTime(txtdate.Text));

com.Parameters.Add(@ Remindme,CheckBox1.Checked);

com.Parameters.Add (@ Emailid,txtemail.Text);

com.ExecuteNonQuery();

con.Close();

Label1.Text =您的旅行计划成功创建;

ClearControls(页面);

//}

}



你可以帮我吗。

i使用sql server 2005

Hi,

3 days before I have to send email to my customers to remind their travelling timings. in that i am using ASP.NET 2.0, C#.net, and SQL server 2005.and i am using this code..

protected void Submit_Click(object sender, EventArgs e)
{
SmtpClient smtpClientUser = new SmtpClient();
MailMessage messageUser = new MailMessage();
messageUser.To.Add(txtemail.Text);
MailAddress fromAddressUser = new MailAddress("sumalatha69@gmail.com");
messageUser.From = fromAddressUser;
messageUser.Subject = "Welcome to HopAroundIndia";
messageUser.IsBodyHtml = true;
messageUser.Body = "Dear " + txttriptitle.Text + ", "
+ txtdestination.Text +"," + txtdescription.Text +"," +txtdate.Text + "," +txtemail.Text;

smtpClientUser.Host = "ASPMX.L.GOOGLE.COM";
smtpClientUser.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClientUser.Port = 25;
smtpClientUser.Send(messageUser);
SqlConnection con = new SqlConnection(str);
SqlCommand com = new SqlCommand("Hai_insertnewtripplandetailes", con);
con.Open();
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@userId", Request.Cookies["userId"].Value.ToString());
if (Request.QueryString["TripId"] != null)
{
com.Parameters.Add("@Id", Request.QueryString["TripId"].ToString());
}
com.Parameters.Add("@TripTitle", txttriptitle.Text);
com.Parameters.Add("@Destination", txtdestination.Text);
com.Parameters.Add("@Description", txtdescription.Text);
com.Parameters.Add("@TripStartDate", Convert.ToDateTime(txtdate.Text));
com.Parameters.Add("@Remindme", CheckBox1.Checked);
com.Parameters.Add("@Emailid" ,txtemail.Text);
com.ExecuteNonQuery();
con.Close();
Label1.Text = "Your Trip Plan Created successfully";
ClearControls(Page);
//}
}

can u please help me.
i am used sql server 2005

推荐答案

使用DATEDIFF(Transact-SQL) [ ^ ]查找截至今天的客户旅行日期的剩余数量,并检索旅行客户的电子邮件地址date是从今天开始的3天。

所以你的查询将在where子句


Use DATEDIFF (Transact-SQL)[^] to find the number of remaining for customer''s travel date as of today and retrieve the email addresses for customers whose travel date is 3 days from today.
So your query will have something like this in the where clause

WHERE DATEDIFF(DAY, GETDATE(), TravelDate) = 3



这里TravelDate是客户的旅程日期。


Here TravelDate is customer''s date of journey.


创建Windows服务

http: //www.aspdotnet-suresh.com/2011/06/creating-windows-service-in-c-or.html [ ^ ]



发送电子邮件程序

http://stackoverflow.com/ question / 449887 / sending-e-mail-using-c-sharp [ ^ ]



http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8 [< a href =http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8\"target =_ blanktitle =New Window> ^ ]



在创建的窗口服务的主类中包含此方法..

private void ScheduleEmail()

{

//从数据库中获取邮件信息然后

//查看日期(例如:21/12/2012)

DateTime dtYourDate = Convert.ToDateTime(21/12/2012);

if(DateTime.Now.AddDays(-3).ToShortDateString()== dtYourDate.ToShortDateString())

{

//在这里调用SendMailMethod;

}

}



在OnElapsedTime事件上调用此方法

Create windows service
http://www.aspdotnet-suresh.com/2011/06/creating-windows-service-in-c-or.html[^]

Sending Email procedure
http://stackoverflow.com/questions/449887/sending-e-mail-using-c-sharp[^]

http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8[^]

include this method in main class in the created window service..
private void ScheduleEmail()
{
//Get the Mailing Information from database and then
//Check the Date (ex: "21/12/2012")
DateTime dtYourDate = Convert.ToDateTime("21/12/2012");
if (DateTime.Now.AddDays(-3).ToShortDateString() == dtYourDate.ToShortDateString())
{
//Call SendMailMethod here;
}
}

Call this method on OnElapsedTime event
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
  TraceService("Another entry at "+DateTime.Now);
  ScheduleEmail();
}


检查此链接以创建Windows服务

http://www.c-sharpcorner.com/ UploadFile / mahesh / window_service11262005045007AM / window_service.aspx



//连接sql的示例

string ConnStrng =Data Source = localhost \\\ \\ sqlexpress; Initial Catalog = MembersAccount; Integrated Security = SSPI;;

string updateRec =INSERT INTO TblMembers(FullName,Iam,Email,Password,BirthMonth,BirthDay,BirthYear)VALUES(@FullName, @Iam,@ Email,@ Password,@ BirthMonth,@ BirthDay,@ BirthYear);



SqlConnection SqlConn = new SqlConnection(ConnStrng);



SqlCommand sqlCmd = new SqlCommand(updateRec,SqlConn);



sqlCmd.Parameters.AddWithValue(@ FullName,txtfullname .Text);

sqlCmd.Parameters.AddWithValue(@ Iam,txtiam.Text);

sqlCmd.Parameters.AddWithValue(@ Email,txtemail.Text );

sqlCmd.Parameters.AddWithValue(@ P assword,txtpassword.Text);

sqlCmd.Parameters.AddWithValue(@ BirthMonth,ddlmonth.Text);

sqlCmd.Parameters.AddWithValue(@ BirthDay ,ddlday.Text);

sqlCmd.Parameters.AddWithValue(@ BirthYear,txtyear.Text);





试试

{

SqlConn.Open();

sqlCmd.ExecuteNonQuery();

lblSecMessage.Text =感谢您注册。;

}

catch(例外情况)

{

lblSecMessage.Text = ex.Message;

}

终于

{

SqlConn.Close();

}
Check this link to create windows service
http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx

// Sample for connection sql
string ConnStrng = "Data Source=localhost\\sqlexpress;Initial Catalog=MembersAccount; Integrated Security=SSPI;";
string updateRec = "INSERT INTO TblMembers(FullName, Iam, Email, Password, BirthMonth, BirthDay, BirthYear) VALUES (@FullName, @Iam, @Email, @Password, @BirthMonth, @BirthDay, @BirthYear)";

SqlConnection SqlConn = new SqlConnection(ConnStrng);

SqlCommand sqlCmd = new SqlCommand(updateRec,SqlConn);

sqlCmd.Parameters.AddWithValue("@FullName", txtfullname.Text);
sqlCmd.Parameters.AddWithValue("@Iam", txtiam.Text);
sqlCmd.Parameters.AddWithValue("@Email", txtemail.Text);
sqlCmd.Parameters.AddWithValue("@Password", txtpassword.Text);
sqlCmd.Parameters.AddWithValue("@BirthMonth", ddlmonth.Text);
sqlCmd.Parameters.AddWithValue("@BirthDay", ddlday.Text);
sqlCmd.Parameters.AddWithValue("@BirthYear", txtyear.Text);


try
{
SqlConn.Open();
sqlCmd.ExecuteNonQuery();
lblSecMessage.Text = "Thank you for registering.";
}
catch (Exception ex)
{
lblSecMessage.Text = ex.Message;
}
finally
{
SqlConn.Close();
}


这篇关于使用asp.net c#在指定日期前3天发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆