Posts Tagged ‘POP3’

POP3 vs IMAP

Thursday, August 5th, 2010

POP3 and IMAP are application-layer Internet standard protocols used by local e-mail clients to retrieve e-mails from a remote server over a TCP/IP connection. Virtually all modern e-mail clients and mail servers support both protocols as a means of transferring e-mail messages from a server.

They are both text-based. Both transmit more or less same amount of data.

Here is the brief comparison of both:

POP3 IMAP
Intended message store Client Server
Download message YES YES
Download message headers YES YES
Download specific message part (single attachment) NO YES
Delete message YES YES
Send message NO (use SMTP) NO (use SMTP)
Get only unseen messages NO YES
Mark message Seen/Unseen NO YES
Server side search NO YES
Folders NO YES
Sent items NO YES
SSL YES YES
Push email NO YES

Mail.dll supports both POP3 and IMAP (and SMTP), give it a try at:
http://www.lesnikowski.com/mail/

The handshake failed due to an unexpected packet format

Monday, July 26th, 2010

Most likely your server requires implicit SSL. So first try to connect without SSL:

// C#

client.Connect("imap.example.com");
' VB.NET

client.Connect("imap.example.com");

Then, before logging-in, start implicit SSL negotiation. The command name differs for different protocols:

In case of IMAP:

// C#

client.StartTLS();
' VB.NET

client.StartTLS()

In case of POP3:

// C#

client.STLS();
' VB.NET

client.STLS()

In case of SMTP:

// C#

client.StartTLS();
' VB.NET

client.StartTLS()

Now, your connection is secured.

Remember that you can ignore SSL certificate errors using ServerCertificateValidate event:

// C#

client.ServerCertificateValidate +=
    (sender, e) => { e.IsValid = true; };
' VB.NET

client.ServerCertificateValidate += Function(sender, e) Do
	e.IsValid = True
End Function

Download email attachments in .NET

Monday, January 25th, 2010

First you’ll need an IMAP client or POP3 client to download emails from the server.

The email attachments are downloaded as a part of the message. Attachments are stored within the email as part of a mime tree. Usually Quoted-Printable or Base64 encoding are used. Mail.dll is going to parse such tree for you and expose all attachments as well known .NET collections.

IMail uses 3 collections for storing attachments:

  • Attachments - contains all attached documents
  • Visuals - contains files that should be ‘displayed’ to the user (like images or music that should be played in background)
  • NonVisuals - contains attachments that should not be ‘displayed’ immediately

Following you’ll find samples of how you can save all attachments to disk using C# and VB.NET via POP3 and IMAP protocols.

When you use IMAP server:

// C# version
using(Imap imap = new Imap())
{
	imap.Connect("server");
	imap.Login("user", "password");

	imap.SelectInbox();
	List<long> uids = imap.SearchFlag(Flag.All);
	foreach (long uid in uids)
	{
		string eml = imap.GetMessageByUID(uid);
		IMail email = new MailBuilder()
			.CreateFromEml(eml);

		Console.WriteLine(email.Subject);

		// save all attachments to disk
		email.Attachments.ForEach(mime => mime.Save(mime.FileName));
	}
	imap.Close(true);
}

You can also save attachment to stream: void MimeData.Save(Stream stream)
Or get direct access to it: MemoryStream MimeData.GetMemoryStream()

' VB.NET version
Using imap As New Imap()
	imap.Connect("server")
	imap.Login("user", "password")

	imap.SelectInbox()
	Dim uids As List(Of Long) = imap.SearchFlag(Flag.All)
	For Each uid As Long In uids
		Dim eml As String = imap.GetMessageByUID(uid)
		Dim email As IMail = New MailBuilder()_
			.CreateFromEml(eml)

		Console.WriteLine(email.Subject)

		' save all attachments to disk
		email.Attachments.ForEach(Function(mime) mime.Save(mime.FileName))
	Next
	imap.Close(True)
End Using

When you use POP3 server:

// C# version
using(Pop3 pop3 = new Pop3())
{
	pop3.Connect("server");
	pop3.Login("user", "password");

	foreach (string uid in pop3.GetAll())
	{
		IMail email = new MailBuilder()
			.CreateFromEml(pop3.GetMessageByUID(uid));

		Console.WriteLine(email.Subject);

		// save all attachments to disk
		email.Attachments.ForEach(mime => mime.Save(mime.FileName));
	}
	pop3.Close(true);
}
' VB.NET version
Using pop3 As New Pop3()
	pop3.Connect("server")
	pop3.Login("user", "password")

	For Each uid As String In pop3.GetAll()
		Dim email As IMail = New MailBuilder() _
      			.CreateFromEml(pop3.GetMessageByUID(uid))

		Console.WriteLine(email.Subject)

		' save all attachments to disk
		email.Attachments.ForEach(Function(mime) mime.Save(mime.FileName))
	Next
	pop3.Close(True)
End Using

Save raw eml file using IMAP and POP3

Monday, December 7th, 2009

mailThere are times that you need to save raw eml format of the email (for example when you want to report a bug).

This tutorial shows how to use Mail.dll POP3 and IMAP clients to achieve this goal.

Notice that we are not parsing the emails so we don’t need to use MailBuilder and IMail classes.

Samples provided use both POP3 and IMAP protocols using C# and VB.NET

IMAP version:

The code below will write all emails with specified subject to your c:\ drive:

// C# code

using (Imap imap = new Imap())
{
    imap.Connect("server");
    // imap.ConnectSSL("server"); // if you need SSL connection.
    imap.Login("user", "password");

    imap.SelectInbox();

    List<long> uids = imap.Search(
        Expression.Subject("email subject"));

    foreach (long uid in uids)
    {
        string eml = imap.GetMessageByUID(uid);
        string fileName = string.Format("c:\\email_{0}.eml", uid);
        File.WriteAllText(fileName, eml);
    }
    imap.Close(true);
}
' VB.NET code

Using imap As New Imap()
	imap.Connect("server")
	' imap.ConnectSSL("server") ' if you need SSL connection.
	imap.Login("user", "password")

	imap.SelectInbox()

	Dim uids As List(Of Long) = imap.Search( _
		Expression.Subject("email subject"))

	For Each uid As Long In uids
		Dim eml As String = imap.GetMessageByUID(uid)
		Dim fileName As String = String.Format("c:\\email_{0}.eml", uid)
		File.WriteAllText(fileName, eml)
	Next
	imap.Close(True)
End Using

POP3 version:

As POP3 does not allow searching, the code below will write all emails to your c:\ drive:

// C# code

using (Pop3 pop3 = new Pop3())
{
    pop3.Connect("server");
    // pop3.ConnectSSL("server"); // if you need SSL connection.
    pop3.Login("user", "password");

    foreach (string uid in pop3.GetAll())
    {
        string eml = pop3.GetMessageByUID(uid));
        string fileName = string.Format("c:\\email_{0}.eml", uid);
        File.WriteAllText(fileName, eml);
    }
    pop3.Close(true);
}
' VB.NET code

Using pop3 As New Pop3()
	pop3.Connect("server")
	' pop3.ConnectSSL("server"); ' if you need SSL connection.
	pop3.Login("user", "password")

  For Each uid As String In pop3.GetAll()
		Dim eml As String = pop3.GetMessageByUID(uid)
		Dim fileName As String = String.Format("c:\\email_{0}.eml", uid)
		File.WriteAllText(fileName, eml)
	Next
	pop3.Close(True)
End Using

You can download Mail.dll at: Mail.dll .NET email component

Download emails from GMail via POP3

Tuesday, December 1st, 2009

gmail
Accessing Gmail with POP3 is a bit different that accessing other POP3 servers. Still is quite easy with Mail.dll IMAP client. Remember that POP3 protocol unlike IMAP does not store the unseen information on the server.

Mail.dll has of course IMAP support

First we need to make sure that POP3 access is enabled for your GMail account.

  1. Go to GMail settings:

    gmail_settings

  2. Then select ‘Forwarding and POP/IMAP’ tab:

    gmail_settings_tab

  3. And check one of the ‘Enable …’ options in ‘IMAP Access’ section:

    gmail_settings_pop3

  4. GMail behaves differently than other POP3 servers and its POP3 configuration may be sometimes confusing.
    Luckily you can configure what you expect from it. Here’s the value list of drop down list called “When messages are accessed with POP” and the Gmail’s behavior:

    1. “delete Gmail’s copy”: Message is deleted by issuing GetMessage command.
    2. “keep Gmail’s copy in the Inbox”: Message stays in the Inbox, Web interface says it’s unread, however message is not received for the second time using POP3 client.
    3. “archive Gmail’s copy”: Message is deleted by issuing GetMessage command, however it is possible to find it using web interface.
  5. Now you are able to connect to your GMail account with Mail.dll POP3 client.

If you want to have a greater control over your mail you should use
Mail.dll .NET IMAP client. You can easily move messages between folders (IMAP calls them mailboxes) and flag them.

Remember that GMail only allows secure SSL connections so we need to use ConnectSSL method.

// C# code:

using(Pop3 pop3 = new Pop3())
{
	pop3.ConnectSSL("pop.gmail.com");
	pop3.Login("your_email@gmail.com", "password");

	foreach (string uid in pop3.GetAll())
	{
		string eml = pop3.GetMessageByUID(uid);
		IMail mail= new MailBuilder()
			.CreateFromEml(eml);

		Console.WriteLine(mail.Subject);
		Console.WriteLine(mail.TextDataString);
	}
	pop3.Close(false);
}
' VB.NET code:

Using pop3 As New Pop3()
	pop3.ConnectSSL("pop.gmail.com")
	pop3.Login("your_email@gmail.com", "password")

  For Each uid As String In pop3.GetAll()
    Dim email As IMail = New MailBuilder() _
        .CreateFromEml(pop3.GetMessageByUID(uid))
    Console.WriteLine(email.Subject)
		Console.WriteLine(mail.TextDataString)
	Next
	pop3.Close(False)
End Using

If you like the idea of simple GMail access just give it a try for yourself and download it at: Mail.dll .NET email component