+1 vote

I need to Copy All the E-Mails into My local Drive !

Need to Fetch Date and Time of the E-Mail

by
retagged by

1 Answer

0 votes

I need to Copy All the E-Mails into My local Drive !

You download emails by using GetMessageByUID method - it returns byte[] array.
You can save it to disk any way you want.

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com");  // or ConnectSSL
    imap.UseBestLogin("user", "password");

    imap.SelectInbox();

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

    foreach (long uid in uids)
    {
        var eml = imap.GetMessageByUID(uid);
        string fileName = string.Format(@"c:\email_{0}.eml", uid);
        File.WriteAllBytes(fileName, eml);
    }
    imap.Close();
}

https://www.limilabs.com/blog/save-raw-eml-file-imap-pop3

Need to Fetch Date and Time of the E-Mail

You can parse emails using MailBuilder.CreateFromEml, and the use IMail.Date to get the date stored in email's Date header:

IMail email = new MailBuilder().CreateFromEml(eml);
DateTime? date = email.Date;

https://www.limilabs.com/blog/get-common-email-fields-subject-text-with-imap

You can also use Imap.FetMessageInfoByUID to retrieve most common email information directly from an IMAP server:

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com");   // or ConnectSSL
    imap.UseBestLogin("user", "password");

    imap.SelectInbox();
    List<long> uids = imap.Search(Flag.Unseen);
    List<MessageInfo> infos = imap.GetMessageInfoByUID(uids);

    foreach (MessageInfo info in infos)
    {
        string subject = info.Envelope.Subject;
        DateTime? date = info.Envelope.Data;
    }
    imap.Close();
}

https://www.limilabs.com/blog/get-email-information-from-imap-fast

by (301k points)
...