+1 vote

Hi all,

I'm getting the following error intermittently. It is able to connect now and again, however, in most instances it errors when calling "imap.UseBestLogin(_user, _password)" with "Tried to read a line. No data received. "

Any help would be very much appreciated

Private Const _server As String = "outlook.office365.com"
Private Const _user As String = "xxxxxx"
Private Const _password As String = "xxxxxxx"

ServicePointManager.SecurityProtocol = _
    SecurityProtocolType.Tls12

Using imap As New Imap
    Try
         imap.ConnectSSL(_server)                           
         imap.UseBestLogin(_user, _password)                

         imap.SelectInbox()                    

         'Find all unseen messages.
         Dim uids As List(Of Long) = imap.Search(Flag.Unseen)        

         lblOutput.Text = "Number of unseen messages is: " _
              + uids.Count.ToString

         For Each uid As Long In uids

             ' Download and parse each message.
             Dim email As IMail = New MailBuilder() _
                .CreateFromEml(imap.GetMessageByUID(uid))   

             ' Display email data, save attachments:
             ProcessMessage(email)  

         Next

         imap.Close(False)
    Catch ex As Exception
         lblOutput.Text = ex.ToString
    Finally
        imap.Dispose()
    End Try
End Using
by

1 Answer

0 votes

First make sure, you are using the latest version of Mail.dll.

Second, there is a chance that after too many failed attempts, Exchange/Office365 cuts you off. This is what this error actually means.

Office365 does not allow basic authenticationa anymore:
https://www.limilabs.com/blog/office-365-basic-auth-is-disabled

You need to use one of the OAuth 2.0 flows to login:

Daemons/Services: Password grant (MFA/2FA must be turned off for this account):
https://www.limilabs.com/blog/oauth2-password-grant-office365-exchange-imap-pop3-smtp

Daemons/Services: Client credential flow:
https://www.limilabs.com/blog/oauth2-client-credential-flow-office365-exchange-imap-pop3-smtp

Web apps (requires user interaction):
https://www.limilabs.com/blog/oauth2-web-flow-office365-exchange-imap-pop3-smtp

Standalone devices (requires very little interaction):
https://www.limilabs.com/blog/oauth2-device-flow-office365-exchange-imap-pop3-smtp

Desktop apps (requires user interaction):
https://www.limilabs.com/blog/oauth2-office365-exchange-imap-pop3-smtp

by (297k points)
...