简体   繁体   中英

How to resolve Unable to read data from the transport connection: net_io_connectionclosed error while sending email using SMTPClient

We have an email accounts as emailName@companyDomain.in and this is configured in Office365 . We want to send an email using emailName@companyDomain.in from C#. Below code sometimes work and sometimes not (most of the times not working). Giving Error as "Unable to read data from the transport connection: net_io_connectionclosed" . Code is

 public static void SendEmail(string toEmailId, string subject, string mailMessage)
    {
        string fromEmail = "emailName@companyDomain.in";
        MailMessage msg = new MailMessage();
        msg.To.Add(toEmailId);
        msg.From = new MailAddress(fromEmail, "Sender Name");
        msg.Subject = subject;
        msg.Body = mailMessage;
        msg.IsBodyHtml = true;

        SmtpClient client = new SmtpClient();
        client.UseDefaultCredentials = false; // Tried by commenting this too
        client.Credentials = new System.Net.NetworkCredential(fromEmail, "password");
        client.Port = 587; // Tried port number 25
        client.Host = "smtp.office365.com";
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.TargetName = "STARTTLS/smtp.office365.com";
        client.EnableSsl = true;
        try
        {
            client.Send(msg);
        }
        catch (Exception ex)
        {
           
        }
    }

Can you please give any hint about what could be wrong?

Adding this code before creating the smtp client worked for me.

{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                      | SecurityProtocolType.Tls11
                                      | SecurityProtocolType.Tls12;
}

First Use Port = 587

Generally STARTTLS is required to send mail, Adding the Security Protocol Tls12 will help to resolve this issue.

Secondly test the stmp connection using powershell

$userName = 'username_here'
$password = 'xxxxxxxxx'
$pwdSecureString = ConvertTo-SecureString -Force -AsPlainText $password
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $pwdSecureString

$sendMailParams = @{
    From = 'abc.com'
    To = 'xyz@gmail.com'
    Subject = 'Test SMTP'
    Body = 'Test SMTP'
    SMTPServer = 'smtp.server.com'
    Port = 587
    UseSsl = $true
    Credential = $credential
}

Send-MailMessage @sendMailParams

Thirdly If this send out the email, Add below code inside SmtpClient function:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                      | SecurityProtocolType.Tls11
                                      | SecurityProtocolType.Tls12;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM