MailKit 身份验证失败

MailKit authentication fails

我有下面的代码

MimeMessage message = new MimeMessage();

        MailboxAddress from = new MailboxAddress("Admin",
        "myemail@gmail.com");
        message.From.Add(from);

        MailboxAddress to = new MailboxAddress("User",
        "myemail2@gmail.com");
        message.To.Add(to);

        message.Subject = "Hi user";
        BodyBuilder bodyBuilder = new BodyBuilder();
        bodyBuilder.TextBody = "message body here";
        message.Body = bodyBuilder.ToMessageBody();
        SmtpClient client = new SmtpClient();
        
        client.AuthenticationMechanisms.Remove("XOAUTH2");
        client.Connect("smtp.gmail.com", 465, true);
        client.Authenticate("myemail@gmail.com", "pass");
        client.Send(message);
        client.Disconnect(true);
        client.Dispose();

它说我的凭据不正确,尽管它们是正确的。我正在使用 MailKitMimeMessage。 我在这里做错了什么?

尝试使用端口 587 而不是 465。 这是我的 github 存储库的 link,一个发送邮件的简单项目。 https://github.com/osman-developer/sendingMails

你可以使用类似的东西。

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Test Project",
"your email"));
message.To.Add(new MailboxAddress("pritom", email));
message.Subject = "Hi,this is demo email";
message.Body = new TextPart("plain")
{
Text = "Hello,My First Demo Mail it is.Thanks",
};
//add attach
MemoryStream memoryStream = new MemoryStream();
BodyBuilder bb = new BodyBuilder();
using (var wc = new WebClient())
{
//bb.Attachments.Add("attachmentName",
wc.DownloadData("wwwroot/Images/H.pdf"));
bb.Attachments.Add("Email.pdf",
wc.DownloadData("wwwroot/Images/Email.pdf"));
//bb.Attachments.Add("H.pdf", new MemoryStream());
}
message.Body = bb.ToMessageBody();
//end attach
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, false);
client.Authenticate("your email",
"yourpassword");
client.Send(message);
client.Disconnect(true);
}

更新

转到这个link

enter link description here

并允许安全性较低的应用。

如果您遇到同样的问题,请转到您的 gmail 帐户 > 安全 > 允许安全性较低的应用。这解决了我的问题