 |
| .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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 17th, 2009, 05:39 AM
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi Baburman,
Thank you for your reply.
Now the program is connecting to Gmail.But how to retrive all inbox messages through my program. Please can you help me out.
Thank you with Warm Regards,
Gauri
|
|

February 1st, 2009, 08:06 PM
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Code with minor changes and fixed typos.
I have used this example in .net 3.5 When I first gotthe code i was full of errors.
After making a few adjustments it works great Thanks to the poster for the usefulness of the post here is the code with the changes I made "It could still be formatted a little more professionally"
publicpartialclassMailForm : Form
{
SslStream sslStream = null;
public MailForm()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, 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);
TcpClient Server = newTcpClient("pop.gmail.com", 995);
//Status.Items.Clear();
try
{
/////////////////////////////////////////////////////////////////////////
sslStream = newSslStream(Server.GetStream(), false, newRemoteCertificateValidationCallback(ValidateServerCertificate), null);
sslStream.AuthenticateAsClient("pop.gmail.com");
string messageData = ReadMessage(sslStream, false);
//Status.Items.Add(messageData);
string Data = "USER " + "[email protected]" + Environment.NewLine ;
byte[] szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
sslStream.Write(szData, 0, szData.Length);
sslStream.Flush();
messageData = ReadMessage(sslStream, false);
//Status.Items.Add(messageData);
Data = "PASS " + "YOUR PASSWORD" + Environment.NewLine;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
sslStream.Write(szData, 0, szData.Length);
sslStream.Flush();
messageData = ReadMessage(sslStream, false);
//Status.Items.Add(messageData);
Data = "STAT" + Environment.NewLine;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
sslStream.Write(szData, 0, szData.Length);
sslStream.Flush();
messageData = ReadMessage(sslStream, false);
//Status.Items.Add(messageData);
Data = "LIST" + Environment.NewLine;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
sslStream.Write(szData, 0, szData.Length);
sslStream.Flush();
messageData = ReadMessage(sslStream, true);
//Status.Items.Add(messageData);
/////////////////////////////////////////////////////////////////////////
// back to normal cursor
Cursor.Current = cr;
}
catch (InvalidOperationException err)
{
//Status.Items.Add("Error: " + err.ToString());
}
}
publicstaticbool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
returntrue;
else
returnfalse;
}
publicstaticstring ReadMessage(SslStream sslStream1, bool MultilineResponse)
{
byte[] buffer = newbyte[1024];
int bytes = -1;
StringBuilder messageData = newStringBuilder();
Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars;
while (true)
{
bytes = sslStream1.Read(buffer, 0, buffer.Length);
if (bytes <= 0)
break;
chars = newchar[decoder.GetCharCount(buffer, 0, bytes)];
decoder.GetChars(buffer, 0, bytes, chars, 0);
messageData.Append(chars);
if (!MultilineResponse)
break;
elseif (messageData.ToString().EndsWith("\r\n.\r\n"))
break;
}
return messageData.ToString();
}
privatevoid button2_Click(object sender, EventArgs e)
{
// change cursor into wait cursor
Cursor cr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
try
{
// retrieve mail with number mail parameter
string Data = "RETR " + "1" + Environment.NewLine;
byte[] szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
sslStream.Write(szData, 0, szData.Length);
sslStream.Flush();
string messageData = ReadMessage(sslStream, true);
webBrowser1.DocumentText = messageData;
// back to normal cursor
Cursor.Current = cr;
}
catch (InvalidOperationException err)
{
//Status.Items.Add("Error: " + err.ToString());
}
}
}
|
|
 |