Posts Tagged ‘POP3 component’

Tried to read a line. Only ” received.

Friday, September 27th, 2013

Mail.dll

If you are using Mail.dll:

Please make sure that you have disabled antivirus and firewall software or that you have configured them correctly. This includes Windows Defender – especially if you use SMTP client.

Many reports show that AVG antivirus tracks IMAP sessions and interrupts them with no reason.

In most cases this error means that the connection was interrupted. It was lost, the server disconnected or your antivirus/firewall cut the connection. For instance: SMTP server may be configured to disconnect as soon as it decides your message is spam or when you are using incorrect from address.

In technical terms, it means exactly what the exception’s message says: component tried to read a line (ending with CRLF), the line was expected in accordance to the protocol in use, but it has not been received during the specified period.

On extremely slow networks you may increase timeout values: ReceiveTimeout and SendTimeout.

If increasing timeout values doesn’t help, it means that the connection was dropped by the remote server or some intermediary router – you’ll need to connect again.

If you are using *Imap.Idle* you can use lower IDLE timeout. It specifies timeout after which IDLE command is re-issued. This breaks inactivity period (which happens when no new message is delivered) and thus prevents routers from cutting the connection.

MailForWindowsStore.dll

If you are using MailForWindowsStore.dll: Connect and ConnectSSL methods are asynchronous. You must use await ConnectSSL and await Connect:

// C#

using(Imap imap = new Imap())
{
    await client.ConnectSSL("imap.example.com");
    await client.UseBestLoginAsync("user", "password");
    // ...
}   
' VB.NET

Using imap As New Imap()
    Await client.ConnectSSL("imap.example.com")
    Await client.UseBestLoginAsync("user", "password")
    ' ...
End Using

All points in Mail.dll section apply as well.

Save raw eml file using IMAP and POP3

Wednesday, December 7th, 2011

There are times that you need to save raw email data in eml format.

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

Note that we don’t need to parse the emails, so we aren’t using MailBuilder class and IMail interface.

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

Save email using IMAP:

The code below will search for all the emails with specified subject and write all these emails to your c: drive.

// C# code

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com");    // use ConnectSSL for SSL connection.
    imap.UseBestLogin("user", "password");

    imap.SelectInbox();

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

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

Using imap As New Imap()
	imap.Connect("imap.example.com")    ' use ConnectSSL for SSL connection.
	imap.UseBestLogin("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 = imap.GetMessageByUID(uid)
		Dim fileName As String = String.Format(@"c:\email_{0}.eml", uid)
		File.WriteAllBytes(fileName, eml)
	Next
	imap.Close()
End Using

Save email using POP3:

As POP3 protocol does not allow searching, the code below will write all emails to your c: drive. You can easily limit this to the specified uid.

// C# code

using (Pop3 pop3 = new Pop3())
{
    pop3.Connect("pop3.example.com");    // use ConnectSSL for SSL connection.
    pop3.UseBestLogin("user", "password");

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

Using pop3 As New Pop3()
	pop3.Connect("pop3.example.com")    'use ConnectSSL for SSL connection.
	pop3.UseBestLogin("user", "password")

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

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

Validate S/MIME emails

Monday, October 25th, 2010

In this article we’ll show how to verify digitally signed emails (S/MIME) using Mail.dll email component.

S/MIME (Secure/Multipurpose Internet Mail Extensions) is a standard for public key encryption and signing of MIME data.

S/MIME was originally developed by RSA Data Security. Specification uses Cryptographic Message Syntax, an IETF specification that is identical in most respects with PKCS #7. S/MIME provides the following cryptographic security services for electronic messaging applications: authentication, message integrity, non-repudiation of origin (using digital signatures), privacy and data security (using encryption).

S/MIME signatures are usually done with what’s called “detached signatures”. The signature information is separate from the text being signed. The MIME type for this is multipart/signed with the second part having a MIME subtype of application/(x-)pkcs7-signature.

Sometimes attached signatures (application/pkcs7-mime; smime-type=”signed-data”) format is used. In such case signature and data are represented by single MIME entity. Mail.dll recognizes both detached and attached signatures.

To check if the message has been signed use IsSigned property on IMail object.
CheckSignature(bool verifySignatureOnly) method is used for signature validation.

Using IMAP protocol

// C# 

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com"); // or ConnectSSL
    imap.UseBestLogin("user", "password");

    MailBuilder builder = new MailBuilder();
    foreach (long uid in imap.GetAll())
    {
        IMail email = builder.CreateFromEml(
            imap.GetMessageByUID(uid));

        // Check signature
        if (email.IsSigned == true)
            email.CheckSignature(true);
    }
    imap.Close();
}

' VB.NET

Using imap As New IMAP()
    imap.Connect("imap.example.com") ' or ConnectSSL
    imap.UseBestLogin("user", "password")

    Dim builder As New MailBuilder()
    For Each uid As Long In imap.GetAll()
        Dim email As IMail = builder.CreateFromEml( _
            imap.GetMessageByUID(uid))

        ' Check signature
        If email.IsSigned = True Then
            email.CheckSignature(True)
        End If
    Next
    imap.Close()
End Using

Using POP3 protocol

using (Pop3 pop3 = new Pop3())
{
    pop3.Connect("pop3.example.com"); // or ConnectSSL
    pop3.Login("user", "password");

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

        // Check signature
        if (email.IsSigned == true)
            email.CheckSignature(true);
    }
    pop3.Close();
}
Using pop3 As New Pop3()
    pop3.Connect("pop3.example.com") ' or ConnectSSL
    pop3.Login("user", "password")

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

        ' Check signature
        If email.IsSigned = True Then
            email.CheckSignature(True)
        End If
    Next
    pop3.Close()
End Using

CheckSignature method will throw an exception, if it fails to verify the signature.

Issue a custom command to POP3 server

Monday, October 25th, 2010

You can send any command to POP3 server using Mail.dll and receive servers’s response easy.

In this example we will issue LIST command. LIST command is used to check the email size without downloading it from the POP3 server.

There are 2 versions of the LIST command. First takes message number parameter:

LIST messageNumber

We are asking for specific message here, which means that we expect to receive single line response.

Remember that message numbers on POP3 servers start from 1.

// C# version

using System;
using Limilabs.Client.POP3;

class Program
{
    static void Main(string[] args)
    {
        using (Pop3 pop3 = new Pop3())
        {
            pop3.Connect("server.example.com");
            pop3.Login("user", "password");

            Pop3Response response = pop3.SendCommand("LIST " + 1);

            // We expect string in following format: "0 32768"
            string sizeInBytes = response.Message.Split(' ')[1];

            Console.WriteLine(sizeInBytes);
            pop3.Close();
        }
    }
};
' VB.NET version

Imports System
Imports Limilabs.Client.POP3

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using pop3 As New Pop3()
            pop3.Connect("server.example.com")
            pop3.Login("user", "password")

            Dim response As Pop3Response = pop3.SendCommand("LIST " + 1)

            ' We expect string in following format: "0 32768"
            Dim sizeInBytes As String = response.Message.Split(" "c)(1)

            Console.WriteLine(sizeInBytes)
            pop3.Close()
        End Using

    End Sub
End Module

Second version is LIST command without any arguments:

LIST

It gets the size of every message that is currently stored on the POP3 server – server replies with multi-line response:

// C# version

using System;
using Limilabs.Mail;
using Limilabs.Client.POP3;

class Program
{
    static void Main(string[] args)
    {
        using (Pop3 pop3 = new Pop3())
        {
            pop3.Connect("server.company.com");
            pop3.Login("user", "password");

            Pop3Response response = pop3.SendMultiLineCommand("LIST");

            foreach(string line in response.GetLines())
            {
                // We expect following format: "0 32768"
                string[] tmp = line.Split(' ');
                Console.WriteLine("Message number {0} is {1} bytes long.",
                                  tmp[0],
                                  tmp[1]);
            }
            pop3.Close();
        }
    }
};

' VB.NET version

Imports System
Imports Limilabs.Client.POP3

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using pop3 As New Pop3()
            pop3.Connect("server.company.com")
            pop3.Login("user", "password")

            Dim response As Pop3Response = pop3.SendMultiLineCommand("LIST")

            For Each line As String In response.GetLines()
                ' We expect following format: "0 32768"
                Dim tmp As String() = line.Split(" "c)
                Console.WriteLine("Message number {0} is {1} bytes long.", tmp(0), tmp(1))
            Next
            pop3.Close()
        End Using

    End Sub
End Module


Get email headers using POP3 (TOP command)

Monday, October 25th, 2010

The POP3 protocol’s TOP command is useful to download only headers of the big email message, without actually downloading entire message including all attachments. With Mail.dll POP3 component it is easy to use TOP command, you simply invoke one of the GetHeadersBy method.

// C# version

using System;
using Limilabs.Mail;
using Limilabs.Client.POP3;

class Program
{
    static void Main(string[] args)
    {
        using (Pop3 pop3 = new Pop3())
        {
            pop3.Connect("pop3.example.com");    // or ConnectSSL
            pop3.Login("user", "password");

            MailBuilder builder = new MailBuilder();
            foreach (string uid in pop3.GetAll())
            {
                var headers = pop3.GetHeadersByUID(uid);
                IMail email = builder.CreateFromEml(headers);

                Console.WriteLine("subject: {0}", email.Subject);
            }
            pop3.Close();
        }
    }
}
' VB.NET version

Imports System
Imports Limilabs.Mail
Imports Limilabs.Client.POP3

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using pop3 As New Pop3()
            pop3.Connect("pop3.example.com")    ' or ConnectSSL
            pop3.Login("user", "password")

            Dim builder As New MailBuilder()
            For Each uid As String In pop3.GetAll()
                Dim headers = pop3.GetHeadersByUID(uid)
                Dim email As IMail = builder.CreateFromEml(headers)

                Console.WriteLine("subject: {0}", email.Subject)
            Next
            pop3.Close()
        End Using

    End Sub
End Module