System.Net.Mail.SmtpException: 'Failure sending mail.'

Aishwarya shingre 21 Reputation points
2022-03-09T11:54:08.5+00:00

I am trying to make a simple website but on reset password page after typing any mail address and clicking on reset password button I am getting a error saying

System.Net.Mail.SmtpException: 'Failure sending mail.'

Inner Exception
IOException: Unable to read data from the transport connection: net_io_connectionclosed.

Here is my code

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

namespace ARTIst_Sketch_Work
{
public partial class ForgotPassword : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        string Username = string.Empty;
        string Password = string.Empty;
        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Username, Password FROM [Table] WHERE [E-mail] = @Email"))
            {
                cmd.Parameters.AddWithValue("@Email", TextBoxmail.Text.Trim());
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    if (sdr.Read())
                    {
                        Username = sdr["Username"].ToString();
                        Password = sdr["Password"].ToString();
                    }
                }
                con.Close();
            }
        }
        if (!string.IsNullOrEmpty(Password))
        {
            MailMessage mm = new MailMessage("sender@gmail.com", TextBoxmail.Text.Trim())
            {
                Subject = "Password Recovery",
                Body = string.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", Username, Password),
                IsBodyHtml = true
            };
            SmtpClient smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                EnableSsl = true
            };
            NetworkCredential NetworkCred = new NetworkCredential
            {
                UserName = "sender@gmail.com",
                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.";
        }

    }
}

}

The exception pops at :

smtp.Send(mm);

Any help will be appreciated

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,264 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,599 questions
{count} votes

2 answers

Sort by: Most helpful
  1. John Calladine 0 Reputation points
    2023-10-03T18:06:03.47+00:00

    Found a key solution.

    If you are using .NET version 4.x, SMTP defaults to TLS 1.0. Most modern email systems (Comcast, Gmail, etc.) require you to use TLS 1.2

    Simply add this code in the initial (Main) or top block

    Imports System.Net.ServicePointManager
    ..
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 
    

    More details for the C# version

    https://learn.microsoft.com/en-us/answers/questions/400152/authentication-failed-because-the-remote-party-has

    0 comments No comments

  2. KOZ6.0 6,300 Reputation points
    2023-10-03T21:41:41.7766667+00:00

    I suggest using MailKit.

    https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient

    Important under Remarks

    We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.

    0 comments No comments