1

I have a client that will send us everyday by email attachment files to process it. I have script task but it doesn't work, the script connect to my account through webmail but when the script try to read the Inbox folder I get an error. In this specific line: FindItemsResults results = service.FindItems(WellKnownFolderName.Inbox, querystring, view);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.Exchange.WebServices.Data;

namespace testmailattachment
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ExchangeService service = new ExchangeService();
            service.Credentials = new WebCredentials("user", "pass", "domain");
            /*service.Url = new Uri("https://webmail.domain.es/owa/");*/
            service.AutodiscoverUrl("birep@domain.es");

            GetAttachments(service);

        }
        private static void GetAttachments(ExchangeService service)
        {
            // Return a single item.
            ItemView view = new ItemView(1);

            string querystring = "HasAttachments:true Subject:'Message with Attachments' Kind:email";
            //Console.WriteLine("check test");
            // Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
            FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, querystring, view);

            if (results.TotalCount > 0)
            {
                EmailMessage email = results.Items[0] as EmailMessage;

                // Request all the attachments on the email message. This results in a GetItem operation call to EWS.
                email.Load(new PropertySet(EmailMessageSchema.Attachments));

                foreach (Attachment attachment in email.Attachments)
                {
                    if (attachment is FileAttachment)
                    {
                        FileAttachment fileAttachment = attachment as FileAttachment;

                        // Load the file attachment into memory. This gives you access to the attachment content, which 
                        // is a byte array that you can use to attach this file to another item. This results in a GetAttachment operation
                        // call to EWS.
                        fileAttachment.Load();
                        //Console.WriteLine("Load a file attachment with a name = " + fileAttachment.Name);

                        // Load attachment contents into a file. This results in a GetAttachment operation call to EWS.
                        fileAttachment.Load("\\\\server\\folder" + fileAttachment.Name);

                        // Put attachment contents into a stream.
                        using (FileStream theStream = new FileStream("C:\\temp\\Stream_" + fileAttachment.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                            //This results in a GetAttachment operation call to EWS.
                            fileAttachment.Load(theStream);
                        }
                    }
                    else // Attachment is an item attachment.
                    {
                        ItemAttachment itemAttachment = attachment as ItemAttachment;

                        // Load the item attachment properties. This results in a GetAttachment operation call to EWS.
                        itemAttachment.Load();
                        //Console.WriteLine("Loaded an item attachment with Subject = " + itemAttachment.Item.Subject);
                    }
                }
            }
        }
    }
}

Also I don´t know how to do, that the script just take the emails from a specific person or subject.

Thanks in advance.

0 Answers0