Download Gmail Chat Logs via IMAP

You can download all Gmail and Google Talk conversations using IMAP protocol.

Gmail can turn your instant messaging conversations into threads of emails — available conveniently for download anywhere via IMAP.

To access and export Gmail and Google Talk chat logs make sure IMAP access is enabled for your Gmail account.

Make also sure Show in IMAP is checked for Chats under System Labels.
Gmail-chats

All chat logs are stored inside the “[Gmail]/Chats” folder.

Each email message stored in that folder contains one conversation, stored as HTML (available through IMail.HTML property) and XML (available through IMail.Visuals attachment property). Of course XML is much more interesting from the processing point of view and you can use XDocument class to parse it. Each conversation consists of multiple messages.

// C#

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

    imap.Select("[Gmail]/Chats");

    IEnumerable<long> firstFive = imap.GetAll().Take(5);
    foreach (long uid in firstFive)
    {
        IMail email = new MailBuilder().CreateFromEml(
            imap.GetMessageByUID(uid));

        MimeText xml = (MimeText)email.Visuals[0];
        XDocument document = XDocument.Parse(xml.Text);

        XNamespace con = XNamespace.Get("google:archive:conversation");
        XNamespace cli = XNamespace.Get("jabber:client");

        XElement conversation = document.Element(con + "conversation");
        foreach (XElement message in conversation.Elements(cli + "message"))
        {
            XElement body = message.Element(cli + "body");

            Console.WriteLine("{0} -> {1}: {2}",
                message.Attribute("from").Value,
                message.Attribute("to").Value,
                body.Value);
        }
    }
    imap.Close();
}
' VB.NET

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

    imap.Select("[Gmail]/Chats")

    Dim firstFive As IEnumerable(Of Long) = imap.GetAll().Take(5)
    For Each uid As Long In firstFive
        Dim email As IMail = New MailBuilder().CreateFromEml( _
            imap.GetMessageByUID(uid))

        Dim xml As MimeText = DirectCast(email.Visuals(0), MimeText)
	Dim document As XDocument = XDocument.Parse(xml.Text)

	Dim con As XNamespace = XNamespace.[Get]("google:archive:conversation")
	Dim cli As XNamespace = XNamespace.[Get]("jabber:client")

	Dim conversation As XElement = document.Element(con + "conversation")
	For Each message As XElement In conversation.Elements(cli + "message")
	    Dim body As XElement = message.Element(cli + "body")

            Console.WriteLine("{0} -> {1}: {2}", _
                message.Attribute("from").Value,  _
                message.Attribute("to").Value,  _
                body.Value)
        Next
    Next
    imap.Close()
End Using

Tags:     

Questions?

Consider using our Q&A forum for asking questions.