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.
Posted in Component | Tags: C#, DKIM, Mail.dll, VB.NET | No Comments »
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
Posted in Component | Tags: C#, IMAP, Mail.dll, POP3, SMTP, VB.NET | No Comments »
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
Posted in Component | Tags: C#, email, IMAP, Mail.dll, VB.NET | No Comments »
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.
Posted in Component | Tags: C#, IMAP, VB.NET | 1 Comment »
June 28th, 2010
OAuth is an open protocol to allow secure API authorization in a simple and standard method from desktop and web applications.
In this post I’ll show how to access GMail account using OAuth authentication method. The key advantage of this method is that it allows an application to access users email without knowing user’s password.
You can read more on OAuth authentication with Google accounts here:
http://code.google.com/apis/accounts/docs/OAuth_ref.html
Gmail IMAP and SMTP using OAuth:
http://code.google.com/apis/gmail/oauth/protocol.html
If your application is not registered, please select HMAC-SHA1 and use the following key and secret:
consumer key: “anonymous”
consumer secret: “anonymous”
You can manage your domains here:
https://www.google.com/accounts/ManageDomains
The following code makes several HTTP requests to authenticate your application. It also fires up the web browser, so the user can allow or deny the application to access his emails.
const string userEmailAccount = "alice@gmail.com";
const string consumerKey = "anonymous";
const string consumerSecret = "anonymous";
// First: get request token
ParameterList parameters1 = OAuth.ForUrl(
"https://www.google.com/accounts/OAuthGetRequestToken")
.Consumer(consumerKey, consumerSecret)
.AddParameter("scope", "https://mail.google.com/")
.AddParameter(OAuthParameterName.OAuthCallback, "oob")
.Sign()
.ExecuteWebRequest();
// Second: user interaction
string url2 = OAuth.ForUrl(
"https://www.google.com/accounts/OAuthAuthorizeToken")
.Consumer(consumerKey, consumerSecret)
.Token(parameters1.GetValue(OAuthParameterName.OAuthToken))
.TokenSecret(parameters1.GetValue(OAuthParameterName.OAuthTokenSecret))
.Sign()
.GetUrl();
// Fire up the browser
Process.Start(url2);
Console.WriteLine("Please enter the key: ");
string key = Console.ReadLine().Trim();
// Third: get access token
ParameterList parameters3 = OAuth.ForUrl(
"https://www.google.com/accounts/OAuthGetAccessToken")
.Consumer(consumerKey, consumerSecret)
.Token(parameters1.GetValue(OAuthParameterName.OAuthToken))
.TokenSecret(parameters1.GetValue(OAuthParameterName.OAuthTokenSecret))
.AddParameter("oauth_verifier", key)
.Sign()
.ExecuteWebRequest();
// Log-in to IMAP server using XOAuth
using (Imap client = new Imap())
{
client.ConnectSSL(TestConstants.GmailImapServer);
string imapUrl = string.Format(
"https://mail.google.com/mail/b/{0}/imap/", userEmailAccount);
string oauthImapKey = OAuth.ForUrl(imapUrl)
.Consumer(consumerKey, consumerSecret)
.Token(parameters3.GetValue(OAuthParameterName.OAuthToken))
.TokenSecret(parameters3.GetValue(OAuthParameterName.OAuthTokenSecret))
.Sign()
.GetXOAuthKey();
client.LoginOAUTH(oauthImapKey);
// Now you can access user's emails.
client.Close(true);
}
Posted in Component | Tags: GMail, IMAP, Mail.dll, OAuth | No Comments »