0

I am working on an application that crawls my email and sniffs out any emails with attachments. All attachment are being returned in the order they were received. Now I want to go a step further and would like to save any attachments in a local directory. I have been looking for documentation or examples but I have come up empty. I will show you a snippet of my code

This Function will get Email Attachments

public static List<IMessage> GetEmailAttachments()
    {
        OutlookServicesClient star_Mail_Box = Start_OutLook_Services();

        try
        {

            var Email_Box = star_Mail_Box.Users["*****@dell.com"].Folders["Inbox"].Messages.Where(m => m.HasAttachments == true).Expand(m => m.Attachments).ExecuteAsync();

            var messages = Email_Box.Result.CurrentPage;

            foreach (var message in messages.OrderByDescending(m=> m.DateTimeReceived))
            {
                var attachments = message.Attachments.CurrentPage;

                foreach (var attachment in attachments)
                {
                  ///This is where I will need to put my Logic.                       
                }
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine("Not Able To Get This Mail Box" + ex.Message + "\n\nDetails : \n\n " + ex.InnerException);
            Console.ReadLine();

        }

        return null; // returning null right now for testing 
    }
EasyE
  • 560
  • 5
  • 26
  • so what happens when you run the call and it hits the `GetEmailAttachments()` method..? are you at least getting the attachments.. can you please edit the question and explain the `what is vs what is not` happening..? – MethodMan Jan 25 '16 at 15:04
  • Yup all attachment are returned in the order they were received. I would like some help getting the logic on how to store the attachments to a directory, I am not finding any documentation on this. Thank you for your prompt reply. – EasyE Jan 25 '16 at 15:06
  • storing it on a Directory should be the simple part.. are you wanting to store them on your local or on a remote server location.. – MethodMan Jan 25 '16 at 15:09
  • Locally is ideal for now, but if you can also give help with storing them on a server that will be very generous of you. – EasyE Jan 25 '16 at 15:14
  • why can't you use google to find examples on how to copy files to a Directory.. this is fairly simple with in your `foreach (var attachment in attachments)` you will can't to copy the attachment to a folder / directory `if (!Directory.Exists` do a google search on examples for this then look at the this link http://stackoverflow.com/questions/7146021/copy-all-files-in-directory you can put all of this together very quickly..`SO` is not a `Code Factory Site` – MethodMan Jan 25 '16 at 15:21
  • the office 365 api v 2.0 is relatively new and I have not seen examples in what I am trying to do. Now the example you showed in the link you provided not going to work because I am working on MS office 365 which is web based. thank you for your help. – EasyE Jan 25 '16 at 15:35
  • you can most definitely do this web base in regards to copying files to a location on the server you need to lookup how to use `Server.MapPath` if you are capturing the attachments, you need to capture their source filepath\filename also make sure that you have permissions setup for the target filepath on the remote serve.. – MethodMan Jan 25 '16 at 15:55
  • Ok got it after looking at the attachment definition I went ahead and did it like a certain way. This is very generic and can be used by anyone. I will post it now – EasyE Jan 25 '16 at 16:15

1 Answers1

0

Ok so after looking at the attachment definition I figured I go through a byte array to achieve what I want. Here goes the Some code for my attachment loop.

foreach (FileAttachment attachment in attachments)
{
    byte[] bytefiles = attachment.ContentBytes;
    string path = @"C:\Top-Level\" + attachment.Name;

    if (!string.IsNullOrEmpty(message.Subject))
    {
        path =  @"C:\Top-Level\" + message.Subject + "." + attachment.ContentType;
    }
    File.WriteAllBytes(path, bytefiles);
}
EasyE
  • 560
  • 5
  • 26
  • 1
    I would wrap this `File.WriteAllBytes(path, bytefiles);` around a `try{}catch(IOException iex){}` just incase something fails – MethodMan Jan 25 '16 at 16:21