Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 13th, 2008, 01:43 AM
Authorized User
Points: 382, Level: 6
Points: 382, Level: 6 Points: 382, Level: 6 Points: 382, Level: 6
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: May 2004
Location: Rawalpindi, -, Pakistan.
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default How to get message headers from a POP3 server?

Hi guys,

I am working on retrieving mails from a pop3 server (currently gmail). The code is as below. It can susseccfully connect to the server and authenticate. It fetches the message details by providing it the message number, though i am not sure how does it assign message number. Now the main problem is that i first want to get the header information like message sender, date, subject, attachment info. This information will be shown as a grid to the user and then the user will select a message which will then be retrieved from the server. How do i get the message header information? Please help.

This is only the relevant code, the entire form-specifc code can be easily understood and is not necessary to put here.

Here POPServ.Text could be like pop.gmail.com, User.Text=a valid email account in gamil, Passw.Text = user password



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Text;

namespace POPapp
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class POPForm : System.Windows.Forms.Form
    {

        public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
                return true;
            else
                return false;
        }


        public static string ReadMessage(SslStream sslStream1, bool MultilineResponse)
        {
            byte[] buffer = new byte[1024];
            int bytes = -1;
            StringBuilder messageData = new StringBuilder();
            Decoder decoder = Encoding.UTF8.GetDecoder();
            char[] chars;

            while (true)
            {
                bytes = sslStream1.Read(buffer, 0, buffer.Length);
                if (bytes <= 0)
                    break;
                chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
                decoder.GetChars(buffer, 0, bytes, chars, 0);
                messageData.Append(chars);
                if (!MultilineResponse)
                    break;
                else if (messageData.ToString().EndsWith("\r\n.\r\n"))
                    break;
            }
            return messageData.ToString();
        }



        private void ConnectBtn_Click(object sender, System.EventArgs e)
        {
            // change cursor into wait cursor
            Cursor cr = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            // create server POP3 with port 110
            //Server = new TcpClient(POPServ.Text,110);
            Server = new TcpClient(POPServ.Text, 995);
            Status.Items.Clear();

            try
            {
                /////////////////////////////////////////////////////////////////////////
                sslStream = new SslStream(Server.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServer Certificate), null);
                sslStream.AuthenticateAsClient(POPServ.Text);
                string messageData = ReadMessage(sslStream, false);
                Status.Items.Add(messageData);

                Data = "USER " + User.Text + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArr ay());
                sslStream.Write(szData, 0, szData.Length);
                sslStream.Flush();
                messageData = ReadMessage(sslStream, false);
                Status.Items.Add(messageData);

                Data = "PASS " + Passw.Text + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArr ay());
                sslStream.Write(szData, 0, szData.Length);
                sslStream.Flush();
                messageData = ReadMessage(sslStream, false);
                Status.Items.Add(messageData);

                Data = "STAT" + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArr ay());
                sslStream.Write(szData, 0, szData.Length);
                sslStream.Flush();
                messageData = ReadMessage(sslStream, false);
                Status.Items.Add(messageData);

                Data = "LIST" + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArr ay());
                sslStream.Write(szData, 0, szData.Length);
                sslStream.Flush();
                messageData = ReadMessage(sslStream, true);
                Status.Items.Add(messageData);

                /////////////////////////////////////////////////////////////////////////

                // change enabled - disabled button
                ConnectBtn.Enabled = false;
                DisconnectBtn.Enabled = true;
                RetrieveBtn.Enabled = true;

                // back to normal cursor
                Cursor.Current = cr;

            }
            catch (InvalidOperationException err)
            {
                Status.Items.Add("Error: " + err.ToString());
            }
        }


        private void RetrieveBtn_Click(object sender, System.EventArgs e)
        {
            // change cursor into wait cursor
            Cursor cr = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            string szTemp;
            Message.Clear();
            try
            {
                // retrieve mail with number mail parameter
                Data = "RETR " + Number.Text + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArr ay());


                sslStream.Write(szData, 0, szData.Length);
                sslStream.Flush();
                string messageData = ReadMessage(sslStream, true);
                Message.Text = messageData;


                // back to normal cursor
                Cursor.Current = cr;

            }
            catch (InvalidOperationException err)
            {
                Status.Items.Add("Error: " + err.ToString());
            }

        }
    }
}


BaburMan
__________________
BaburMan
Reply With Quote
The Following User Says Thank You to baburman For This Useful Post:
masterMunkus (February 1st, 2009)
  #2 (permalink)  
Old October 13th, 2008, 03:07 AM
samjudson's Avatar
Friend of Wrox
Points: 8,448, Level: 39
Points: 8,448, Level: 39 Points: 8,448, Level: 39 Points: 8,448, Level: 39
Activity: 33%
Activity: 33% Activity: 33% Activity: 33%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,088
Thanks: 1
Thanked 181 Times in 180 Posts
Default

The "TOP" command retrieves the headers and an optional number of lines from the message. This is an optional POP3 command, but is supported by most modern mail servers so shouldn't be a problem.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old October 13th, 2008, 02:08 PM
Authorized User
Points: 382, Level: 6
Points: 382, Level: 6 Points: 382, Level: 6 Points: 382, Level: 6
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: May 2004
Location: Rawalpindi, -, Pakistan.
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sam! Thank you for the reply. But here i encounter another problem. Against one of the client's test account on gmail i get the following response when i use STAT command:


+OK 0 0\r\n


Which means there are 0 messages in his inbox whereas i can see there are 92 messages when i look into them directly through gmail. There are many unread records as well. What's wrong with the code then? Its really annoying to me. Please help.



BaburMan
Reply With Quote
  #4 (permalink)  
Old October 13th, 2008, 02:19 PM
Imar's Avatar
Wrox Author
Points: 67,332, Level: 100
Points: 67,332, Level: 100 Points: 67,332, Level: 100 Points: 67,332, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,231
Thanks: 66
Thanked 1,418 Times in 1,398 Posts
Default

Have you popped messages from Gmail before? I believe by default Gmail only exposes messages through pop since you enabled pop, although you can change that in the settings.....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #5 (permalink)  
Old October 13th, 2008, 02:33 PM
Authorized User
Points: 382, Level: 6
Points: 382, Level: 6 Points: 382, Level: 6 Points: 382, Level: 6
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: May 2004
Location: Rawalpindi, -, Pakistan.
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes i popped messages using my own gmail account. Though there are a couple of other issues but definitely i get the results of STAT, LIST and RETR commands successfully.



Quote:
quote:Originally posted by Imar
 Have you popped messages from Gmail before? I believe by default Gmail only exposes messages through pop since you enabled pop, although you can change that in the settings.....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
BaburMan
Reply With Quote
  #6 (permalink)  
Old October 15th, 2008, 09:21 AM
Authorized User
Points: 382, Level: 6
Points: 382, Level: 6 Points: 382, Level: 6 Points: 382, Level: 6
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: May 2004
Location: Rawalpindi, -, Pakistan.
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default

Ok, now successfully getting mails. Don't know why it wasn't working before. Anyways thank you guys for your suggestions. :)

BaburMan
Reply With Quote
  #7 (permalink)  
Old January 1st, 2009, 08:44 AM
Authorized User
Points: 382, Level: 6
Points: 382, Level: 6 Points: 382, Level: 6 Points: 382, Level: 6
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: May 2004
Location: Rawalpindi, -, Pakistan.
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default DELE doesn't seem to work.

The code has been working successfully. Now the client asked to write code to delete some selected messages. I used dele command. The code follows:

Data = "DELE 2 \r\n";
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArr ay());
sslStream.Write(szData, 0, szData.Length);
sslStream.Flush();
messageData = ReadMessage(sslStream, true);


The problem is that the server response is too slow for dele. It takes about 10 minutes to come back and respond for success. Secondly the dele command requires the quit command at the end so that all updation can be committed to the server:

Data = "QUIT \r\n";
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArr ay());
sslStream.Write(szData, 0, szData.Length);
sslStream.Flush();
messageData = ReadMessage(sslStream, true);


Here the last line which reads the response from the server for quit command causes an exception that says the software that is running in the host machine closed the connection. When i comment out dele command the quit command successfuly ends the session.
__________________
BaburMan
Reply With Quote
  #8 (permalink)  
Old January 4th, 2009, 05:27 AM
Registered User
 
Join Date: Dec 2008
Location: ,MH,India.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi guys..

i m using the above code, but its not worked for me. When i m giving username then in the function "ReadMessage" - the variable bytes gets 0 so because of this it come out from While loop and same happens with the password.
i want to access my gmail acount through progrmming (vb.net/C#.net)
but i am facing problem....
plz help me ...

Thanks and Regards,
Gauri
Reply With Quote
  #9 (permalink)  
Old January 5th, 2009, 06:14 AM
Authorized User
Points: 382, Level: 6
Points: 382, Level: 6 Points: 382, Level: 6 Points: 382, Level: 6
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: May 2004
Location: Rawalpindi, -, Pakistan.
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default Configure pop settings in your gmail account.

Gauri!

You have to give proper pop server credentials. Like for gmail i have set the server as "pop.gmail.com" and the port as 995. For gmail, you also need to allow accessing pop mails right from your gmail account. There's a link that says "Settings" somewhere on the top which you have to explore a bit. For other servers i am not sure. Are you able to connect to the pop server? Consider the following lines which connect to the server:

sslStream = new SslStream(Server.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServer Certificate), null);
sslStream.AuthenticateAsClient(POPServ.Text);
string messageData = ReadMessage(sslStream, false);

Here what i get as a response in messageData is "+OK Gpop ready for requests from ...". Which means the pop server connected successfully. If you get a message like this then you are ok and now you should enter correct email and password in the subsequent calls. Let me know if still not working.
__________________
BaburMan
Reply With Quote
  #10 (permalink)  
Old January 5th, 2009, 06:40 AM
Authorized User
Points: 382, Level: 6
Points: 382, Level: 6 Points: 382, Level: 6 Points: 382, Level: 6
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: May 2004
Location: Rawalpindi, -, Pakistan.
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default Resolved.

Finally found the reason. It was a little mistake. The lines

messageData = ReadMessage(sslStream, true);

for DELE and QUIT should be slightly modified to

messageData = ReadMessage(sslStream, false);

and here we go. Everything works fine . In fact the second parameter of ReadMessage() should be set to true only if its command expects multiple- line response from the server.
__________________
BaburMan
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can any body help POP3 in C# coding... Bashu C# 2005 2 August 20th, 2007 01:10 PM
Microsoft SMTP POP3 HOW? bluelabs Internet Information Services 1 February 16th, 2007 09:40 PM
Fsockopne with pop3 mail vibinkrish Pro PHP 0 March 1st, 2006 10:16 AM
POP3 and ASP knightm28 SQL Server ASP 0 April 6th, 2005 04:46 AM
SMTP and POP3 for local bspradeep Classic ASP Professional 2 January 17th, 2004 03:11 PM



All times are GMT -4. The time now is 09:55 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.