0
votes

i have written below addin code to save attachments from Selected Outlook Email.

 MailItem mailItem = null;
            Attachments mtAttachments = null;

            Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer();

            if (explorer != null && explorer.Selection != null && explorer.Selection.Count > 0)
            {
                object item = explorer.Selection[1];
                if (item is MailItem)
                {
                    mailItem = item as MailItem;
                    subject = mailItem.Subject;
                    body = mailItem.HTMLBody;
                    mtAttachments = mailItem.Attachments;

                    if (mailItem.Attachments.Count > 0)
                    {
                        for (int j = 1; j <= mailItem
                           .Attachments.Count; j++)
                        {
                            mailItem.Attachments[j].SaveAsFile
                                (@"C:\TestFileSave\" +
                                mailItem.Attachments[j].FileName);
                        }
                    }
                }
            }

But the problem is, it is also saving images from signatures also, which is completely wrong.

I just want to save attachments of email.

can anyone please help me.?

1

1 Answers

0
votes

With the reference Getting Error Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))

I have updated my code and it is working fine.

Now it will download only attachments attached to email.

MailItem mailItem = null;
            Attachments mtAttachments = null;

            Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer();

            if (explorer != null && explorer.Selection != null && explorer.Selection.Count > 0)
            {
                object item = explorer.Selection[1];
                if (item is MailItem)
                {
                    mailItem = item as MailItem;
                    subject = mailItem.Subject;
                    body = mailItem.HTMLBody;
                    mtAttachments = mailItem.Attachments;

                    if (mailItem.Attachments.Count > 0)
                    {
                        for (int j = 1; j <= mailItem
                           .Attachments.Count; j++)
                        {
                            if (mailItem.Attachments[j].PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x7FFE000B") == false)
                            {
                                mailItem.Attachments[j].SaveAsFile
                                    (@"C:\TestFileSave\" +
                                    mailItem.Attachments[j].FileName);
                            }
                        }
                    }
                }
            }