System.IO.IOException: Unable to read data from the transport connection: Connection reset by peer

I want to send a verification code via email but smtp does not work.

This is my method:

public static void SendVerificationEmail(string email, string code)
{
    var fromAddress = new MailAddress("[email protected]", "ExampleApp");
    var toAddress = new MailAddress(email);
    const string fromPassword = "my-email-password";

    const string subject = "Email Verification";
    string body = $"Your verification code is {code}";

    var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };

    using (var message = new MailMessage(fromAddress, toAddress)
                             {
                                 Subject = subject,
                                 Body = body
                             })
    {
        smtp.Send(message);
    }
}

I get this error:

enter image description here

How can I solve that? Please help me. Thank you in advance.

Upvotes: 0

Views: 216

Answers (0)

Related Questions