1
votes

I'm using Mailkit v2.2.0 to retrieve e-mails from an exchange server using IMAP. First, I read mails and process them, afterwards they will be deleted - if neccessary. Therefore, I connect to the IMAP server two times within a short time span.

private ImapClient GetImapClient(MailServerConnectionString connectionString) {
    var client = new ImapClient();

    if (connectionString.AcceptAllCertificates) {
        client.ServerCertificateValidationCallback = (s, c, h, e) => true;
    }

    client.Connect(connectionString.Server, connectionString.Port, connectionString.Encryption);

    if (connectionString.AuthenticationRequired) {
        client.Authenticate(connectionString.UserName, connectionString.Password);
    }

    return client;
}

Retrieve the mails:

using (var client = GetImapClient(config.ImapConnectionString)) {
    var inbox = client.Inbox;
    inbox.Open(FolderAccess.ReadWrite);

    var items = inbox.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size | MessageSummaryItems.Flags);

    foreach (var item in items) {
        var message = inbox.GetMessage(item.UniqueId);
        var mail = message.Convert(includeAttachments);

        mail.Seen = item.Flags?.HasFlag(MessageFlags.Seen) ?? false;
        mail.UniqueId = item.UniqueId.Id;

        result.Add(mail);

        inbox.AddFlags(item.UniqueId, MessageFlags.Seen, true);
    }

    inbox.Close();
    client.Disconnect(true);
}

After processing the mails, delete them:

using (var client = GetImapClient(config.ImapConnectionString)) {
    var inbox = client.Inbox;
    inbox.Open(FolderAccess.ReadWrite);

    inbox.AddFlags(ids, MessageFlags.Deleted, true);
    inbox.Expunge();

    client.Disconnect(true);
}

Good to know:

  • Everything works just fine with the hMailServer, MS Exchange doesn't though.

  • The first connection works, the second connection fails.

  • The exception on the second connection: An error occurred while attempting to establish an SSL or TLS connection.

  • I'm using port 993 and SecureSocketOptions.SslOnConnect both times.

1
I figured out, that it doesn't make a difference if I open the inbox or not. Even just establishing the connection and instantly closing it reproduces the problem. - mrsubwoof
Have you considered keeping the ImapClient connection alive rather than constantly connecting and disconnecting? - jstedfast
Yes. Originally, these two operations were intended to be a closed action each, but if that won't work, I've to consider keeping the connection open as well. - mrsubwoof
I think that will be your best bet. - jstedfast

1 Answers

0
votes

This exception means that System.Net.Security.SslStream.AuthenticateAsClient() has failed.

It's possible that the SslStream was unable to connect to the Certificate Authority in order to check revocation status of the server's certificates.

To disable certificate revocation checks, you can set the client.CheckCertificateRevocation property to false before connecting.

It's also possible that the Exchange server is configured to limit frequent connections from the same IP address as an attempt to foil DoS attacks. To deal with this possibility, you will need to either keep the ImapClient connection alive (i.e. stop using ImapClient in short bursts like you are doing) or else wait a longer period of time between reconnecting.