发送电子邮件.Asp.net.C#.远程主机已强制关闭现有连接

本文关键字:连接 主机 Asp 电子邮件 net 程主机 | 更新日期: 2023-09-27 18:25:50

代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress(txtfrom.Text, "OLMS");
    msg.To.Add(new MailAddress(txtto.Text));
    msg.Subject = txtsub.Text;
    msg.Body = txtbody.Text;
    msg.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Credentials = new NetworkCredential(txtfrom.Text, txtto.Text);
    smtp.EnableSsl = true;
    smtp.Send(msg);
    lblresult.Text = "Message Sent Successful!";
}
}

堆栈跟踪:

[SocketException (0x2746): An existing connection was forcibly closed by the remote host]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, 
SocketFlags socketFlags) +6416371
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +130
[IOException: Unable to read data from the transport connection: 
An existing connection was         forcibly closed by the remote host.]
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, 
Int32 size) +296
System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 
count) +45
System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, 
Int32 count) +106
System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader 
caller, Boolean oneLine) +203
System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader 
caller) +16
System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& 
response) +54
System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn) +28
System.Net.Mail.SmtpConnection.GetConnection(ServicePoint 
servicePoint) +843
System.Net.Mail.SmtpTransport.GetConnection(ServicePoint 
servicePoint) +170
System.Net.Mail.SmtpClient.GetConnection() +44
System.Net.Mail.SmtpClient.Send(MailMessage message) +1554
[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage message) +1906
_Default.Button1_Click(Object sender, EventArgs e) in c:'Users
'Azhar Khan'Documents'Visual Studio 2010'WebSites
'WebSite2'Default.aspx.cs:31
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9553594
System.Web.UI.WebControls.Button.RaisePostBackEvent(String 
eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.R
aisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler 
sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean 
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

我还没有为SMTP配置IIS(我不知道是否要这样做)。我也没有在这个网站的Web.config文件中添加任何smtp配置。任何关于我收到的阻止我发送电子邮件的异常的帮助。感谢

发送电子邮件.Asp.net.C#.远程主机已强制关闭现有连接

确保在谷歌帐户设置中启用允许不太安全的应用选项https://google.com/settings/security/lesssecureapps——从2014年下半年开始,这需要使用基本身份验证通过Gmail SMTP进行身份验证。

或者,您可以禁用上述选项,并为SMTP身份验证实现XOAUTH2(请参阅https://developers.google.com/gmail/xoauth2_protocol)。

从您的代码中可以看出,您正在通过smtp.EnableSsl = true;启用SSL。若你们想用同样的方法,那个么就使用465端口。以下是相关问题的已解决答案链接:

如何使用.NET Framework通过SSL SMTP发送电子邮件?

如果你发现任何困难,请告诉我。