Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to send email automatically using timer control. But it is not responding. But the same code I have used under button click event, it works perfectly. Help me to find a proper solution. Thank you.


C#
namespace AlertMail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            MailMessage loginInfo = new MailMessage();
            string em = "toAddress@gmail.com";
            loginInfo.To.Add(em.ToString());
            loginInfo.From = new MailAddress("fromAddress@gmail.com");
            loginInfo.Subject = "Alert Information";

            loginInfo.Body = "Hai";
            loginInfo.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("fromAddress@gmail.com", "Password");
            smtp.Send(loginInfo);
            label6.Text = "Alert is send to your email..!!";
        }
   }
}
Posted
Comments
pradiprenushe 14-Nov-14 0:51am    
Any exception?
BillWoodruff 14-Nov-14 2:26am    
Have you started the Timer somewhere in code you have not shown here ?

Go to the form designer and click on the timer1 control and in the properties panel click on events and hook up the Tick() event to the timer1_Tick() method.
 
Share this answer
 
C#
private void Form1_Load(object sender, EventArgs e)
{
    timer1.Interval = 1000; // setting my value here
    timer1.Start();
}


Add form load/page load event inside which start the timer control.
 
Share this answer
 

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