gpt4 book ai didi

asp.net-core - 使用 MailKit 通过 Outlook.com 发送

转载 作者:行者123 更新时间:2023-12-02 03:03:37 30 4
gpt4 key购买 nike

我正在尝试使用 MailKit 通过我的 Outlook.com 电子邮件地址发送电子邮件。

我遵循了我在网上看到的所有示例,这就是我所拥有的:

public async Task SendEmailAsync(string email, string subject, string htmlmessage)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Service Account", "me@outlook.com"));
message.To.Add(new MailboxAddress("First Last", email));
message.Subject = subject;
message.Body = new TextPart(TextFormat.Html)
{
Text = htmlMessage
};

using (var client = new SmtpClient())
{
//Have tried both false and true
client.Connect("smtp-mail.outlook.com", 587, false);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate("me@outlook.com", "mypassword");
await client.SendAsync(message);
client.Disconnect(true);
}

return;
}

如果我在 client.Connect() 上将 useSSL 参数设置为 true,则会收到以下错误:

An error occurred while attempting to establish an SSL or TLS connection

如果我将 useSSL 参数设置为 false,则会收到此错误:

AuthenticationException: AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful

我做错了什么?

更新

根据@jstedfast的建议添加了ProtocolLogger,结果如下:

Connected to smtp://smtp-mail.outlook.com:587/?starttls=when-available
S: 220 BN6PR11CA0009.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sun, 10 Feb 2019 03:26:30 +0000
C: EHLO [192.168.1.12]
S: 250-BN6PR11CA0009.outlook.office365.com Hello [73.175.143.94]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO [192.168.1.12]
S: 250-BN6PR11CA0009.outlook.office365.com Hello [73.175.143.94]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH LOGIN XOAUTH2
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: AUTH LOGIN
S: 334 ************
C: ****************
S: 334 ************
C: ****************
S: 535 5.7.3 Authentication unsuccessful [BN6PR11CA0009.namprd11.prod.outlook.com]

顺便说一句,我用 * 注释掉了一些内容。我不确定那是什么以及它是否敏感。

最佳答案

如果您遇到的问题是由于 2FA(双因素身份验证)引起的,您应该转到 Microsoft 设置,在高级设置中,您可以在收到“应用密码”后请求它,您应该使用它而不是你的电子邮件密码对我有用。

关于asp.net-core - 使用 MailKit 通过 Outlook.com 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54610232/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com