【发布时间】:2015-10-29 16:41:04
【问题描述】:
我正在使用 C# 发送电子邮件。当互联网可用时它工作正常,但它一次又一次地返回异常消息并在互联网连接不可用时停止执行。如果连接不可用,我如何忽略发送电子邮件并继续执行, 或任何其他合适的方法来做到这一点..
while (true)
{
try
{
Thread.Sleep(50);
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mymail");
mail.To.Add("mymail");
mail.Subject = "data - 1";
mail.Body = "find attachement";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(filepath);
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception)
{
MessageBox.Show("Internet Connection is not found");
}
}
【问题讨论】: