Posts Tagged ‘email’

Download parts of email message

Thursday, July 8th, 2010

Some times you know you’ll receive large emails, but you only need to access some parts without downloading entire messages .

Mail.dll .NET IMAP client allows you to download only needed parts of the specified message.

First you need to get the structure of the message.
There are two methods for that: GetBodyStructureByUID and GetBodyStructure.

BodyStructure contains information about plain text, html, and all attachments of the message. It does not contain any data though.

To download text parts of the email (like html or plain text) you can use: GetMimePartTextByUID or GetMimePartText.

To download attachments use: GetMimePartByUID or GetMimePart.

Here’s the full sample for this feature:

// C#

using(Imap imap = new Imap())
{
	imap.Connect("imap.server.com");
	imap.Login("user", "password");

	imap.SelectInbox();
	List<long> uidList = imap.SearchFlag(Flag.Unseen);
	foreach (long uid in uidList)
	{
		// Get the structure of the email
		BodyStructure structure = imap.GetBodyStructureByUID(uid);

		// Download only text and html parts
		string text = imap.GetMimePartTextByUID(uid, structure.Text);
		string html = imap.GetMimePartTextByUID(uid, structure.Html);

		Console.WriteLine(text);
		Console.WriteLine(html);

		// Show all attachments' filenames
		foreach(MimeStructure attachment in structure.Attachments)
		{
			Console.WriteLine(attachment.FileName);
			// You can also download entire attachment
			byte[] bytes = imap.GetMimePartByUID(uid, attachment);
		}
	}
	imap.Close(true);
}
' VB.NET

Using imap As New Imap()
	imap.Connect("imap.server.com")
	imap.Login("user", "password")

	imap.SelectInbox()
	Dim uidList As List(Of Long) = imap.SearchFlag(Flag.Unseen)
	For Each uid As Long In uidList
		' Get the structure of the email
		Dim struct As BodyStructure = imap.GetBodyStructureByUID(uid)

		' Download only text and html parts
		Dim text As String = imap.GetMimePartTextByUID(
			uid, struct.Text)
		Dim html As String = imap.GetMimePartTextByUID(
			uid, struct.Html)

		Console.WriteLine(text)
		Console.WriteLine(html)

		' Show all attachments' filenames
		For Each attachment As MimeStructure In struct.Attachments
			Console.WriteLine(attachment.FileName)
			' You can also download entire attachment
			Dim bytes As Byte() = imap.GetMimePartByUID(
				uid, attachment)
		Next
	Next
	imap.Close(True)
End Using

IMAP: LIST, XLIST and LSUB

Thursday, June 24th, 2010

In IMAP protocol there are two commands to retrieve a folder list from the server.
Most servers use LIST command, some servers support XLIST command (GMail does).

XLIST provides additional folder flags such as \Inbox or \Spam (which are very useful if folder names are localized).

You can read more about XLIST here:
http://www.lesnikowski.com/blog/index.php/localized-gmail-imap-folders/

Unfortunately when you ask for subscribed folders with LSUB , those additional flags are not send by the server. There is no such thing as XLSUB command.

The only way to workaround this limitation is to first download all folders with flags using XLIST, then use LSUB to get subscribed folders and finally match the flags by name.

Email template engine

Friday, February 19th, 2010

Newest version of Mail.dll email client includes a easy to use template engine.

It allows you to easily create html template for your emails:

Loading and rendering such template requires one line of code:

Contact contact = ...;

 string html = Template
     .FromFile("template.txt")
     .DataFrom(contact)
     .Render();

This is how the template looks like:

<html>
<head>
    <title>Your order</title>
</head>
<body>
	Hi [FirstName] [LastName],
	<br />
	<p>
	Your account [if Verified]has[else]<strong>has not</strong>[end] been verified. <br/>
	Your password is: [Password].
	</p>
	<p>
	Here are your orders:
	</p>
	[foreach Orders]
		<p>
		Order sent to <strong>[Street]</strong>:
		</p>
		<table style="width: 30%;">
			[foreach Items]
				<tr style="background-color: #E0ECFF;">
					<td>[Name]</td><td>[Price]</td>
				</tr>
			[end]
		</table>
	[end]
	<p>
		Thank you for your orders.
	<p>
</body>
</html>

Here’s the sample of how to load, fill the template and send it using Mail.dll:

Mail.Html(Template
              .FromFile("template.txt")
              .DataFrom(_contact)
              .Render())
    .Text("This is text version of the message.")
    .From(new MailBox("alice@mail.com", "Alice"))
    .To(new MailBox("bob@mail.com", "Bob"))
    .Subject("Your order")
    .UsingNewSmtp()
    .WithCredentials("alice@mail.com", "password")
    .Server("mail.com")
    .WithSSL()
    .Send();

And this is how the data used by template look like:

public class Contact
{
    public List<Order> Orders { get; private set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Verified;
    private string Password { get; set; }

    public Contact()
    {
        Orders = new List<Order>();
    }
} ;