Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

S22.Imap dll save attached files to designated directory

Posted on 2016-09-15
1
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
louisfr earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

661 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