从Gmail中以编程方式下载电子邮件(备份)

6

有没有人知道一种方法可以批量导出 Gmail 账户中的每封电子邮件并将邮件写入文件?

我正在寻找一种编写程序的方式,使用户可以通过 IMAP 备份他们的 Gmail,并将其备份为单个文件或作为 PST(我知道 PST 可能会更难)。

4个回答

6

之前我写过一篇关于同样主题的博客文章。详细内容请参见HOWTO: Download emails from a GMail account in C#

代码使用我们的Rebex Mail组件

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);
   }   
}

4

Gmail提供POP访问。因此,只需使用任何允许您使用POP进行通信的,您就可以轻松完成。

编辑:我刚刚注意到您提到了IMAP;我建议您改用POP进行批量转储。对于您想要做的事情来说,IMAP太啰嗦了。

如果您必须使用IMAP,这里有一个供您使用。


如果有人选择使用POP,请确保在收件箱包含>1000条消息时代码仍然能够正常工作。 - arnt

1

0

有一个开源的Python程序编译到Windows(使用py2exe)上,链接在https://github.com/jay0lee/got-your-back/wiki

但是Mac用户需要自己编译它(由于py2exe错误,我还没有完全弄清楚如何编译)。

无论如何,您还需要一种方法来按计划自动执行该程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接