使用本地计算机上的 Gmail 帐户发送电子邮件
我在我的 asp.net 应用程序中使用 gmail 发送电子邮件。如果我在服务器上发送电子邮件,电子邮件工作正常,但如果我尝试在本地计算机上发送电子邮件,则会出现错误。我在代码中放置了断点,当调用 send 方法时,它会显示错误框,标题为“Smtp 异常未由用户代码处理”,详细地说是“Faliur 发送邮件”。
如果我继续在浏览器上继续,它会显示包含以下详细信息的错误页面:
现有连接被远程主机强行关闭 描述:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.Net.Sockets.SocketException:现有连接被远程主机强制关闭
下面是我的代码,请指导我。
谢谢。
protected void btnConfirm_Click(object sender, EventArgs e)
{
string _Message = GetAdminEmailMessage();
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
NetworkCredential mailAuthentication = new NetworkCredential("[email protected]", "mypassword");
message.To.Add(new MailAddress("[email protected]"));
message.From = new MailAddress("[email protected]");
message.IsBodyHtml = true;
message.Subject = "Local test email";
message.Body = _Message;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = "smtp.gmail.com";
smtp.Credentials = mailAuthentication;
smtp.Send(message);
}
I am using gmail for sending email in my asp.net application. Email works fine if I send email on server but if I try to send emails on local machine it give error. I placed break poin in code and when send method is called it shows error box with heading "Smtp exception was unhandled by user code" and in detail it says "Faliur sending mail".
If I continued on browser it shows error page with these details:
An existing connection was forcibly closed by the remote host Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
Below is my code, kindly guide me.
Thanks.
protected void btnConfirm_Click(object sender, EventArgs e)
{
string _Message = GetAdminEmailMessage();
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
NetworkCredential mailAuthentication = new NetworkCredential("[email protected]", "mypassword");
message.To.Add(new MailAddress("[email protected]"));
message.From = new MailAddress("[email protected]");
message.IsBodyHtml = true;
message.Subject = "Local test email";
message.Body = _Message;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = "smtp.gmail.com";
smtp.Credentials = mailAuthentication;
smtp.Send(message);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误代码 0x800CCC0E 表示端口已被阻止: http://support.microsoft.com/kb/191687
尝试 telnet smtp.gmail.com 587 - 可以连接吗?
如果您的端口已解锁并且您的凭据正确,则此问题接受的答案中的代码应该可以工作。
使用 C# 通过 Gmail SMTP 服务器发送电子邮件
Error code 0x800CCC0E indicates that the port has been blocked: http://support.microsoft.com/kb/191687
Try telnet smtp.gmail.com 587 - Can you connect?
If your port is unblocked and your credentials correct, then the code in the accepted answer for this question should work..
Sending email through Gmail SMTP server with C#