【发布时间】:2017-04-23 15:13:52
【问题描述】:
我正在尝试通过我的网络发送电子邮件,我研究寻求帮助没有运气。 仍然收到错误:用户代码未处理 smtpexception
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("abc@gmail.com");
msg.To.Add(TextBox3.Text);
msg.Subject = TextBox4.Text;
msg.Body = TextBox5.Text;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
System.Net.NetworkCredential netCred = new System.Net.NetworkCredential();
netCred.UserName = "abc@gmail.com";
netCred.Password = "11111111";
smtp.UseDefaultCredentials = true;
smtp.Credentials = netCred;
smtp.Port = 465;
smtp.EnableSsl = true;
smtp.Send(msg);
try
{
smtp.Send(msg);
Label1.Text = "Your E-Mail Sent Great Job!!!!";
}
catch (Exception ex)
{
//Handle your exception here
Label1.Text = ex.Message + " " + "Oeps, something when wrong when we tried to send the email";
return;
}
}
当我运行它时,它会显示这个错误,突出显示 smtp.Send(message);:
用户代码未处理 SmtpException,发送邮件失败。
System.Net.Mail.SmtpException:发送邮件失败。 ---> System.IO.IOException:无法从传输连接读取数据:net_io_connectionclosed。在 System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory .ReadLine(SmtpReplyReader caller) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net .Mail.SmtpClient.Send(MailMessage message) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Mail.SmtpClient.Send(MailMessage message) at IntroAsp.net.EmailMsg.Button1_Click(Object sender, EventArgs e ) 在 c:\Users\Regev\Documents\Visual Studio 2013\Projects\IntroAsp.net\IntroAsp.net\EmailMsg.aspx.cs:46 行
【问题讨论】:
-
异常细节应该远不止这些,你能补充一下吗?
-
你检查过这个 SmtpException 及其消息吗?
-
确保 gmail 设置为允许
less secure apps