【问题标题】:SmtpException while sending Email发送电子邮件时出现 SmtpException
【发布时间】:2015-09-13 05:22:14
【问题描述】:

我正在尝试通过我的 C# 代码发送电子邮件,但我收到了SmtpException

连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应 173.194.67.109:587

这是我发送电子邮件的方式:

string HostAddress = "smtp.gmail.com";
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromEmail);
msg.Subject = "Test Email";
msg.Body = "Hi testing invoice";
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress("ramshaafaq2012@gmail.com"));
SmtpClient client = new SmtpClient();
client.Host = HostAddress;
client.EnableSsl = true;
NetworkCredential creadit = new NetworkCredential();
creadit.UserName = msg.From.Address;
creadit.Password = Password;
client.UseDefaultCredentials = true;
client.Credentials = creadit;
client.Port = 587;
client.Send(msg);

【问题讨论】:

  • 有什么例外?
  • 您是否看过 SmtpException.InnerException,因为它可能包含更详细的信息。
  • 您是否在代理后测试此代码?如果是这样,您是否配置了您的程序以使用它?可能是防火墙阻止了您的请求?
  • @IgnacioSolerGarcia 即使该帖子中推荐的解决方案也不起作用

标签: c# email smtpexception


【解决方案1】:

你的问题在这里:

client.UseDefaultCredentials = true;
client.Credentials = creadit;

您正在指定一组凭据,但您告诉SmtpClient 使用默认凭据(即登录用户的 Windows 用户名和密码)。将 UseDefaultCredentials 设置为 false,它将使用您提供的凭据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-08
    • 2013-08-27
    • 2020-03-12
    • 1970-01-01
    • 2013-06-24
    • 2018-10-17
    相关资源
    最近更新 更多