发送电子邮件时发生SmtpException

时间:2015-06-26 07:58:59

标签: c# email smtpexception

我正在尝试通过我的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);

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

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

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