7

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?

Oxygen
  • 831
  • 4
  • 17
  • 42
  • Does it work every time if you use same parameters (email account, TO, message) and Machine running c# code? Check send box to make sure limits are not exceeded. Some servers if messages are not sent will only allow a max number before failing. – jdweng Jan 24 '22 at 15:59
  • It fails for the same parameters, running on my local. Daily limit not reached too, just confirmed that. – Oxygen Jan 24 '22 at 16:05
  • What is different between working and not working? – jdweng Jan 24 '22 at 16:13
  • Working means sending email and not working means throwing an above exception. – Oxygen Jan 24 '22 at 16:19
  • Check if you are using the correct target name for a TLS secure email with you ADMIN. Most people having this issue the problem goes away what they change the target. You may be using a target that is overloaded or is designed for non secure and does not work every time with a secure email. – jdweng Jan 24 '22 at 16:32
  • How long does it take for error to occur. If it is 30 seconds that means you never connected to the proxy server. The email credentials have to be verified by a proxy server on your network that is used for port 587 before being sent. When the proxy doesn't respond in 30 seconds your SMTP times out. – jdweng Jan 24 '22 at 16:46

2 Answers2

16

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

{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                      | SecurityProtocolType.Tls11
                                      | SecurityProtocolType.Tls12;
}
Elikill58
  • 4,050
  • 24
  • 23
  • 45
Guy Bahn
  • 176
  • 1
  • 2
0

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;