C# 如何忽略发送电子邮件方法并继续执行?
我正在用C#发送电子邮件。当互联网可用时,它工作正常,但它会一次又一次地返回异常消息,当互联网连接不可用时停止执行。如果连接不可用,我如何忽略发送电子邮件并继续执行, 或任何其他合适的方法来实现C# 如何忽略发送电子邮件方法并继续执行?,c#,.net,email,C#,.net,Email,我正在用C#发送电子邮件。当互联网可用时,它工作正常,但它会一次又一次地返回异常消息,当互联网连接不可用时停止执行。如果连接不可用,我如何忽略发送电子邮件并继续执行, 或任何其他合适的方法来实现 while (true) { try { Thread.Sleep(50); MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smt
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");
}
}
在没有互联网的情况下,只需打破循环:
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;
}
}
任何依赖于反复尝试的解决方案最终都可能无休止地循环 您的代码正在同步发送电子邮件,为什么不使用 这会将电子邮件放入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 ex)
{
MessageBox.Show("Error " + ex.InnerException);
return false;
}
}
或者您可以将bool设置为true,然后在最后检查bool是否为true或false
外汇:
然后在代码的末尾执行以下操作:
if(noInterNet != "")
MessageBox.Show("Error " + noInterNet);
最好有一个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实现中,您可以尝试从以下链接获得帮助:
如果
端口
错误,或者SSL
不可用,或者网络凭据
错误,会发生什么情况?这是已给出答案的错误版本。几乎相同的代码,但有一些缺陷,比如在返回类型为boolean的函数中只能返回false。在你的第二个例子中,它仍然多次显示异常,而不是一次。嗯,我需要信息(我没有得到)来为用户预定义返回,我希望他是聪明的enouth,能够将返回转换为其他类型的fx null或错误字符串。不管重点是什么,我试着让他朝着一个方向走,顺便说一句,如果你觉得我的帖子有小瑕疵,编辑比-1要好。为什么我的程序卡住了??当我尝试发送电子邮件时,甚至没有进入调试器。。。当我在发送电子邮件时发表评论,它就会正常运行。。。。为什么我要面对这个问题这可能是因为如果你发送大量邮件,这需要很长时间。或线程。睡眠(50);如果你在fx上发送2000多封邮件,这可能是一个很大的数字。这其中有很多原因,还有文件附件。。如果找不到文件,可能需要一段时间。对不起,我需要更多的信息来帮助您更直接地解决您的问题。。请您编辑您的问题,以便我们获得初始代码?
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);
}
}