Archive for the ‘Component’ Category

New Barcode.dll tutorial videos

Friday, August 13th, 2010

New Barcode.dll tutorial videos are out. You can watch them here:

Barcode.dll ASP.NET tutorial video Barcode.dll ASP.NET tutorial video.

Barcode.dll Windows Forms tutorial video Barcode.dll Windows Forms tutorial video.

Barcode.dll Windows Forms printing tutorial video Barcode.dll Windows Forms printing tutorial video.

Sign emails with DKIM

Tuesday, July 27th, 2010

DKIM is short for DomainKeys Identified Mail.

Adding a signature looks like this:

// C#

using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
   IMail email = Mail
       .Text("text")
       .From("alice@mail.com")
       .To("bob@mail.com")
       .Subject("subject")
       .DKIMSign(rsa, "brisbane", "example.com")
       .Create();
}
' VB.NET

Using rsa As New RSACryptoServiceProvider()
	Dim email As IMail = Mail _
		.Text("text") _
		.From("alice@mail.com") _
		.[To]("bob@mail.com") _
		.Subject("subject") _
		.DKIMSign(rsa, "brisbane", "example.com") _
		.Create()
End Using

So what you need is RSACryptoServiceProvider with your private key, and two strings: selector and domain.

Basically how this works is the recipient of the email queries the DNS server for TXT record for selector._domainkey.domain (in our sample it is: “brisbane._domainkey.example.com”) to get the public key and validate the signature.

You can use nslookup to get the public key for a domain:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Pawel>nslookup
Default Server:  UnKnown
Address:  192.168.0.1

> set type=TXT
> gamma._domainkey.gmail.com
Server:  UnKnown
Address:  192.168.0.1

Non-authoritative answer:
gamma._domainkey.gmail.com      text =

        "k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrI
Ve9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBD
jiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB"

gmail.com       nameserver = ns1.google.com
gmail.com       nameserver = ns4.google.com
gmail.com       nameserver = ns3.google.com
gmail.com       nameserver = ns2.google.com
ns1.google.com  internet address = 216.239.32.10
ns2.google.com  internet address = 216.239.34.10
ns3.google.com  internet address = 216.239.36.10
ns4.google.com  internet address = 216.239.38.10
>

But don’t worry as Mail.dll will make this DNS query for you.

Validating (including DNS query for public key) is simple:

// C#

IMail email = new MailBuilder()
    .CreateFromEmlFile("signed_gamma.gmail.eml");
bool isValid = email.CheckDKIMSignature();
' VB.NET

Dim email As IMail = New MailBuilder() _
    .CreateFromEmlFile("signed_gamma.gmail.eml")
Dim isValid As Boolean = email.CheckDKIMSignature()

You can download Mail.dll .NET email component here.

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 parts of email message

Thursday, July 8th, 2010

Some times you know you’ll receive large emails, and 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

Peek message on IMAP server

Tuesday, July 6th, 2010

When you access messages stored on IMAP server, it automatically adds \Seen flag to the messages you have downloaded.

If you don’t want this behavior, there are two things you can do.

First is to use Examine method instead of Select:

// C#

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

    imap.ExamineInbox(); // -or- imap.Examine("Inbox");

    // ...

    imap.Close(true);
}
' VB.NET

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

    imap.SelectInbox()

    imap.ExamineInbox() ' -or- imap.Examine("Inbox")

    ' ...

    imap.Close(True)
End Using

Examine puts mailbox (also called folder) into read-only state, so no \Seen flag is added.
The drawback is that you can not delete a message.

Second approach is to use one of the Peek methods on the IMAP client:

// 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)
    {
        IMail email = new MailBuilder()
            .CreateFromEml(imap.PeekMessageByUID(uid));
        Console.WriteLine(email.Subject);
    }
    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
        Dim email As IMail = New MailBuilder() _
            .CreateFromEml(imap.PeekMessageByUID(uid))
        Console.WriteLine(email.Subject)
    Next
    imap.Close(True)
End Using

There are Peek methods for downloading headers and even parts of the email message.

You can download Mail.dll IMAP client here.