Solved

S22.Imap dll save attached files to designated directory

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net VB.net Load contents of a GridView  to Excel 2 27
Code works but I need to redo it and assign values to Model 7 32
C# Single Form 8 26
Message not shown 5 29
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

12 Experts available now in Live!

Get 1:1 Help Now