Solved

S22.Imap dll save attached files to designated directory

Posted on 2016-09-15
1
61 Views
Last Modified: 2016-10-18
Hello

Im S22.Imap dll with the following C# code below to save attached files to emails within a mailbox.

Does anyone know I can save the attached files to a directory?
Source of the code is : https://github.com/smiley22/S22.Imap/blob/master/Examples.md#7

namespace S22
{

    class Program
    {
        static void Main(string[] args)
        {
            using (ImapClient Client = new ImapClient("server", port, @"domain\user_id\mailbox", "Password", AuthMethod.Login, false))
            {
                // This returns all messages sent since August 23rd 2016.
                IEnumerable<uint> uids = Client.Search(SearchCondition.SentSince(new DateTime(2016, 8, 26)));
                // The expression will be evaluated for every MIME part
                // of every mail message in the uids collection.
                IEnumerable<MailMessage> messages = Client.GetMessages(uids,
                    (Bodypart part) =>
                    {
                        // We're only interested in attachments.
                        if (part.Disposition.Type == ContentDispositionType.Attachment)
                        {
                            Int64 TwoMegabytes = (1024 * 1024 * 2);
                            if (part.Size > TwoMegabytes)
                            {
                                // Don't download this attachment
                                return false;
                            }
                        }

                        // Fetch MIME part and include it in the returned MailMessage instance.
                        return true;
                    }
                );
            }
        }
    }
}

Open in new window

0
Comment
Question by:barkome
1 Comment
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 41804506
This should copy all attachments from the 'messages' collection to disk, with the original name.
Directory.CreateDirectory(@"d:\Temp\Attachments\");
foreach(MailMessage message in messages)
{
    foreach(Attachment attachment in message.Attachments)
    {
        using (FileStream stream = new FileStream(Path.Combine(@"d:\Temp\Attachments\", Path.GetFileName(attachment.Name)), FileMode.Create))
        {
            attachment.ContentStream.CopyTo(stream);
    }
}

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question