SmtpException在发送电子邮件 [英] SmtpException while sending Email
问题描述
我想通过我的C#代码发送电子邮件,但我得到 SmtpException
一个连接尝试失败,因为连接的方没有正确一段时间后响应或已建立的连接失败,因为连接主机未能响应173.194.67.109:587
块引用>
这里是我如何发送电子邮件:
字符串HostAddress =smtp.gmail。 COM;
味精MAILMESSAGE新= MAILMESSAGE();
msg.From =新的MailAddress(fromEmail);
msg.Subject =测试邮件;
msg.Body =嗨测试发票;
msg.IsBodyHtml = TRUE;
msg.To.Add(新MailAddress(ramshaafaq2012@gmail.com));
SmtpClient客户端=新SmtpClient();
client.Host = HostAddress;
client.EnableSsl = TRUE;
的NetworkCredential =空间日志相册新的NetworkCredential();
creadit.UserName = msg.From.Address;
creadit.Password =密码;
client.UseDefaultCredentials = TRUE;
client.Credentials =空间日志相册;
client.Port = 587;
client.Send(MSG);
解决方案您的问题就在这里:
client.UseDefaultCredentials = TRUE;
client.Credentials =空间日志相册;
您指定的一组凭据,但你的也的告诉
SmtpClient
使用默认凭据(即登录用户的Windows用户名和密码)。设置UseDefaultCredentials
设置为false,它会使用你提供的凭据来代替。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);
解决方案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). SetUseDefaultCredentials
to false and it will use your supplied credentials instead.这篇关于SmtpException在发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!