0 votes

Code as given below

objImap.SSLConfiguration.EnabledSslProtocols = 
    SslProtocols.Tls12;

objImap.ConnectSSL("outlook.office365.com");
objImap.UseBestLogin("emailaddress", "password");
objImap.SelectInbox();
List<long> uids = objImap.Search(Flag.All);

foreach (long uid in uids)
{
    var eml = objImap.GetMessageByUID(uid);
    IMail objEmail = new MailBuilder()
        .CreateFromEml(eml);


    foreach (MimeData mime in objEmail.Attachments)
    {
        if (mime.ContentType == ContentType.ApplicationPdf)
        {
            string path = @"C:\Att\" + obj.EmailAddress + @"\";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            mime.Save(path + mime.SafeFileName);
        }
    }
}
objImap.Close();

Used TLS and credentials are correct still getting error 6B4D0003 NO AUTHENTICATE failed.

by (200 points)

1 Answer

0 votes

Your problem is not with connecting or SSL/TLS version.
The error is clear: server rejects your credentials.

Most likely you are trying to use your primary password.

You either need to configure your Office365 to allow that, use OAuth or use application passwords.

First make sure IMAP is turned on for this account:
https://www.limilabs.com/blog/office365-enable-imap-pop3-smtp

To use OAuth user flow:
https://www.limilabs.com/blog/oauth2-office365-exchange-imap-pop3-smtp

To use OAuth password grant flow:
https://www.limilabs.com/blog/oauth2-password-grant-office365-exchange-imap-pop3-smtp

To use OAuth client credential flow:
https://www.limilabs.com/blog/oauth2-client-credential-flow-office365-exchange-imap-pop3-smtp

To use OAuth password grant flow:
https://www.limilabs.com/blog/oauth2-password-grant-office365-exchange-imap-pop3-smtp

To use application passwords (no longer recommended):
https://www.limilabs.com/blog/office365-app-passwords

by (297k points)
edited by
I have cheked IMAP is turned on for this account, I want to use the email address and primary password to connect not the application password or OAuth, just simple user email address and password, which should work. How can I do that?
You'll need to work with your Office365 administrator:

- In your AD set "Enable Security defaults" to No.
- Remove any Conditional Access Policies defined in your AD.
- MFA must be turned off for this account.

Some of those changes take 20 minutes to activate.

Please note that using your primary password is not a recommended way.

We strongly suggest switching to one of the OAuth flows.
...