使用MailKit通过Outlook.com发

问题描述 投票:2回答:3

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

我跟着我在网上看到的所有例子中,这是我所:

public async Task SendEmailAsync(string email, string subject, string htmlmessage)
{
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("Service Account", "[email protected]"));
    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("[email protected]", "mypassword");
        await client.SendAsync(message);
        client.Disconnect(true);
    }

    return;
}

如果我设置useSSL参数trueclient.Connect(),我得到这个错误:

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

如果我设置useSSL参数false,我得到这个错误:

AuthenticationException: AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful

我究竟做错了什么?

更新

新增ProtocolLogger每@jstedfast的建议,这里是结果:

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]

顺便说一句,我注释掉了一些东西与*。我不知道那是什么,如果这是大小写。

asp.net-core mailkit outlook.com
3个回答
1
投票

尝试以这种方式:

client.Connect("smtp-mail.outlook.com", 587);
client.UseDefaultCredentials = false;    
client.Credentials = new System.Net.NetworkCredential(From, Password);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;

您也可以尝试client.ConnectType = SmtpConnectType.ConnectSSLAuto;


对于MailKit

client.Connect("smtp-mail.outlook.com", 587, SecureSocketOptions.StartTls);
client.Authenticate("[email protected]", "mypassword");

0
投票

AuthenticationException错误意味着服务器拒绝用户名和/或密码。

也许用户名应me代替[email protected]

尝试得到一个协议日志,看到正在使用什么类型的认证机制。

https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog


0
投票

它看起来好像代码本身的工作原理。但是,要知道,如果你已经设置您的帐户,要求2FA,你会得到错误信息上面表明您的凭据无效。一定要禁用2FA!

© www.soinside.com 2019 - 2024. All rights reserved.