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