我使用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");
}
}
答案 0 :(得分:3)
任何依赖于重复尝试的解决方案都可能无休止地循环。
您的代码是在同步发送电子邮件,为什么不send asynchronously使用pickup directory
这会将电子邮件放入SMTP提取目录,并且SMTP服务器将通过重试一段可配置的时间来处理暂时的网络问题。
答案 1 :(得分:0)
当没有互联网时,只需摆脱循环:
{
"capabilities":
[
{
"browserName": "",
"version":"",
"maxInstances": 1,
"platform":"ANDROID"
}
],
"configuration":
{
"cleanUpCycle":2000,
"timeout":30000,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"url":"http://127.0.0.1:4720/wd/hub",
"host":"192.168.1.163",
"port": 4723,
"maxSession": 1,
"register": true,
"registerCycle": 5000,
"hubPort": 4444,
"hubHost": "192.168.1.163"
}
}
答案 2 :(得分:0)
最好使用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实现中,您可以尝试从以下链接获得帮助:
答案 3 :(得分:-1)
使用try catch
继续执行
bool a = true;
while (a)
{
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 e) {
MessageBox.Show(e.Message);
a = false;
}
当你edited your question时,我也更新了我的答案。
现在我创建了布尔值'a',这是真的,直到你陷入困境 catch子句,然后它将通过设置从while循环中断 'a'等于假。
答案 4 :(得分:-1)
试试这个:
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是否为false FX:
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);