我要发送电子邮件,但错误提示发送电子邮件失败 我的代码出了什么问题

I am going to send email but the error says the failure to send email what's wrong in my code

var userClient = new SmtpClient("smtp.gmail.com")
            {
                Port = 587,
                Credentials = new NetworkCredential("email_sender", "sender_password"),
                EnableSsl = true
            };
userClient.Send("email_sender", "email_reciver", "body", "subject");

有我用的代码,问题出在哪里?!

我建议更新您的 SmtpClient 设置以设置 DeliveryMethod 和 UserDefaultCredentials 属性:

var userClient = new SmtpClient("smtp.gmail.com")
{
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential("email_sender", "sender_password")
};

userClient.Send("email_sender", "email_reciver", "body", "subject");

我过去曾看到,如果您不设置 DeliveryMethod,您将遇到身份验证问题。

我还会检查 email_sender 和 email_password 凭据是否正确。