本文介绍了如何自动发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用计时器控制自动发送电子邮件。但它没有回应。但是我在按钮点击事件下使用的代码相同,它完美地运行。帮我找一个合适的解决方案。谢谢。
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 = "[email protected]";
loginInfo.To.Add(em.ToString());
loginInfo.From = new MailAddress("[email protected]");
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("[email protected]", "Password");
smtp.Send(loginInfo);
label6.Text = "Alert is send to your email..!!";
}
}
}
推荐答案
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.
这篇关于如何自动发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!