【问题标题】:Error Failure sending mail with smtpexception使用 smtpexception 发送邮件失败
【发布时间】: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

标签: c# asp.net


【解决方案1】:

可能的解决方案:

1) 使 smtpClient.UseDefaultCredentials = false;

2) 凭据“用户名”和“密码”对发送电子邮件有效。

3) 检查端口 465 未被防火墙阻止。

4) 不要使用端口号 465,而是使用端口号 587。

希望,这可能对你有所帮助。

谢谢。

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2016-04-11
    • 1970-01-01
    • 2012-08-10
    • 2019-03-01
    相关资源
    最近更新 更多