Click here to Skip to main content
16,020,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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.";
        }
    }
}
Posted
Updated 4-May-17 20:09pm
v2

  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);

            }



            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900