【问题标题】:Error in client.SendAsync客户端错误。SendAsync
【发布时间】:2014-09-09 02:43:05
【问题描述】:

我尝试根据代码发送电子邮件:

http://msdn.microsoft.com/es-es/library/system.net.mail.smtpclient(v=vs.110).aspx

Sending email in .NET through Gmail

所以我做到了:

SmtpClient client = new SmtpClient("smtp.gmail.com",587);
MailAddress from = new MailAddress("xxx@gmail.com","Jane " + (char)0xD8 + " Clayton",System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("yyy@gmail.com");
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userState = "test message1";
client.SendAsync(message, userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = Console.ReadLine();
if (answer.StartsWith("c") && mailSent == false)
{
    client.SendAsyncCancel();
}
message.Dispose();
Console.WriteLine("Goodbye.");
return View();

但在“client.SendAsync(message, userState);”行出现问题 和错误说:

System.dll 中的“System.Net.Mail.SmtpException”类型的未处理异常,但未在用户代码中处理。

附加信息:发送邮件时出错。

我该如何解决它??

【问题讨论】:

  • Console.ReadLine() 在 ASP.NET 项目中?它从哪个控制台读取?
  • 好吧...我将代码更改为link,但没有工作....并且错误仍然在这里“异常是:System.Net.Mail.SmtpException:发送邮件失败。---> System.Net.WebException: Unable to connect to the remote ---> System.Net.Sockets.SocketException server: 连接尝试时出错,因为连接方在一段时间后没有正确响应或发生错误连接已建立,因为连接的主机未能响应......"
  • 您应该在您的问题中发布整个异常。它有很大的不同。例外情况很明显:您尝试与之交谈的系统没有应答。原因与打电话的原因相同:错误号码,正确号码但他们没有接听,正确号码但他们正忙。下一步是找出哪个是真的。
  • 感谢您的回复和解释!!...m...所以我需要更改到另一个端口??像“25”??但发生同样的错误,整个错误:link where line 105 is "smtp.Send(message);"
  • 您需要更改为正确端口,并且您需要确保您可以从您的机器访问该端口。就像@SLaks 所说,您的机器可能正在阻止传出连接,至少到 smtp.gmail.com。

标签: c# asp.net .net email


【解决方案1】:

Gmail 需要 SSL 连接。

EnableSsl 设置为真。

【讨论】:

  • 所以我尝试了:link 但也没有用...错误说:异常是:System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException: Unable to connect to the remote ---> System.Net.Sockets.SocketException server: 在连接尝试过程中发生错误,因为连接方在一段时间或一段时间后没有正确响应连接建立时出错,因为连接的主机没有响应............等等。
  • 您的服务器可能正在阻止出站连接。
  • 这是什么意思?? M...我能做些什么来解决它?对不起...我对网络知识一无所知...
  • 要求您的主机在该端口上启用出站连接,或切换到更好的主机。
【解决方案2】:

首先将您的代码保留在 try catch 块内 -

.....//other codes

try{

    client.SendAsync(message, userState);

}
catch(Exception e){
    //add a breakpoint here to see what is the error
}

然后,在 catch 块内添加一个断点并开始调试以查看错误/异常是什么。通常大多数异常都包含修复它的说明。所以你找到了错误,你就找到了解决方案。

【讨论】:

  • 错误说:异常是:System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException: Unable to connect to the remote ---> System.Net.Sockets.SocketException server: 在连接尝试过程中发生错误,因为连接方在一段时间或一段时间后没有正确响应连接建立时出错,因为连接的主机没有响应............等等。
【解决方案3】:

最后我改变了我的代码类似于>>

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("RecipientMail");
            msg.From = new MailAddress("sender@gmail.com", "gmailpassword", System.Text.Encoding.UTF8);
            msg.Subject = "UR SUBJECT";
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = "UR BODY";
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = false;

            //Aquí es donde se hace lo especial
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "gmailpassword");
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true; //Esto es para que vaya a través de SSL que es obligatorio con GMail
            try
            {
                client.Send(msg);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }

并在此页面中设置我的 gmail 帐户>> https://www.google.com/settings/security/lesssecureapps

感谢大家的帮助!!

【讨论】:

  • 您希望将异常输出到哪个控制台?如果这是 ASP.NET,那么您应该摆脱 try/catch 块。完整的异常将显示在“黄屏死机”上。
  • 另外,MailMessageSmtpClient 都实现了IDisposable 接口,所以你应该有using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage()){ ... using (SmtpClient client = new SmtpClient()) { ... }}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 2011-10-03
  • 2018-10-29
相关资源
最近更新 更多