代码背后:
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(我不知道是否要这样做)。我还没有在这个wesite中将任何smtp配置添加到Web.config文件中。关于我收到的异常的任何帮助都阻止我发送电子邮件。感谢
答案 0 :(得分:1)
确保在https://google.com/settings/security/lesssecureapps的Google帐户设置中启用允许安全性较低的应用选项 - 从2014年下半年开始,需要使用基本身份验证通过Gmail SMTP进行身份验证
或者,您可以禁用上述选项并实施XOAUTH2进行SMTP身份验证(请参阅https://developers.google.com/gmail/xoauth2_protocol)。
答案 1 :(得分:0)
您的代码似乎是smtp.EnableSsl = true;
启用SSL。
如果您想以相同的方式执行此操作,请使用465端口。
以下是相关问题的已解决答案链接:
How can I send emails through SSL SMTP with the .NET Framework?
如果您发现任何困难,请告诉我。