Sign in to follow this  

[.net] Read e-mail in C#

This topic is 4725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I should've been more clear in my first post.

I want to utilize pop3, log-on to my e-mail account, and then display my e-mail messages.

Can anyone help me out with a very basic barebone system of pop3? I'm having trouble figuring out where to even begin.

Thanks for your help.

Share this post


Link to post
Share on other sites
Check out Indy ( http://www.indyproject.org/ ) I haven't used it myself, but you might find their Internet Direct library to be very useful.

Also, this article might be helpful:
http://codeproject.com/csharp/despop3client.asp?df=100&forumid=33306&exp=0&select=766084

Share this post


Link to post
Share on other sites
Thanks for the links. :)

I found another link that I was able to use and get my program to work!
I seem to have lost the link, but I have the source I can post for others interested. Now I must tackle sending e-mail's, and those links will help me even more. Thank you both very much.


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.IO;

namespace POPapp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class POPForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox POPServ;
private System.Windows.Forms.TextBox User;
private System.Windows.Forms.TextBox Passw;
private System.Windows.Forms.RichTextBox Message;
private System.Windows.Forms.Button ConnectBtn;
private System.Windows.Forms.Button DisconnectBtn;
private System.Windows.Forms.ListBox Status;
private System.Windows.Forms.Button RetrieveBtn;
private System.Windows.Forms.TextBox Number;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public POPForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Number = new System.Windows.Forms.TextBox();
this.Status = new System.Windows.Forms.ListBox();
this.label4 = new System.Windows.Forms.Label();
this.Message = new System.Windows.Forms.RichTextBox();
this.label6 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.ConnectBtn = new System.Windows.Forms.Button();
this.POPServ = new System.Windows.Forms.TextBox();
this.User = new System.Windows.Forms.TextBox();
this.DisconnectBtn = new System.Windows.Forms.Button();
this.Passw = new System.Windows.Forms.TextBox();
this.RetrieveBtn = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Number
//
this.Number.Location = new System.Drawing.Point(80, 296);
this.Number.Name = "Number";
this.Number.Size = new System.Drawing.Size(56, 20);
this.Number.TabIndex = 12;
this.Number.Text = "";
//
// Status
//
this.Status.Location = new System.Drawing.Point(8, 344);
this.Status.Name = "Status";
this.Status.Size = new System.Drawing.Size(392, 56);
this.Status.TabIndex = 16;
this.Status.SelectedIndexChanged += new System.EventHandler(this.Status_SelectedIndexChanged);
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 96);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 16);
this.label4.TabIndex = 7;
this.label4.Text = "Message";
//
// Message
//
this.Message.Location = new System.Drawing.Point(8, 112);
this.Message.Name = "Message";
this.Message.Size = new System.Drawing.Size(392, 176);
this.Message.TabIndex = 6;
this.Message.Text = "";
this.Message.WordWrap = false;
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 328);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(100, 16);
this.label6.TabIndex = 15;
this.label6.Text = "Status";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "POP Server";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 40);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "User";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 64);
this.label3.Name = "label3";
this.label3.TabIndex = 2;
this.label3.Text = "Password";
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 296);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(80, 16);
this.label5.TabIndex = 11;
this.label5.Text = "Mail Number";
//
// ConnectBtn
//
this.ConnectBtn.Location = new System.Drawing.Point(328, 16);
this.ConnectBtn.Name = "ConnectBtn";
this.ConnectBtn.TabIndex = 8;
this.ConnectBtn.Text = "Connect";
this.ConnectBtn.Click += new System.EventHandler(this.ConnectBtn_Click);
//
// POPServ
//
this.POPServ.Location = new System.Drawing.Point(80, 16);
this.POPServ.Name = "POPServ";
this.POPServ.Size = new System.Drawing.Size(224, 20);
this.POPServ.TabIndex = 3;
this.POPServ.Text = "pop.server.com";
//
// User
//
this.User.Location = new System.Drawing.Point(80, 40);
this.User.Name = "User";
this.User.Size = new System.Drawing.Size(224, 20);
this.User.TabIndex = 4;
this.User.Text = "user";
//
// DisconnectBtn
//
this.DisconnectBtn.Enabled = false;
this.DisconnectBtn.Location = new System.Drawing.Point(328, 48);
this.DisconnectBtn.Name = "DisconnectBtn";
this.DisconnectBtn.TabIndex = 9;
this.DisconnectBtn.Text = "Disconnect";
this.DisconnectBtn.Click += new System.EventHandler(this.DisconnectBtn_Click);
//
// Passw
//
this.Passw.Location = new System.Drawing.Point(80, 64);
this.Passw.Name = "Passw";
this.Passw.PasswordChar = '*';
this.Passw.Size = new System.Drawing.Size(224, 20);
this.Passw.TabIndex = 5;
this.Passw.Text = "password";
//
// RetrieveBtn
//
this.RetrieveBtn.Enabled = false;
this.RetrieveBtn.Location = new System.Drawing.Point(152, 296);
this.RetrieveBtn.Name = "RetrieveBtn";
this.RetrieveBtn.TabIndex = 10;
this.RetrieveBtn.Text = "Retrieve";
this.RetrieveBtn.Click += new System.EventHandler(this.RetrieveBtn_Click);
//
// POPForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(408, 405);
this.Controls.Add(this.Status);
this.Controls.Add(this.label6);
this.Controls.Add(this.Number);
this.Controls.Add(this.label5);
this.Controls.Add(this.RetrieveBtn);
this.Controls.Add(this.DisconnectBtn);
this.Controls.Add(this.ConnectBtn);
this.Controls.Add(this.label4);
this.Controls.Add(this.Message);
this.Controls.Add(this.Passw);
this.Controls.Add(this.User);
this.Controls.Add(this.POPServ);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Name = "POPForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Download Mail via POP3";
this.ResumeLayout(false);
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new POPForm());
}

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);
Status.Items.Clear();

try
{
// initialization
NetStrm = Server.GetStream();
RdStrm= new StreamReader(Server.GetStream());
Status.Items.Add(RdStrm.ReadLine());

// Login Process
Data = "USER "+ User.Text+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.Items.Add(RdStrm.ReadLine());

Data = "PASS "+ Passw.Text+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.Items.Add(RdStrm.ReadLine());

// Send STAT command to get information ie: number of mail and size
Data = "STAT"+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.Items.Add(RdStrm.ReadLine());

// 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 DisconnectBtn_Click(object sender, System.EventArgs e)
{
// change cursor into wait cursor
Cursor cr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

// Send QUIT command to close session from POP server
Data = "QUIT"+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.Items.Add(RdStrm.ReadLine());

//close connection
NetStrm.Close();
RdStrm.Close();

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

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

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.ToCharArray());
NetStrm.Write(szData,0,szData.Length);

szTemp = RdStrm.ReadLine();
if(szTemp[0]!='-')
{

while(szTemp!=".")
{
Message.Text += szTemp+CRLF;
szTemp = RdStrm.ReadLine();
}
}
else
{
Status.Items.Add(szTemp);
}

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

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

}
public TcpClient Server;
public NetworkStream NetStrm;
public StreamReader RdStrm;
public string Data;
public byte[] szData;
public string CRLF = "\r\n";

}
}

Share this post


Link to post
Share on other sites
Dotnet 2.0 has two new namespaces System.Net.Mail and System.Net.Mime that may help you if you want to use 2.0. Sadly the libs seem focused on sending and not Pop3/Imap access (seems like it ought to be there in the Net lib with HTTP and FTP, but whatever).
It is not really that complicated to access a POP3 server manually it is just a pain because the protocol is so old and based on printable ascii.
to do it you just open up a socket to the server, give the handshake, pump the right string commands and read the string responses.

Here is something to look at.

And here is something that will probably hive you a headache.

I also found a couple of pay components searching around and a free one that was not available. Perhaps you will have better luck finding a free one.

Hope somehting here helps.

Share this post


Link to post
Share on other sites
Thanks for the help turnpast. I'll take a look into DotNet 2.0, as I'm still on v1.1 as of now.

What other alternatives are their to POP3 for reading e-mails? This is one area I haven't really explored, as I usually wonder around game develment more than app development.


8)

Share this post


Link to post
Share on other sites
Mail servers often provide IMAP (RFC) access. It is a more complicated mail retreval protocol that provides a bunch of features over POP3. There were a number of dotnet components listed here that supported POP3 and IMAP, but the majority of them appeared to be pay (maybe that is ok).

If you don't like POP3 or IMAP and you are in a server environment your could work directly with SMTP (RFC) but that is probably overkill.

Also: if you want to handle with attachments and/or html mail you are going to have to deal with MIME and it is probably worth a little research.

cheers.

Share this post


Link to post
Share on other sites
Thanks for all the great links and advice.

I'll have to further look into my options then. All I can say is, it's so nice programming in C# using managed code, makes things so much easier on my head.

:)

Share this post


Link to post
Share on other sites

This topic is 4725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this