Click here to Skip to main content
15,886,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 17-Dec-12 23:07pm
v2

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

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

Here TravelDate is customer's date of journey.
 
Share this answer
 
v2
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
C#
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
  TraceService("Another entry at "+DateTime.Now);
  ScheduleEmail();
}
 
Share this answer
 
Comments
suma2212 20-Dec-12 2:20am    
Hi i am using ASP.net 2.0 ,C# and sql server 2005
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();
}
 
Share this answer
 
// this by using outlook


string emailBody = txtbody.Text;

//string[] split = filePath.ToString().Split('\\');
//emailBody.Replace("[Imagefile]", "");

Outlook._Application outlookApp = new Outlook.Application();

Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

mailItem.Subject = txtsub.Text;
mailItem.HTMLBody = emailBody;

Outlook.Inspector outlookInspector = mailItem.GetInspector;

mailItem.To = txtto.Text;

mailItem.CC = "chowdary.avula@gmail.com";

mailItem.DeferredDeliveryTime = Convert.ToDateTime(dateTimePicker1.Text);
//mailItem.Send();
((Outlook._MailItem)mailItem).Send();
 
Share this answer
 
v2
Comments
suma2212 20-Dec-12 5:25am    
Hi,

thanq for ur answer. i have one doubt i am using asp.net 2.0, is it possible to create webservices in asp.net 2.o?

please send code for gmail. i am using gmail not outlook
First Create a windows service by following steps

[^]

write following code in service1.cs file

C#
protected override void OnStart(string[] args)
        {
            try
            {
                int iHourIn24HourFormat = Convert.ToInt32(DateTime.Now.ToString("HH"));
                int iCurrentMinutes = DateTime.Now.Minute;
                int iCurrentSecond = DateTime.Now.Second;

                int iFirstIntervalGapInHours = 24 - iHourIn24HourFormat;
                eventLog1.WriteEntry("Service Started ---->" + DateTime.Now.ToString());
                objTimer.Interval = (1000 * 60 * 60 * iFirstIntervalGapInHours) + (1000 * 60 * 60 * 2) - (1000 * 60 * iCurrentMinutes) - (1000 * iCurrentSecond); // at Every day 2:00 AM    
                eventLog1.WriteEntry("Interval -> " + objTimer.Interval.ToString());                

                objTimer.Enabled = true;
                objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed);
                objTimer.Start();
	}
            catch (Exception ex)
            {

                eventLog1.WriteEntry("Error: " + ex.Message);
            }            
        }

void objTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                eventLog1.WriteEntry("Timer Elapsed ---->" + DateTime.Now.ToString());

                objTimer.Interval = 1000 * 60 * 60 * 24; //set interval for every 1 day

                ContinueProcess();
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("Error: " + ex.Message);
            }
        }


private void ContinueProcess()
        {
            HttpWebRequest objHttpWebRequest;
            HttpWebResponse objHttpWebResponse = null;
            try
            {
                objHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.xyz.com/email_Service.aspx");
                objHttpWebRequest.Timeout = 300000;
                objHttpWebRequest.Proxy = null;
                objHttpWebRequest.Method = "Get";
                objHttpWebRequest.ContentLength = 0;
                objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
                Stream objResponseStream = objHttpWebResponse.GetResponseStream();                
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("ContinueProcess Error: " + ex.Message);
            }
            finally
            {
                if (objHttpWebResponse != null)
                {
                    objHttpWebResponse.Close(); // Close web response Connection
                }
                objHttpWebRequest = null; //To clear up the web request
                objHttpWebResponse = null;//To clear up the web Response
            }
        }



In your email_Service.aspx page write following code in page_load event
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
	//Add code to find the list of user to whom you wants to send email.
        }
   }
 
Share this answer
 
Comments
suma2212 27-Dec-12 0:27am    
Hi i am not using asp.net 4.5, i am using asp.net 2.0
Mukund Thakker 28-Dec-12 1:33am    
This will work in asp.net 2.0. There is no new feature of 4.5
suma2212 16-Jan-13 7:33am    
how to declare eventlog1,objtimer
Mukund Thakker 16-Jan-13 8:29am    
In the Service1, you can drag EventLog from Toolbox.
and for objtimer, please include following namespace.
using System.Timers;
suma2212 16-Jan-13 23:58pm    
Hi thanks for ur help.
my toolbox i didn't get eventlog and objtimer. please explained me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900