Connecting Tech Pros Worldwide Forums | Help | Site Map

check unread emails?

Member
 
Join Date: Apr 2009
Posts: 44
#1: Sep 30 '09
i wanna get number of unread mails from mail servers, like gmail, yahoo, msn, aim and any other email server which user gives

but the problem is - i dont know HOW (lol) i tried this DLL: http://www.lesnikowski.com/mail/faq.aspx but in its faq it says this:
That is not possible with POP3 protocol. There is no way to mark on the server which messages were received by client application. You have to mark which messages have been read by yourself.

The Windows Live Messenger shows unread mails in messenger, i just wanna implement in my own app, i dont wanna show the mails, i just want the number of unread mails, please note - i m newbie at this Net stuff :), and yes its a C# windows forms

[Edit]
i did some research and *i m not sure* but IMAP can read the number of unread mails?, but do yahoo, msn, gmail, aol and other services support IMAP? if yes then how can i implement IMAP in my application because all the DLL's out there ask money and i dont wanna spend money.. EAGetMail DLL works like a charm but its 30 day trial - argghhh
Newbie
 
Join Date: Jul 2009
Posts: 14
#2: Oct 1 '09

re: check unread emails?


After you have a connection to the server send the command:
"list"

the server then will replay:
+OK 1 414
when +OK state the the command is recognized by the server
1 - is the number of unread emails
414 - is the emails capacity.
Member
 
Join Date: Apr 2009
Posts: 44
#3: Oct 1 '09

re: check unread emails?


Quote:

Originally Posted by liadmz View Post

After you have a connection to the server send the command:
"list"

the server then will replay:
+OK 1 414
when +OK state the the command is recognized by the server
1 - is the number of unread emails
414 - is the emails capacity.

OMG AWESOME!! but how to establish connection to the server (lol)
Newbie
 
Join Date: Jul 2009
Posts: 14
#4: Oct 1 '09

re: check unread emails?


Here is a class I'm working on.
You need to add it to your project and create a new object.

MailHandler X = new MailHandler();

MailHandler.zip

Set the POP3 and SMTP setting using:
X.SetSMTP(txtSMTP_server.Text, txtSMTP_user.Text, txtSMTP_pass.Text);
X.SetPOP(txtPOP_server.Text, txtPOP_user.Text, txtPOP_pass.Text);

To count the emails on the server use:
int EmailCount = X.RetrieveMailCount();

Hope this one will help.
Member
 
Join Date: Apr 2009
Posts: 44
#5: Oct 1 '09

re: check unread emails?


well i dont want total mail counts but want only the count of unread mails, and i m leaving the idea of IMAP because Yahoo and Hotmail dont give IMAP support ffs...
Member
 
Join Date: Apr 2009
Posts: 44
#6: Oct 1 '09

re: check unread emails?


cmon guys please help i need help please!!!!!!!
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#7: Oct 1 '09

re: check unread emails?


Have you tried looking at the Yahoo mail api?
Member
 
Join Date: Apr 2009
Posts: 44
#8: Oct 1 '09

re: check unread emails?


Quote:

Originally Posted by cloud255 View Post

Have you tried looking at the Yahoo mail api?

yea.....but its only for yahoo... and i need gmail, and aol, and hotmail

but i have decided

i am gonna forget hotmail for now and work on Gmail, Aol, and Yahoo
Yahoo has an API so no problems there,
now Gmail and AOL
both support IMAP,
i wrote this code:
Expand|Select|Wrap|Line Numbers
  1.  TcpClient objTCP = new TcpClient();
  2. SslStream sslstream = default(SslStream);
  3. //StreamReader reader = null;
  4. objTCP.Connect("imap.gmail.com", 993);
  5. sslstream = new SslStream(objTCP.GetStream());
  6. sslstream.AuthenticateAsClient("imap.gmail.com");
  7. // authenticate as client GMAIL
  8. // Assigned reader to stream
  9. //reader = new StreamReader(sslstream);
  10. sendcmd("$ LOGIN (my email) (my password) $ STATUS INBOX (unseen)", sslstream);
  11. //string messageCount = sendcmd("$ STATUS INBOX", sslstream);
  12. objTCP.Close();
  13.  
  14. private static string sendcmd(string cmd, SslStream NetStrm)
  15. {
  16.          byte[] szData;
  17.          string returnedData = "";
  18.          StreamReader RdStrm = new StreamReader(NetStrm);
  19.          szData =
  20.          System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray());
  21.          NetStrm.Write(szData, 0, szData.Length);
  22.          returnedData = RdStrm.ReadLine();
  23.          return returnedData;
  24. }
  25.  
but it doesnt seem working, it dont work if i write the commands in different lines too...
i dont wanna use any freaking 3rd party IMAP stuff to connect, so any way to connect to IMAP without 3rd party ****?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,763
#9: Oct 1 '09

re: check unread emails?


Time to stop... take a deep breath...

Yahoo has an api, so you are good there.
You were given a command to ask IMAP for the number of unread messages.
Quote:

Originally Posted by Liadmz

After you have a connection to the server send the command:
"list"

the server then will replay:
+OK 1 414
when +OK state the the command is recognized by the server
1 - is the number of unread emails
414 - is the emails capacity.

You were handed an entire class of code for connectivity.

Quote:
but it doesnt seem working, it dont work
doesn't give anyone any idea what the problem is? Exception... server error... empty data...

After all the help you've been given including someone else's source code outbursts like
Quote:
i dont wanna use any freaking 3rd party IMAP stuff to connect, so any way to connect to IMAP without 3rd party ****?
just seem a little out of place.

I suggest you take a day or even the weekend to first relax, then read back through all the help you've been given, then try to combine some of these fine suggestions.

Come MONDAY: If after that and giving it several of your best "trial and error" attempts you are still having issues, post them with details that could help someone, to help you.
Member
 
Join Date: Apr 2009
Posts: 44
#10: Oct 1 '09

re: check unread emails?


cls
okay cleared my mind, the source i was given is for POP3 not for IMAP, i tried using it with IMAP at the first command (even if i write "guwag") it will reply "+OK CONNETED then some ip bla bla", and when i give it 2nd command it wont reply and app is just stuck waiting for some reply from server...

i just want a simple code to get unread mails from a IMAP server, cmon is that so tuff? :\
codegecko's Avatar
Moderator
 
Join Date: May 2007
Location: United Kingdom
Posts: 395
#11: Oct 1 '09

re: check unread emails?


Vishal,

The principle of what you are trying to achieve is simple enough but you must understand how mail is handled using various protocols, and the existing operating-system-provided tools available to you before you can choose how best to approach the problem.

POP3 is a non-authoratitve email protocol. This means that when you access the mail server, it will send you all of the messages on the server, and then by default remove those messages, emptying the "central" store. You can specify for it not to do this, but then you must keep a local "list" of all emails previously downloaded, to "know" which ones are "new" on the mail server.
On the other hand, IMAP is an authoritative email protocol, so when you access the mail server, all messages previously received are also stored there and the server keeps a tally of which ones are "new".

Both IMAP and POP3 can be utilised using a number of simple Telnet commands - if you do not want to purchase a third-party component, then your best bet is to understand how you can write C# code that will interact with a Telnet session so you can send commands to a remote server and process the response as and when received. At that stage you can then make your own decision about which protocol to use, IMAP or POP3.

I hope that this helps.

codegecko
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,763
#12: Oct 1 '09

re: check unread emails?


Quote:

Originally Posted by vishal1082 View Post

i just want a simple code to get unread mails from a IMAP server, cmon is that so tuff? :\

It doesn't seem to be. From the few minutes I spent looking at it, it seems to do a search on the IMAP server and get results. Search for "unseen" gets you unread messages.

I did a Google for "C# IMAP unread" and got a good set of matches.
Including an entire royalty free IMAP library
A complete listing of search keys for IMAP
Or this source code for an IMAP library project
Member
 
Join Date: Apr 2009
Posts: 44
#13: Oct 1 '09

re: check unread emails?


@codegecko i have decided to IMAP...
@tlhintoq i have googled alot, i have tried both of the lib's
the 1st Lib on code project by Rohit Joshi DOES NOT support SSL, so useless
the 2nd lib - i didnt understand how to use it..
Member
 
Join Date: Apr 2009
Posts: 44
#14: Oct 1 '09

re: check unread emails?


okay so i used the 2nd lib tlhintoq gave (its modified of 1st lib he gave) and saw an example (that is actually for 1st lib) here:
http://kevinthant.wordpress.com/2009...sing-asp-netc/

and wrote this code:
Expand|Select|Wrap|Line Numbers
  1.                     InterIMAP.IMAP l = new InterIMAP.IMAP();
  2.                     l.Login("imap.gmail.com", "(my email)", "(my pass)", true);
  3.                     InterIMAP.IMAPFolder f = new InterIMAP.IMAPFolder("INBOX", "INBOX");
  4.                     l.SelectFolder(f);
  5.                     System.Collections.ArrayList list = new System.Collections.ArrayList();
  6.                     l.SearchMessage(new string[] { "unseen" }, false, list);
  7.                     return list.Count;
but on the line l.SearchMessage i get this error: Error in the application.
i think i m writing wrong folder to select, anyone knw the correct one?
Member
 
Join Date: Apr 2009
Posts: 44
#15: Oct 2 '09

re: check unread emails?


no replies? aww cmon
Member
 
Join Date: Apr 2009
Posts: 44
#16: Oct 2 '09

re: check unread emails?


THE SOLUTION!

Expand|Select|Wrap|Line Numbers
  1.                     l.Login(server, user, pass, ssl);
  2.                     l.SelectFolder("INBOX"); 
  3.                     System.Collections.ArrayList list = new System.Collections.ArrayList();
  4.                     int mails = 0;
  5.                     try { l.SearchMessage(new string[] { "unseen" }, false, list); }
  6.                     catch{}
  7.                     mails = list.Count - 1;
  8.                     l.LogOut();
  9.                     return mails;
i edited the 2nd lib so i can give it a string as folder name, and VOILA! works like a charm!! wohoo!!, only problem is with non-SSL mails that when there is no unread mails it gives error thats why i have the try catch around search message
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,763
#17: Oct 2 '09

re: check unread emails?


Congratulations! A little determination, a little help from others, a LOT of trial and error. Feels good, huh?
Member
 
Join Date: Apr 2009
Posts: 44
#18: Oct 2 '09

re: check unread emails?


YEUP! thanks people!
Reply