Solved

S22.Imap dll save attached files to designated directory

Posted on 2016-09-15
1
22 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now