Posts Tagged ‘IMAP’

Label message with Gmail label

Sunday, June 12th, 2011

Gmail treats labels as folders for the purposes of IMAP.

As such, labels can be modified using the standard IMAP commands, CreateFolder, RenameFolder, and DeleteFolder, that act on folders.

System labels, which are labels created by Gmail, are reserved and prefixed by “[Gmail]” or “[GoogleMail]” in the list of labels.

Use the XLIST command to get the entire list of labels for a mailbox.

The labels for a given message may be retrieved by using the X-GM-LABELS attribute with the FETCH command.

Labels may be added to a message using the STORE command in conjunction with the X-GM-LABELS attribute.

// C# version

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.gmail.com");
    imap.Login("pat@gmail.com", "password");

    imap.SelectInbox();
    long uid = imap.GetAll().Last();

    imap.GmailLabelMessageByUID(uid, "MyLabel");

    List<string> labels = imap.GmailGetLabelsByUID(uid);
    labels.ForEach(Console.WriteLine);

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

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.Login("pat@gmail.com", "password")

	imap.SelectInbox()
	Dim uid As Long = imap.GetAll().Last()

	imap.GmailLabelMessageByUID(uid, "MyLabel")

	Dim labels As List(Of String) = imap.GmailGetLabelsByUID(uid)
	labels.ForEach(Console.WriteLine)

	imap.Close()
End Using

You can learn more about this Gmail IMAP extension here:
http://code.google.com/apis/gmail/imap/#x-gm-labels

Get Gmail labels for specified messages

Sunday, June 12th, 2011

Gmail treats labels as folders for the purposes of IMAP.

As such, labels can be modified using the standard IMAP commands, CreateFolder, RenameFolder, and DeleteFolder, that act on folders.

System labels, which are labels created by Gmail, are reserved and prefixed by “[Gmail]” or “[GoogleMail]” in the list of labels.

Use the XLIST command to get the entire list of labels for a mailbox.

The labels for a given message may be retrieved by using the X-GM-LABELS attribute with the FETCH command.

For list of messages

// C# version

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.gmail.com");
    imap.Login("pat@gmail.com", "password");

    imap.SelectInbox();

    List<long> uids = imap.GetAll();
    List<MessageInfo> infos = imap.GetMessageInfoByUID(uids);

    foreach (MessageInfo info in infos)
    {
        List<string> labels = info.GmailLabels.Labels;

        Console.WriteLine("[{0}] {1}",
            string.Join(" ", labels.ToArray()),
            info.Envelope.Subject);
    }

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

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.Login("pat@gmail.com", "password")

	imap.SelectInbox()

	Dim uids As List(Of Long) = imap.GetAll()
	Dim infos As List(Of MessageInfo) = imap.GetMessageInfoByUID(uids)

	For Each info As MessageInfo In infos
            Dim labels As List(Of String) = info.GmailLabels.Labels

	    Console.WriteLine("[{0}] {1}",  _
                String.Join(" ", labels.ToArray()),  _
                info.Envelope.Subject)
	Next

	imap.Close()
End Using

For single message:

// C# version

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.gmail.com");
    imap.Login("pat@gmail.com", "password");

    imap.SelectInbox();

    long uid = imap.GetAll().Last();
    List<string> labels = imap.GmailGetLabelsByUID(uid);

    labels.ForEach(Console.WriteLine);

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

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.Login("pat@gmail.com", "password")

	imap.SelectInbox()

	Dim uid As Long = imap.GetAll().Last()
	Dim labels As List(Of String) = imap.GmailGetLabelsByUID(uid)

	labels.ForEach(Console.WriteLine)

	imap.Close()
End Using

You can learn more about this Gmail IMAP extension here:
http://code.google.com/apis/gmail/imap/#x-gm-labels

Search Gmail using Gmail’s search syntax

Sunday, June 12th, 2011

To provide access to the full Gmail search syntax, Gmail provides the X-GM-RAW search attribute.

Arguments passed along with the X-GM-RAW attribute when executing the SEARCH command will be interpreted in the same manner as in the Gmail web interface.

// C# version

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.gmail.com");
    imap.Login("pat@gmail.com", "password");

    imap.SelectInbox();

    List<long> uids = imap.Search().Where(
        Expression.GmailRawSearch(
            "in:inbox subject:Notification with -without"));

    foreach (MessageInfo info in imap.GetMessageInfoByUID(uids))
    {
        Console.WriteLine("{0} - {1}",
            info.Envelope.GmailThreadId,
            info.Envelope.Subject);
    }

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

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.Login("pat@gmail.com", "password")

	imap.SelectInbox()

	Dim uids As List(Of Long) = imap.Search().Where( _
            Expression.GmailRawSearch( _
                "in:inbox subject:Notification with -without"))

	For Each info As MessageInfo In imap.GetMessageInfoByUID(uids)
	    Console.WriteLine("{0} - {1}",  _
                info.Envelope.GmailThreadId,  _
                info.Envelope.Subject)
	Next

	imap.Close()
End Using

You can learn more about this Gmail IMAP extension here:
http://code.google.com/apis/gmail/imap/#x-gm-raw

Search Gmail message id

Sunday, June 12th, 2011

Gmail provides a unique message ID for each email so that a unique message may be identified across multiple folders.

Retrieval of this message ID is supported via the X-GM-MSGID attribute on the FETCH command.

The message ID is a 64-bit unsigned integer.

The X-GM-MSGID attribute may also be used in the SEARCH command to find the UID of a message given Gmail’s message ID.

// C# version

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.gmail.com");
    imap.Login("pat@gmail.com", "password");

    imap.SelectInbox();

    long newestMessageUID = imap.GetAll().Last();
    var messageId = imap.GetEnvelopeByUID(newestMessageUID)
        .GmailMessageId;

    List<long> uids = imap.Search().Where(
        Expression.GmailMessageId(messageId));

    long uid =  uids.First();

    MessageInfo info = imap.GetMessageInfoByUID(uid);

    Console.WriteLine("{0} - {1}",
        info.Envelope.GmailMessageId,
        info.Envelope.Subject);

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

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.Login("pat@gmail.com", "password")

	imap.SelectInbox()

	Dim newestMessageUID As Long = imap.GetAll().Last()
	Dim messageId = imap.GetEnvelopeByUID(newestMessageUID).GmailMessageId

	Dim uids As List(Of Long) = imap.Search().Where(Expression.GmailMessageId(messageId))

	Dim uid As Long = uids.First()

	Dim info As MessageInfo = imap.GetMessageInfoByUID(uid)

	Console.WriteLine("{0} - {1}", _
            info.Envelope.GmailMessageId, _
            info.Envelope.Subject)

	imap.Close()
End Using

You can learn more about this Gmail IMAP extension here:
http://code.google.com/apis/gmail/imap/#x-gm-msgid

Search Gmail thread id

Sunday, June 12th, 2011

Gmail provides a thread ID to associate groups of messages in the same manner as in the Gmail web interface.

Retrieval of this thread ID is supported via the X-GM-THRID attribute on the FETCH command.

The thread ID is a 64-bit unsigned integer.

The X-GM-THRID attribute may also be used in the SEARCH command to find the UIDs of messages in a given thread.

// C# version

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.gmail.com");
    imap.Login("pat@gmail.com", "password");

    imap.SelectInbox();

    long newestMessageUID = imap.GetAll().Last();
    var threadId = imap.GetEnvelopeByUID(newestMessageUID)
        .GmailThreadId;

    List<long> uids = imap.Search().Where(
        Expression.GmailThreadId(threadId));

    foreach (MessageInfo info in imap.GetMessageInfoByUID(uids))
    {
        Console.WriteLine("{0} - {1}",
            info.Envelope.GmailThreadId,
            info.Envelope.Subject);
    }

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

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.Login("pat@gmail.com", "password")

	imap.SelectInbox()

	Dim newestMessageUID As Long = imap.GetAll().Last()
	Dim threadId = imap.GetEnvelopeByUID(newestMessageUID).GmailThreadId

	Dim uids As List(Of Long) = imap.Search().Where(Expression.GmailThreadId(threadId))

	For Each info As MessageInfo In imap.GetMessageInfoByUID(uids)
	    Console.WriteLine("{0} - {1}", _
                info.Envelope.GmailThreadId, _
                info.Envelope.Subject)
	Next

	imap.Close()
End Using

You can learn more about this Gmail IMAP extension here:
code.google.com/apis/gmail/imap/#x-gm-thrid