{"id":3990,"date":"2013-05-08T14:09:38","date_gmt":"2013-05-08T12:09:38","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=3990"},"modified":"2014-04-15T14:59:12","modified_gmt":"2014-04-15T12:59:12","slug":"download-gmail-chat-logs-via-imap","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/download-gmail-chat-logs-via-imap","title":{"rendered":"Download Gmail Chat Logs via IMAP"},"content":{"rendered":"<p>You can download all  Gmail and Google Talk conversations using IMAP protocol.<\/p>\n<p>Gmail can turn your instant messaging conversations into threads of emails \u2014 available conveniently for download anywhere via IMAP.<\/p>\n<p>To access and export Gmail and Google Talk chat logs make sure <a href=\"\/blog\/enable-imap-in-gmail\">IMAP access is enabled for your Gmail account<\/a>.<\/p>\n<p>Make also sure Show in IMAP is checked for Chats under System Labels.<br \/>\n<img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2013\/05\/Gmail-chats.png\" alt=\"Gmail-chats\" class=\"alignnone size-full\" \/><\/p>\n<p>All chat logs are stored inside the &#8220;[Gmail]\/Chats&#8221; folder.<\/p>\n<p>Each email message stored in that folder contains one conversation, stored as HTML (available through <em>IMail.HTML<\/em> property) and XML (available through <em>IMail.Visuals<\/em> attachment property). Of course XML is much more interesting from the processing point of view and you can use <em>XDocument <\/em> class to parse it. Each conversation consists of multiple messages.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing (Imap imap = new Imap())\r\n{\r\n    imap.ConnectSSL(&quot;imap.gmail.com&quot;);\r\n    imap.Login(&quot;user@gmail.com&quot;, &quot;password&quot;);\r\n\r\n    imap.Select(&quot;&#x5B;Gmail]\/Chats&quot;);\r\n\r\n    IEnumerable&lt;long&gt; firstFive = imap.GetAll().Take(5);\r\n    foreach (long uid in firstFive)\r\n    {\r\n        IMail email = new MailBuilder().CreateFromEml(\r\n            imap.GetMessageByUID(uid));\r\n\r\n        MimeText xml = (MimeText)email.Visuals&#x5B;0];\r\n        XDocument document = XDocument.Parse(xml.Text);\r\n\r\n        XNamespace con = XNamespace.Get(&quot;google:archive:conversation&quot;);\r\n        XNamespace cli = XNamespace.Get(&quot;jabber:client&quot;);\r\n\r\n        XElement conversation = document.Element(con + &quot;conversation&quot;);\r\n        foreach (XElement message in conversation.Elements(cli + &quot;message&quot;))\r\n        {\r\n            XElement body = message.Element(cli + &quot;body&quot;);\r\n\r\n            Console.WriteLine(&quot;{0} -&gt; {1}: {2}&quot;,\r\n                message.Attribute(&quot;from&quot;).Value,\r\n                message.Attribute(&quot;to&quot;).Value,\r\n                body.Value);\r\n        }\r\n    }\r\n    imap.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nUsing imap As New Imap()\r\n    imap.ConnectSSL(&quot;imap.gmail.com&quot;)\r\n    imap.Login(&quot;user@gmail.com&quot;, &quot;password&quot;)\r\n\r\n    imap.Select(&quot;&#x5B;Gmail]\/Chats&quot;)\r\n\r\n    Dim firstFive As IEnumerable(Of Long) = imap.GetAll().Take(5)\r\n    For Each uid As Long In firstFive\r\n        Dim email As IMail = New MailBuilder().CreateFromEml( _\r\n            imap.GetMessageByUID(uid))\r\n\r\n        Dim xml As MimeText = DirectCast(email.Visuals(0), MimeText)\r\n\tDim document As XDocument = XDocument.Parse(xml.Text)\r\n\r\n\tDim con As XNamespace = XNamespace.&#x5B;Get](&quot;google:archive:conversation&quot;)\r\n\tDim cli As XNamespace = XNamespace.&#x5B;Get](&quot;jabber:client&quot;)\r\n\r\n\tDim conversation As XElement = document.Element(con + &quot;conversation&quot;)\r\n\tFor Each message As XElement In conversation.Elements(cli + &quot;message&quot;)\r\n\t    Dim body As XElement = message.Element(cli + &quot;body&quot;)\r\n\r\n            Console.WriteLine(&quot;{0} -&gt; {1}: {2}&quot;, _\r\n                message.Attribute(&quot;from&quot;).Value,  _\r\n                message.Attribute(&quot;to&quot;).Value,  _\r\n                body.Value)\r\n        Next\r\n    Next\r\n    imap.Close()\r\nEnd Using\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can download all Gmail and Google Talk conversations using IMAP protocol. Gmail can turn your instant messaging conversations into threads of emails \u2014 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15,25,28,77,57],"class_list":["post-3990","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-gmail","tag-imap","tag-imap-component","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3990"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=3990"}],"version-history":[{"count":11,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3990\/revisions"}],"predecessor-version":[{"id":4559,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3990\/revisions\/4559"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=3990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=3990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=3990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}