Download emails (backup) from gmail programmatically
Asked Answered
C

4

6

Does anyone know a way to execute a bulk dump of every email of a gmail account and write the emails to a file?

I'm looking to write a program that would let users back up there gmail (probably via imap) and back it up to either individual files or as a pst (I know pst will probably be much harder)

Crystallization answered 29/5, 2009 at 15:46 Comment(0)
H
6

some time ago I wrote a blog post about exactly same topic. See HOWTO: Download emails from a GMail account in C# for details.

Code uses our Rebex Mail component:

using Rebex.Mail;
using Rebex.Net;
...
// create the POP3 client
Pop3 client = new Pop3();
try
{

   // Connect securely using explicit SSL. 
   // Use the third argument to specify additional SSL parameters. 
   Console.WriteLine("Connecting to the POP3 server...");
   client.Connect("pop.gmail.com", 995, null, Pop3Security.Implicit);

   // login and password
   client.Login(email, password);

   // get the number of messages
   Console.WriteLine("{0} messages found.", client.GetMessageCount());

   // -----------------
   // list messages
   // -----------------

   // list all messages
   ListPop3MessagesFast(client); // unique IDs and size only   
   //ListPop3MessagesFullHeaders(client); // full headers
}
finally
{
   // leave the server alone
   client.Disconnect();      
}


public static void ListPop3MessagesFast(Pop3 client)
{
   Console.WriteLine("Fetching message list...");

   // let's download only what we can get fast
   Pop3MessageCollection messages = 
      client.GetMessageList(Pop3ListFields.Fast);

   // display basic info about each message
   Console.WriteLine("UID | Sequence number | Length");
   foreach (Pop3MessageInfo messageInfo in messages)
   {
      // display header info
      Console.WriteLine
      (
         "{0} | {1} | {2} ",
         messageInfo.UniqueId,
         messageInfo.SequenceNumber,
         messageInfo.Length
      );

      // or download the whole message
      MailMessage mailMessage = client.GetMailMessage(messageInfo.SequenceNumber);
   }   
}
Handclasp answered 24/9, 2009 at 0:16 Comment(0)
A
4

Gmail provides POP access. So just use any library that allows you to communicate using POP and you're golden.

Edit: I just noticed that you mentioned IMAP; I recommend you use POP instead for bulk dumps. IMAP is too chatty for what you want to do.

If you must use IMAP, here's a library for you.

Albertson answered 29/5, 2009 at 15:51 Comment(1)
If anyone chooses to use POP, be sure to check that the code works even when the inbox contains >1000 messages.Bidentate
D
1

You can use fetchmail from a Unix environment to create an mbox file.

http://lifehacker.com/software/gmail/geek-to-live--back-up-gmail-with-fetchmail-235207.php

Dg answered 29/5, 2009 at 15:49 Comment(0)
S
0

There is an open-source Python program compiled to Windows (using py2exe) at https://github.com/jay0lee/got-your-back/wiki

But Mac users would need to compile it (which I haven't completely figured out due to a py2exe error).

Either way, you also need a way to execute the program automatically in a schedule.

Springtail answered 31/12, 2014 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.