如何自动发送电子邮件 [英] How to send email automatically

查看:104
本文介绍了如何自动发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用计时器控制自动发送电子邮件。但它没有回应。但是我在按钮点击事件下使用的代码相同,它完美地运行。帮我找一个合适的解决方案。谢谢。





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.


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..!!";
        }
   }
}

推荐答案

转到表单设计器并单击 timer1 control并在属性面板中单击事件并将 Tick()事件挂钩到 timer1_Tick() method。
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.


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.


这篇关于如何自动发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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