Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am getting the the exception "Authentication failed because the remote party has closed the transport stream" on sending mail in c#. My application is in 3.5 framework. I have added smtp.enableSSL attribute as true while sending mail.

What I have tried:

Mail sending Code
private bool SendMail(string fromMail, string senderName, string toMail, string mailContent, string subject)
    {
        bool result = true;
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        try
        {
            MailAddress fromAddress = new MailAddress(fromMail, senderName);

            // You can specify the host name or ipaddress of your server
            // Default in IIS will be localhost 
            smtpClient.Host = ConfigurationManager.AppSettings["SMTPServer"];

            //Added by Alex on 20240625 for sending mail with TLS (Mantis Id-1859) Start 
            string SMTPServerSSLFlag = ConfigurationManager.AppSettings["SMTPServerSSLFlag"] != null ? Convert.ToString(ConfigurationManager.AppSettings["SMTPServerSSLFlag"].ToString()) : "0";
            if (SMTPServerSSLFlag == "1")
            {
                smtpClient.EnableSsl = true;
            }
            //Added by Alex on 20240625 for sending mail with TLS (Mantis Id-1859) End 
            //Default port will be 25
            smtpClient.Port = 25;

            //From address will be given as a MailAddress Object
            message.From = fromAddress;

            // To address collection of MailAddress
            message.To.Add(toMail);
            message.Subject = subject;

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")
            //message.CC.Add("admin1@yoursite.com");
            //message.CC.Add("admin2@yoursite.com");

            // You can specify Address directly as string
            //message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
            //message.Bcc.Add(new MailAddress("admin4@yoursite.com"));

            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = true;

            // Message body content
            message.Body = mailContent;

            // Send SMTP mail
            smtpClient.Send(message);

            //Common.showMessage("e-Mail successfully sent.", this);
        }
        catch (Exception ex)
        {
            result = false;
        }
        return result;
    }
Posted

1 solution

The most likely cause is that the server you're connecting to no longer supports the TLS versions you are trying to use.

Try setting the SchUseStrongCrypto and SystemDefaultTlsVersions registry keys and restarting the computer to see if that fixes the problem.

Transport Layer Security (TLS) best practices with .NET Framework | Microsoft Learn[^]

If that doesn't resolve it, you're going to need to trace the network communication between your computer and the mail server to find out what the problem is.
 
Share this answer
 
Comments
Member 7513082 27-Jun-24 10:45am    
If i upgrade the .net framework of the application from 3.5 to higher version will the mail communication to the server work. Mail sending was working earlier. We set the enable ssl true as the initial fix but now getting the above exception.
Richard Deeming 27-Jun-24 10:47am    
You would need to confirm that the TLS version is the issue before you could answer that.

You'd also need to check that the OS your app is running on supports the TLS version(s) the server has enabled.

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