如何忽略发送电子邮件的方法并继续执行
本文关键字:方法 继续 执行 电子邮件 何忽略 | 更新日期: 2023-09-27 18:19:32
我正在使用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");
}
}
任何依赖于重复尝试的解决方案最终都可能无休止地循环。
您的代码正在同步发送电子邮件,为什么不使用提取目录异步发送呢
这将把电子邮件放入SMTP提取目录,SMTP服务器将通过重试一段可配置的时间来处理暂时的网络问题。
当没有互联网时就跳出循环:
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");
break;
}
}
最好有一个testconnection方法,如果返回true则发送电子邮件。类似于:
while(true)
{
if(TestConnection())
{
SendEmail(); // you should leave the try...catch.. anyway in case
// the connection failed between the TestConnection
// and the SendEmail() operation. But then do not
// prompt a messagebox, just swallow
Thread.Sleep(50);
}
}
现在实现了TestConnection,您可以尝试从以下链接获得帮助:
在此处输入链接描述
试试这个:
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 ex)
{
MessageBox.Show("Error " + ex.InnerException);
return false;
}
}
或者你可以将bool设置为true,然后在最后检查bool是true还是falseFX:
string noInterNet = "";
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 ex)
{
noInterNet = ex.InnerException;
}
}
然后在代码的末尾做:
if(noInterNet != "")
MessageBox.Show("Error " + noInterNet);