SmtpException while sending Email

梦想与她 提交于 2019-12-02 18:12:40

问题


I am trying to send Email through my C# code but I am getting SmtpException

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 173.194.67.109:587

Here is how I am sending Email:

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:


Your problem is here:

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

You are specifying a set of credentials but you are also telling SmtpClient to use the default credentials (i.e. the Windows username and password of the logged-in user). Set UseDefaultCredentials to false and it will use your supplied credentials instead.



来源:https://stackoverflow.com/questions/31067990/smtpexception-while-sending-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!