0 votes

I want to download .csv format file and save it to database.

so how can i do it ???

I dont want to save the csv format file in any hard drive.

Please Reply.


1 Answer

0 votes
Best answer

Download email using IMAP:

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

    List<long> uids = imap.Search(Flag.Unseen);
    foreach (long uid in uids)
        var eml = imap.GetMessageByUID(uid);
        IMail email = new MailBuilder().CreateFromEml(eml);

        foreach (MimeData mime in email.Attachments)
            // here you can check the content type or the file name.
            if (mime.ContentType == ContentType.TextCsv)

If the csv file is attached to the email you can directly access attachment’s data as stream using MimeData.GetMemoryStream() method:

MemoryStream stream = mime.GetMemoryStream();

or as byte array using MimeData.Data property:

byte[] data = mime.Data;

If your csv attachment has correct mime type (text/plain or text/csv), you can also cast MimeData to MimeText and use Text property to access csv data as regular .NET string:

string text = ((MimeText)mime).Text;

As for your question on how to save this data to database, it depends on the db you are using (MSSQL, Oracle, MySql, document db) and its structure. I don't think I can help you with so little information.

by (298k points)