9
Answers

Send Email to admin at specific time everyday

Carol Cashman

Carol Cashman

7y
285
1
hey guys,
 
Does anyone know how i would code this:
 
to send an email to the admin/manager everyday at a specific time including a table from the database (Stock Table - which only has four pieces of stock) so they will be informed of their stock levels and if they need to reorder.
 
 
Or else 
 
Something in which will send an email to the manager/ admin if one of these four stock levels are low informing them a certin materials/ piece of stock has reached the minimum level and to re order. 
 
I have the code for sending an email: 
protected void Button1_Click1(object sender, EventArgs e)
{
MailMessage mail = new MailMessage(from.Text, to.Text, subject.Text, body.Text);
SmtpClient Client = new SmtpClient(smtp.Text);
Client.Port = 587;
Client.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);
Client.EnableSsl = true;
Client.Send(mail);
Response.Write("<script LANGUAGE='JavaScript' >alert('Mail Sent')</script>");
}
 
any help is welcomed!!! 
 
Thank you! 
 
Answers (9)
0
Carol Cashman

Carol Cashman

NA 49 1.8k 7y
@Nikunj 
 
Thank you very much!! one last question, how would i use the for loop checking grid side and that what i would be more familar! 
 
thanks!!! 
0
Nikunj Satasiya

Nikunj Satasiya

NA 1.5k 144.6k 7y
There is 2 way u can check it grid side using forloop if u r cell of grid contain 200 then send mail.
 
or database side u can use like this
 and do something like this ... and return value from db and based on return value send email.
  1. DECLARE @site_value INT;  
  2. SET @site_value = 0;  
  3.   
  4. WHILE @site_value = 200  
  5. BEGIN  
  6.    PRINT 'Send Email';  
  7.    SET @site_value = @site_value + 1;  
  8. END;  
  9.   
  10. PRINT 'Done WHILE LOOP';  
  11. GO  
 
0
Carol Cashman

Carol Cashman

NA 49 1.8k 7y
@Nikunj 
 
I have the email part working just usnure how to use a  'for loop' through a database to see if 200 is any value in the table. 
 
 
0
Nikunj Satasiya

Nikunj Satasiya

NA 1.5k 144.6k 7y
Ok If you want to send email if one of the below materials goes under 200.
then Simply, you can check every record using any looping such as foreach and etc within this your table if you found then call created function of mail else do nothing .
 
0
Carol Cashman

Carol Cashman

NA 49 1.8k 7y
@Nikunj
 
yes if one of the below materials goes under 200 
 
 
0
Nikunj Satasiya

Nikunj Satasiya

NA 1.5k 144.6k 7y
Hello Carol, 
can u know specific event ?? i meam when you want to send email ??? 
 
Actually as per u r requirement u should check this evrytime user login and when page load first time simply after login in page load  event you can check wather mail is send today for this user ? if no then call this function else do nothing.
 
For this you can add one additional col in database with name "IsSend" with datatype BIT.
  
0
Srikant Maruwada

Srikant Maruwada

NA 501 4.3k 7y
Hi Carol,
 
To send email at specific time.  We have many options which runs in background and send email.
 
1. Create Window Service 
 
http://www.c-sharpcorner.com/UploadFile/ee01e6/create-windows-service-and-send-mail-daily-on-fixed-time-usi575/ 
 
2. Create SQL Job which  
 
https://www.howtogeek.com/howto/database/sending-automated-job-email-notifications-in-sql-server-with-smtp/ 
 
 
 
0
Carol Cashman

Carol Cashman

NA 49 1.8k 7y
@Nikunj 
 
where does the c# code go? on page load on the Stock web form? 
 
thank you!! 
0
Nikunj Satasiya

Nikunj Satasiya

NA 1.5k 144.6k 7y
Hi Carol, 
You can try this way. in my case i send mail on specific date such as birthdate. you can modifay this code and can be use as per u r requirement .
 
i hope this is may helpful to u.
 
App.Config
  1. <appSettings>    
  2.     <add key ="Mode" value ="Daily" />    
  3.     <add key ="IntervalMinutes" value ="1" />    
  4.     <add key ="ScheduledTime" value ="17:43" />    
  5. </appSettings>    
  6. <connectionStrings>    
  7.     <add name="constr" connectionString="Data Source=.\SQL2008R2;Initial Catalog=StudentsDB;integrated security=true" />    
  8. </connectionStrings>   
C# Code :
  1. try  
  2.     {  
  3.         DataTable dt = new DataTable();  
  4.         string query = "SELECT Name, Email FROM Students WHERE DATEPART(DAY, BirthDate) = @Day AND DATEPART(MONTH, BirthDate) = @Month";  
  5.         string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
  6.         using (SqlConnection con = new SqlConnection(constr))  
  7.         {  
  8.             using (SqlCommand cmd = new SqlCommand(query))  
  9.             {  
  10.                 cmd.Connection = con;  
  11.                 cmd.Parameters.AddWithValue("@Day", DateTime.Today.Day);  
  12.                 cmd.Parameters.AddWithValue("@Month", DateTime.Today.Month);  
  13.                 using (SqlDataAdapter sda = new SqlDataAdapter(cmd))  
  14.                 {  
  15.                     sda.Fill(dt);  
  16.                 }  
  17.             }  
  18.         }  
  19.         foreach(DataRow row in dt.Rows)  
  20.         {  
  21.             string name = row["Name"].ToString();  
  22.             string email = row["Email"].ToString();  
  23.             WriteToFile("Trying to send email to: " + name + " " + email);  
  24.    
  25.             using (MailMessage mm = new MailMessage("sender@gmail.com", email))  
  26.             {  
  27.                 mm.Subject = "Birthday Greetings";  
  28.                 mm.Body = string.Format("Happy Birthday {0}Many happy returns of the day.", name);  
  29.    
  30.                 mm.IsBodyHtml = true;  
  31.                 SmtpClient smtp = new SmtpClient();  
  32.                 smtp.Host = "smtp.gmail.com";  
  33.                 smtp.EnableSsl = true;  
  34.                 System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();  
  35.                 credentials.UserName = "sender@gmail.com";  
  36.                 credentials.Password = "";  
  37.                 smtp.UseDefaultCredentials = true;  
  38.                 smtp.Credentials = credentials;  
  39.                 smtp.Port = 587;  
  40.                 smtp.Send(mm);  
  41.                 WriteToFile("Email sent successfully to: " + name + " " + email);  
  42.             }  
  43.         }  
  44.         this.ScheduleService();  
  45.     }  
  46.     catch (Exception ex)  
  47.     {  
  48.         WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);  
  49.    
  50.         //Stop the Windows Service.  
  51.         using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))  
  52.         {  
  53.             serviceController.Stop();  
  54.         }  
  55.     }