Using MailKit to send through Outlook.com
Asked Answered
B

5

5

I'm trying to send an email through my Outlook.com email address using MailKit.

I've followed all the examples I've seen online and this is what I have:

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;
}

If I set the useSSL parameter to true on client.Connect(), I get this error:

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

If I set the useSSL parameter to false, I get this error:

AuthenticationException: AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful

What am I doing wrong?

Update

Added ProtocolLogger per the suggestion of @jstedfast and here was the result:

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]

BTW, I commented out some stuff with *. I wasn't sure what that was and if it was sensitive or not.

Bucko answered 9/2, 2019 at 20:4 Comment(0)
B
2

It appears as though the code itself works. However, be aware that if you've set your account to require 2FA, you will get the error message above that indicates that your credentials are invalid. Be sure to disable 2FA!

Bucko answered 11/2, 2019 at 12:53 Comment(1)
So Pop3/SMTP does not support 2FA?Astound
P
4

If the problem you're facing is because of 2FA (2-factor Authentication) you should go to your Microsoft settings, in advanced settings you could request an "app password" once you receive it, you should use it instead of your email's password, worked for me.

Priestly answered 14/6, 2021 at 20:5 Comment(1)
this is the only way to make it work, when 2FA is enabled and you don't want to disable it!Marionmarionette
B
2

It appears as though the code itself works. However, be aware that if you've set your account to require 2FA, you will get the error message above that indicates that your credentials are invalid. Be sure to disable 2FA!

Bucko answered 11/2, 2019 at 12:53 Comment(1)
So Pop3/SMTP does not support 2FA?Astound
S
1

try in this way:

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;

You can also try client.ConnectType = SmtpConnectType.ConnectSSLAuto;


For MailKit

client.Connect("smtp-mail.outlook.com", 587, SecureSocketOptions.StartTls);
client.Authenticate("[email protected]", "mypassword");
Straitlaced answered 9/2, 2019 at 20:12 Comment(4)
Are you using MailKit or are you using the MS SmtpClient? I don't see any UseDefaultCredentials on the MailKit client.Bucko
Yes this is for default. I will check equivalent conf for mailkit. I will ley you knowAccounting
Tried your MailKit example and got this AuthenticationException: AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful [BN6PR06CA0060.namprd06.prod.outlook.com] I literally cut and past my credentials from my code into the Outlook.com login to verify that my credentials are correct.Bucko
you're using port 578 that means TLS not SSL (487). you have to use emailClient.Connect("smtp-mail.outlook.com", 587, MailKit.Security.SecureSocketOptions.StartTls);Hereford
P
1

I have been struggling with this issue myself for a new exchange online account (without 2FA). The account was for a customer and so I did not configure the account in my email client. After many attempts I added the account to my Outlook client and sent a test email. After that the issue was solved. Hope this will help others to solve the problem.

Panchito answered 13/10, 2020 at 7:59 Comment(0)
H
0

The AuthenticationException error means that the server is rejecting your user name and/or password.

Perhaps the username should be me instead of [email protected]?

Try getting a protocol log and seeing what type of authentication mechanism is being used.

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

Heard answered 10/2, 2019 at 3:13 Comment(5)
See my update with results from ProtocolLogger. I'm not any kind of expert with SMTP so that log doesn't mean a whole lot to me.Bucko
The 2 strings that you *'d out (which is good, because that is sensitive info) are the base64 encoded username and password strings. Assuming those strings are correct, then it means the server is not allowing your account to authenticate. Some webmail servers require you to login via the web from the same IP address first. Could that be the issue? If that's not it, then perhaps you need to enable SMTP for your account in your account settings (GMail requires this, for example).Heard
Good call! So, yes, they had some settings for Let devices and apps use POP -- which I had to set on. They also showed SMTP settings Server name: smtp.office365.com, Port: 587, Encryption method: STARTTLS. I updated my settings to reflect the change to the server name, but I'm not sure what to make of the encryption method. Based on the output above, it would appear that something took place relating to STARTTLS.Bucko
You can use client.Connect (host, port, SecureSocketOptions.StartTls); to enforce STARTTLS. By default, passing false is the same as SecureSocketOptions.StartTlsWhenAvailable which, as the name implies, upgrades to STARTTLS whenever the server supports it.Heard
So I think I figured it out. I have 2FA enabled on my Outlook account. I also had it enabled on Gmail and got the same invalid account error. i disabled 2FA on Gmail and sent email no problem. I'm guessing its the same thing with Outlook. Thanks for all your help!Bucko

© 2022 - 2024 — McMap. All rights reserved.