0

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.

4
  • ~~The exception suggests that a failure is happening on the SMTP server side. You'll need to be able to look into the logs on the server side to see what's going wrong.~~ - Just noticed it's GMail, so you don't be able to do that... Commented Jun 13, 2024 at 13:17
  • @MartinCostello Firstly, thank you for your answer but I don't understand clearly. What can I do to send email? Commented Jun 13, 2024 at 13:35
  • I don't know - the exception appears to be coming from the server. Maybe it's a bug on their end, or a poor way of handling client errors, such as if the credentials are invalid. Commented Jun 13, 2024 at 13:37
  • @MartinCostello ok, got it, thank you so much. Commented Jun 13, 2024 at 13:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.