Click here to Skip to main content
15,887,326 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I am using a MailSystem[^] library to check Gmail account every 10 minutes.

In my application, I connect/log two different Gmail accounts at startup.
Whenever I switch the checking emails methods, I receive the exception:
Exception: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. 
StackTrace: System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)


What I have tried:

I have tried a few things:
Added folder and file to exceptions of Antivirus system.
Allowed the application in the firewall.
I tried first to connect the one email and then connect the second email. After first inbox check, I receive the exception.

I connect to email using the code below, as a note I can add that to connect the email I use Task.Run method:
private Imap4Client Client => client ?? (client = new Imap4Client());

public Email(string username, string password)
{
    try
    {
        Client.ConnectSsl(GMAIL_IMAP, SSL_PORT);
        Client.Login(username, password);
    }
    catch (Exception ex)
    {
        LogSystem.Instance.AddToLog("Exception during logging to email");
        LogSystem.Instance.AddToLog(ex);
    }
}

On the timer object's Tick event, I check the inbox every 10 minutes as recommended in google notes.

Using this code:
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
    var mailbox = Client.SelectMailbox(mailBox);
    MessageCollection messages = mailbox.SearchParse(searchPhrase);
    return messages;
}
Posted
Comments
F-ES Sitecore 3-Aug-20 7:13am    
Check the documentation to see if there is any Dispose or disconnect methods you need to call to clean up the connection. Failing that ask whoever wrote the code as the issue is probably in that library.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900