Tried reading the outlook email attachment in c#. Getting error as Microsoft.Exchange.WebServices.Data.ServiceResponseException: Id is malformed.

Hello everyone,

I am trying to read the email attachment for the outlook email in asp.net c# but i am new to this can anyone help me, i have the below code which select the new email with specific title.

String MailboxToAccess = "myemailaddress";
ExchangeService service = new Microsoft.Exchange.WebServices.Data.ExchangeService(ExchangeVersion.Exchange2007_SP1);
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
            searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "EDI"));
            searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
            SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
            service.Credentials = new NetworkCredential("myemail", "pwd");
            service.AutodiscoverUrl(MailboxToAccess, adAutoDiscoCallBack);
            FolderId FolderToAccess = new FolderId(WellKnownFolderName.Inbox, MailboxToAccess);
            ItemView ivItemView = new ItemView(50);
            FindItemsResults<Item> FindItemResults = service.FindItems(FolderToAccess, searchFilter, ivItemView);
            PropertySet ItemPropertySet = new PropertySet(BasePropertySet.IdOnly);
            ItemPropertySet.Add(ItemSchema.Body);
            ItemPropertySet.RequestedBodyType = BodyType.Text;
///error here??????
                EmailMessage message = EmailMessage.Bind(service, new ItemId("AAMkA"), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

                // Iterate through the attachments collection and load each attachment.
                foreach (Attachment attachment in message.Attachments)
                {
                    if (attachment is FileAttachment)
                    {
                        FileAttachment fileAttachment = attachment as FileAttachment;

                        // Load the file attachment into memory and print out its file name.
                        fileAttachment.Load();
                        Console.WriteLine("Attachment name: " + fileAttachment.Name);

                        // Load attachment contents into a file.
                        fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);

                        // Stream attachment contents into a file.
                        FileStream theStream = new FileStream("C:\\temp\\Stream_" + fileAttachment.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        fileAttachment.Load(theStream);
                        theStream.Close();
                        theStream.Dispose();
                    }
                    else // Attachment is an item attachment.
                    {
                        // Load attachment into memory and write out the subject.
                        ItemAttachment itemAttachment = attachment as ItemAttachment;
                        itemAttachment.Load();
                        Console.WriteLine("Subject: " + itemAttachment.Item.Subject);
                    }
              //  }
               
            }
           
        }
        internal static bool adAutoDiscoCallBack(string redirectionUrl)
        {
            // The default for the validation callback is to reject the URL.
            bool result = false;

            Uri redirectionUri = new Uri(redirectionUrl);

            // Validate the contents of the redirection URL. In this simple validation
            // callback, the redirection URL is considered valid if it is using HTTPS
            // to encrypt the authentication credentials.
            if (redirectionUri.Scheme == "https")
            {
                result = true;
            }

            return result;

        }
Imran KhuntAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsuyemotoCommented:
If you're searching for the email, not sure how you can use this:

PropertySet ItemPropertySet = new PropertySet(BasePropertySet.IdOnly);
            ItemPropertySet.Add(ItemSchema.Body);
            ItemPropertySet.RequestedBodyType = BodyType.Text;
///error here??????
                EmailMessage message = EmailMessage.Bind(service, new ItemId("AAMkA"), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
since you haven't found the email yet.  You'll need to iterate through the FindItemResults, verify that it is of type 'EmailMessage' and then use the properties to get the attachments.
foreach (Item myItem in FindItemResults.Items)
{
    if (myItem is EmailMessage)
    {
        var message = myItem as EmailMessage
        // Iterate through the attachments collection and load each attachment.
                foreach (Attachment attachment in message.Attachments)
                {
                    if (attachment is FileAttachment)
                    {
                        FileAttachment fileAttachment = attachment as FileAttachment;

                        // Load the file attachment into memory and print out its file name.
                        fileAttachment.Load();
                        Console.WriteLine("Attachment name: " + fileAttachment.Name);

                        // Load attachment contents into a file.
                        fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);

                        // Stream attachment contents into a file.
                        FileStream theStream = new FileStream("C:\\temp\\Stream_" + fileAttachment.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        fileAttachment.Load(theStream);
                        theStream.Close();
                        theStream.Dispose();
                    }
                    else // Attachment is an item attachment.
                    {
                        // Load attachment into memory and write out the subject.
                        ItemAttachment itemAttachment = attachment as ItemAttachment;
                        itemAttachment.Load();
                        Console.WriteLine("Subject: " + itemAttachment.Item.Subject);
                    }
            }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
frankhelkCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: dsuyemoto (https:#a42048896)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.