如何为发送忘记密码的邮件解决此错误 [英] How do I solve this error for sending mail for forgot password

查看:84
本文介绍了如何为发送忘记密码的邮件解决此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法从传输连接读取数据:net_io_connectionclosed。



描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.IO.IOException:无法从传输连接中读取数据:net_io_connectionclosed。



来源错误:





第55行:smtp.Credentials = NetworkCred;

第56行:smtp .Port = 587;

第57行:smtp.Send(mm);

第58行:lblMessage.ForeColor = Color.Green;

第59行:lblMessage.Text =密码已发送到您的电子邮件地址。;



我尝试过:



Unable to read data from the transport connection: net_io_connectionclosed.

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.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

Source Error:


Line 55: smtp.Credentials = NetworkCred;
Line 56: smtp.Port = 587;
Line 57: smtp.Send(mm);
Line 58: lblMessage.ForeColor = Color.Green;
Line 59: lblMessage.Text = "Password has been sent to your email address.";

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Drawing;
using System.Configuration;
using System.Data.SqlClient;

public partial class forgotPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SendEmail(object sender, EventArgs e)
    {
        string username = string.Empty;
        string password = string.Empty;
        string constr = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Firstname, [Password] FROM registration WHERE Email = @Email"))
            {
                cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    if (sdr.Read())
                    {
                        username = sdr["Firstname"].ToString();
                        password = sdr["Password"].ToString();
                    }
                }
                con.Close();
            }
        }
        if (!string.IsNullOrEmpty(password))
        {
            MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text.Trim());
            mm.Subject = "Password Recovery";
            mm.Body = string.Format("Hi {0},<br><br>Your password is {1}.<br><br>Thank You.", username, password);
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential();
            NetworkCred.UserName = "sender@gmail.com";
            NetworkCred.Password = "<password>";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
            lblMessage.ForeColor = Color.Green;
            lblMessage.Text = "Password has been sent to your email address.";
        }
        else
        {
            lblMessage.ForeColor = Color.Red;
            lblMessage.Text = "This email address does not match our records.";
        }
    }
}

推荐答案

  private AlternateView Mail_Body(string username, string password)
        {
          
            string str = @"   
            <!DOCTYPE html>
            <html>
            <body>

            <p>
            <h4>This is your Login Details </h4>
            </p>
            <p>Username: " + username + @"</p>
            <p>Password: " + password + @"</p>
            <p>
            Note:We advise change of password after logging in.
            </p>
            <p>
             Please do not hesitate to contact us if you need any assistance
            </p>
            <p>

            </p>
            </body>
            </html>
              ";
            AlternateView AV =
            AlternateView.CreateAlternateViewFromString(str, null, MediaTypeNames.Text.Html);
           
            return AV;
        }



protected void SendEmail(object sender, EventArgs e)
            {
                string username = string.Empty;
                string password = string.Empty;
                string constr = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT Firstname, [Password] FROM registration WHERE Email = @Email"))
                    {
                        cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                        cmd.Connection = con;
                        con.Open();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {

                            if (sdr.HasRows)
                            {
                                while (sdr.Read())
                                {
                                    username = sdr["Firstname"].ToString();
                                    password = sdr["Password"].ToString();
                                }

                            }
                        }
                        con.Close();
                    }
                }

            if (!string.IsNullOrEmpty(password))
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("sender@gmail.com");
                mail.To.Add("User@gmail.com");
                mail.Subject = "Password Recovery";
                mail.IsBodyHtml = true;
                mail.AlternateViews.Add(Mail_Body("username", "Password"));
                //mail.Body = message;

                SmtpServer.Port = 25;
                SmtpServer.Credentials = new System.Net.NetworkCredential("email Username", "email Password");
                SmtpServer.EnableSsl = false;

                SmtpServer.Send(mail);

            }



            }


也许解决方案1可以帮到你





无法从传输连接中读取数据:net_io_connectionclosed。 [ ^ ]





c# - SmtpException:Unable从传输连接读取数据:net_io_connectionclosed - 堆栈溢出 [ ^ ]
Maybe solution 1 can help you


Unable to read data from the transport connection: net_io_connectionclosed.[^]


c# - SmtpException: Unable to read data from the transport connection: net_io_connectionclosed - Stack Overflow[^]


这篇关于如何为发送忘记密码的邮件解决此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆