Unable To Read All Mails

Nov 19, 2013 at 1:55 AM
dear sir

when i am trying to get all mails data , it shows below error.

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host.

i try with 2 methods.
1) pop
2) imap

it reads 1 mail only. and displays same error.

My Code

ImapClient ic=new ImapClient("imap.gmail.com");
ic.UserName="username";
ic.Password="password";
ic.Ssl=true;
ic.Port=993;
ic.ReceiveTimeout=600000;
string alldata=string.Empty;
if(ic.Authenticate()==true)
{
ImapFolder inbox=ic.SelectFolder("inbox");
for(int i=inbox.MailCount ;i>0;i--)
{
MailMessage msg=ic.GetMessage(i);
alldata=alldata+msg.BodyText+"\n";
}
}

please solve my problem

thank you
Coordinator
Nov 21, 2013 at 4:28 AM
Edited Nov 21, 2013 at 4:48 AM
Hi
What line that exception is thrown?

Here?
MailMessage msg=ic.GetMessage(i);
Nov 21, 2013 at 5:00 AM
Edited Nov 21, 2013 at 5:05 AM
i think some emails body is empty. so it shows error.

ok another problem.

EX: a email contains plain text and images and attachments.

i need only plain text, exclude attachments and images.

is it possible?

getting the mail data by GetMessage(), what is the internal process done here?
is it downloading all mail message data, include images & attachments ?
can i directly get the plain text data, without download all mail data, i.e. don't download images & attachments?
Coordinator
Nov 25, 2013 at 5:51 AM
Can you send me the mail that cause error?
I modify library to handle it correctly.

It is impossible to download only plain text due to the spec of POP3 protocol.
So, GetMessage method all data like below and parse it devide to text and images and construct MailMessage object.

---Sample data-----------------------------------
Date: 7 Nov 2013 03:40:42 -0800
Subject: HigLabo Imap
Content-Type: multipart/alternative;
---skip---
X-SID-Result: PASS
X-Message-Status: n:n
MIME-Version: 1.0

----boundary_4_0a55d2e0-e910-4763-9c64-89a078f7db23
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

VXNlciBLZWVzQWxkZXJsaWVzdGVuIGhhcyBjb250YWN0ZWQgeW91IHRocm91Z2ggQ29k
ZVBsZXggdXNpbmcgdGhlIENvbnRhY3QgVXNlciBmb3JtIChodHRwczovL3d3dy5jb2Rl
---skip---
bGQgaSB3b3VsZCBiZSB2ZXJ5IGhhcHB5IDotKQ0KDQpLaW5kIHJlZ2FyZHMsDQpLZWVz
IEFsZGVybGllc3Rlbg==
----boundary_4_0a55d2e0-e910-4763-9c64-89a078f7db23
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64

PGh0bWw+PGJvZHk+PGk+VXNlciBLZWVzQWxkZXJsaWVzdGVuIGhhcyBjb250YWN0ZWQg
eW91IHRocm91Z2ggQ29kZVBsZXggdXNpbmcgdGhlIDxhIGhyZWY9J2h0dHBzOi8vd3d3
---skip---
LmNvZGVwbGV4LmNvbS9zaXRlL3VzZXJzL2NvbnRhY3QvS2Vlc0FsZGVybGllc3Rlbj9P
ZHMsPGJyIC8+S2VlcyBBbGRlcmxpZXN0ZW48L2JvZHk+PC9odG1sPg==
----boundary_4_0a55d2e0-e910-4763-9c64-89a078f7db23--

---End of Sample data-----------------------------------


I recommend that you download mail data to your own database each 10 minute.
It might be a service application executing in background.
And your main application show mail data from your database.
It is easy because your application become a simple database application.

regards.