1

I am trying to use Outlook object in windows service to read mails from a inbox and then extracting embedded images. But mail can have any type of images embedded in it. Is there any easy method in outlook API where i can save all the embedded in GIF Format?

 if (newEmail.Attachments.Count > 0)
            {
                for (int i = 1; i <= newEmail
                   .Attachments.Count; i++)
                {
                    string value = newEmail.Attachments[i].PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");

                    if (!String.IsNullOrEmpty(value))
                    {
                    newEmail.Attachments[i].SaveAsFile
                        (@"C:\TestFileSave\" +
                        newEmail.Attachments[i].FileName);
                    }
                }
            } 
Navaneet
  • 1,367
  • 1
  • 19
  • 44
  • No, you would have to use Binary Detection on Mine Types to confirm the attachment is an image and then save it as a gif. What's the desired functionality? Pics only? – Jeremy Thompson Jan 11 '17 at 15:08
  • @JeremyThompson Yes i want to save all the embedded files. I think in most cases users will embed images of screenshots etc. What other files can be embedded to a Mail in general(other than images)? – Navaneet Jan 11 '17 at 15:10
  • In RTF mode you can put files, afaik they will be attachments though (sorry can't recall last time I checked), nor have I seen an embedded video or audio clip. – Jeremy Thompson Jan 11 '17 at 15:15
  • Can I ask what the GetProperty using a schema achieves? Just checking for embedded images? Btw This is the Mime Type checker code: http://stackoverflow.com/q/15300567/495455 – Jeremy Thompson Jan 11 '17 at 15:18
  • Yes. That GetProperty is used to check whether attachment is embedded or attached one. – Navaneet Jan 12 '17 at 07:13
  • Cool, I'd check MimeType and if it's a gif just save it as you're doing, if it's an Image in a format other than gif use the System.Drawing namespaces Bitmap class and its .Load & .Save methods to convert the image to gif. – Jeremy Thompson Jan 12 '17 at 07:57
  • Be careful about this with my MimeType advice using Binary Detection, see this to understand why: http://security.stackexchange.com/q/81677/10505 – Jeremy Thompson Jan 12 '17 at 08:05

1 Answers1

1

You cannot use Outlook Object Model in a service. Why not create a COM addin.

You are also checking for the presence of the PR_ATTACH_CONTENT_ID property. Keep in mind that Content-ID MIME header can be set on any attachment, it is not necessarily an image, you'd need to check the HTML body for that.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • So there is no way i can use Microsoft.Office.Interop.Outlook.dll with windows service? If not possible then what is the solution? – Navaneet Jan 16 '17 at 05:05
  • 1
    Your options are Extended MAPI (C++ or Delphi), Redemption (it wraps Extended MAPI and can be used from any language), or EWS (In case of an Exchange mailbox). – Dmitry Streblechenko Jan 16 '17 at 13:55
  • So does interop outlook will not work or is it just a security concern that it should not be used in windows service. I tried using it in Windows service in one of my machine and it worked!! but in another server I got remote procedure call failed when I try to get mailitem.SenderEmailAddress. In both cases the windows service has logged on user set to the user which has Outlook connection. So what is the reason behind these two scenarios? – Navaneet Jan 16 '17 at 19:06
  • It will not work - it is simply designed to be used by as end user app that can display prompts. – Dmitry Streblechenko Jan 16 '17 at 19:17
  • But it worked in my machine as Windows service. It was able to get mail properties and attachments. – Navaneet Jan 17 '17 at 01:59
  • That's the thing - it works sometimes (Outlook does not try to detect running in a service and crash on purpose), but it fails at the worst possible moment, like when you deploy to production or demo the app to your CEO. – Dmitry Streblechenko Jan 17 '17 at 03:26
  • Thanks. Where I can learn or get code samples for Extended MAPI? – Navaneet Jan 17 '17 at 03:56
  • Try to get a copy of "Inside MAPI". Remember that Extended MAPI is C++ or Delphi, not C#. – Dmitry Streblechenko Jan 17 '17 at 15:12