예제 #1
0
 /// <summary>
 /// Return true if we can export to the supplied inspector
 /// </summary>
 /// <param name="currentItem">the Item to check</param>
 /// <param name="allowMeetingAsTarget">bool true if also exporting to meetings</param>
 /// <returns></returns>
 private static bool canExportToInspector(Item currentItem, bool allowMeetingAsTarget)
 {
     try {
         if (currentItem != null)
         {
             OlObjectClass currentItemClass = currentItem.Class;
             if (OlObjectClass.olMail.Equals(currentItemClass))
             {
                 MailItem mailItem = (MailItem)currentItem;
                 //MailItem mailItem = COMWrapper.Cast<MailItem>(currentItem);
                 LOG.DebugFormat("Mail sent: {0}", mailItem.Sent);
                 if (!mailItem.Sent)
                 {
                     return(true);
                 }
             }
             else if (outlookVersion.Major >= OUTLOOK_2010 && allowMeetingAsTarget && OlObjectClass.olAppointment.Equals(currentItemClass))
             {
                 //AppointmentItem appointmentItem = COMWrapper.Cast<AppointmentItem>(currentItem);
                 AppointmentItem appointmentItem = (AppointmentItem)currentItem;
                 if (string.IsNullOrEmpty(appointmentItem.Organizer) || (currentUser != null && currentUser.Equals(appointmentItem.Organizer)))
                 {
                     return(true);
                 }
                 else
                 {
                     LOG.DebugFormat("Not exporting, as organizer is {1} and currentuser {2}", appointmentItem.Organizer, currentUser);
                 }
             }
         }
     } catch (Exception ex) {
         LOG.WarnFormat("Couldn't process item due to: {0}", ex.Message);
     }
     return(false);
 }
예제 #2
0
        /// <summary>
        /// A method to retrieve all inspectors which can act as an export target
        /// </summary>
        /// <param name="allowMeetingAsTarget">bool true if also exporting to meetings</param>
        /// <returns>List<string> with inspector captions (window title)</returns>
        public static Dictionary <string, OlObjectClass> RetrievePossibleTargets(bool allowMeetingAsTarget)
        {
            Dictionary <string, OlObjectClass> inspectorCaptions = new Dictionary <string, OlObjectClass>();

            try {
                using (IOutlookApplication outlookApplication = GetOutlookApplication()) {
                    if (outlookApplication == null)
                    {
                        return(null);
                    }

                    using (Inspectors inspectors = outlookApplication.Inspectors) {
                        if (inspectors != null && inspectors.Count > 0)
                        {
                            for (int i = 1; i <= inspectors.Count; i++)
                            {
                                using (Inspector inspector = outlookApplication.Inspectors[i]) {
                                    string inspectorCaption = inspector.Caption;
                                    using (Item currentItem = inspector.CurrentItem) {
                                        if (canExportToInspector(currentItem, allowMeetingAsTarget))
                                        {
                                            OlObjectClass currentItemClass = currentItem.Class;
                                            inspectorCaptions.Add(inspector.Caption, currentItemClass);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                LOG.Warn("Problem retrieving word destinations, ignoring: ", ex);
            }
            return(inspectorCaptions);
        }
예제 #3
0
 /// <summary>
 /// Return true if we can export to the supplied inspector
 /// </summary>
 /// <param name="currentItem">the Item to check</param>
 /// <returns></returns>
 private static bool CanExportToInspector(IItem currentItem)
 {
     try {
         if (currentItem != null)
         {
             OlObjectClass currentItemClass = currentItem.Class;
             if (OlObjectClass.olMail.Equals(currentItemClass))
             {
                 MailItem mailItem = (MailItem)currentItem;
                 //MailItem mailItem = COMWrapper.Cast<MailItem>(currentItem);
                 Log.DebugFormat("Mail sent: {0}", mailItem.Sent);
                 if (!mailItem.Sent)
                 {
                     return(true);
                 }
             }
             else if (_outlookVersion.Major >= (int)OfficeVersion.OFFICE_2010 && OfficeConfig.OutlookAllowExportInMeetings && OlObjectClass.olAppointment.Equals(currentItemClass))
             {
                 //AppointmentItem appointmentItem = COMWrapper.Cast<AppointmentItem>(currentItem);
                 AppointmentItem appointmentItem = (AppointmentItem)currentItem;
                 if (string.IsNullOrEmpty(appointmentItem.Organizer) || (_currentUser != null && _currentUser.Equals(appointmentItem.Organizer)))
                 {
                     return(true);
                 }
                 Log.DebugFormat("Not exporting, as organizer is {0} and currentuser {1}", appointmentItem.Organizer, _currentUser);
             }
         }
     } catch (Exception ex) {
         Log.WarnFormat("Couldn't process item due to: {0}", ex.Message);
     }
     return(false);
 }
예제 #4
0
 protected OutlookDestination(
     string outlookInspectorCaption,
     OlObjectClass outlookInspectorType,
     IOfficeConfiguration officeConfiguration,
     ICoreConfiguration coreConfiguration,
     IGreenshotLanguage greenshotLanguage
     ) : this(officeConfiguration, coreConfiguration, greenshotLanguage)
 {
     _outlookInspectorCaption = outlookInspectorCaption;
     _outlookInspectorType    = outlookInspectorType;
 }
        /// <summary>
        /// A method to retrieve all inspectors which can act as an export target
        /// </summary>
        /// <returns>List<string> with inspector captions (window title)</returns>
        public static IDictionary <string, OlObjectClass> RetrievePossibleTargets()
        {
            IDictionary <string, OlObjectClass> inspectorCaptions = new SortedDictionary <string, OlObjectClass>();

            try {
                using (IOutlookApplication outlookApplication = GetOutlookApplication()) {
                    if (outlookApplication == null)
                    {
                        return(null);
                    }

                    if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2013)
                    {
                        // Check inline "panel" for Outlook 2013
                        using (var activeExplorer = outlookApplication.ActiveExplorer()) {
                            if (activeExplorer != null)
                            {
                                using (var inlineResponse = activeExplorer.ActiveInlineResponse) {
                                    if (canExportToInspector(inlineResponse))
                                    {
                                        OlObjectClass currentItemClass = inlineResponse.Class;
                                        inspectorCaptions.Add(activeExplorer.Caption, currentItemClass);
                                    }
                                }
                            }
                        }
                    }

                    using (IInspectors inspectors = outlookApplication.Inspectors) {
                        if (inspectors != null && inspectors.Count > 0)
                        {
                            for (int i = 1; i <= inspectors.Count; i++)
                            {
                                using (IInspector inspector = outlookApplication.Inspectors[i]) {
                                    string inspectorCaption = inspector.Caption;
                                    using (IItem currentItem = inspector.CurrentItem) {
                                        if (canExportToInspector(currentItem))
                                        {
                                            OlObjectClass currentItemClass = currentItem.Class;
                                            inspectorCaptions.Add(inspector.Caption, currentItemClass);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                LOG.Warn("Problem retrieving word destinations, ignoring: ", ex);
            }
            return(inspectorCaptions);
        }
예제 #6
0
 public OutlookDestination(string outlookInspectorCaption, OlObjectClass outlookInspectorType)
 {
     this.outlookInspectorCaption = outlookInspectorCaption;
     this.outlookInspectorType    = outlookInspectorType;
 }
예제 #7
0
        /// <summary>
        /// Export the file to the supplied inspector
        /// </summary>
        /// <param name="inspector">Inspector</param>
        /// <param name="currentItem">Item</param>
        /// <param name="tmpFile"></param>
        /// <param name="attachmentName"></param>
        /// <returns></returns>
        private static bool ExportToInspector(Inspector inspector, Item currentItem, string tmpFile, string attachmentName)
        {
            if (currentItem == null)
            {
                LOG.Warn("No current item.");
                return(false);
            }
            OlObjectClass itemClass     = currentItem.Class;
            bool          isMail        = OlObjectClass.olMail.Equals(itemClass);
            bool          isAppointment = OlObjectClass.olAppointment.Equals(itemClass);

            if (!isMail && !isAppointment)
            {
                LOG.Warn("Item is no mail or appointment.");
                return(false);
            }
            MailItem mailItem = null;

            try {
                if (isMail)
                {
                    //mailItem = COMWrapper.Cast<MailItem>(currentItem);
                    mailItem = (MailItem)currentItem;
                    if (mailItem.Sent)
                    {
                        LOG.WarnFormat("Item already sent, can't export to {0}", currentItem.Subject);
                        return(false);
                    }
                }

                // Make sure the inspector is activated, only this way the word editor is active!
                // This also ensures that the window is visible!
                inspector.Activate();
                bool isTextFormat = false;
                if (isMail)
                {
                    isTextFormat = OlBodyFormat.olFormatPlain.Equals(mailItem.BodyFormat);
                }
                if (isAppointment || !isTextFormat)
                {
                    // Check for wordmail, if so use the wordexporter
                    // http://msdn.microsoft.com/en-us/library/dd492012%28v=office.12%29.aspx
                    // Earlier versions of Outlook also supported an Inspector.HTMLEditor object property, but since Internet Explorer is no longer the rendering engine for HTML messages and posts, HTMLEditor is no longer supported.
                    if (inspector.IsWordMail() && inspector.WordEditor != null)
                    {
                        try {
                            if (WordExporter.InsertIntoExistingDocument(inspector.WordEditor.Application, inspector.WordEditor, tmpFile, null, null))
                            {
                                LOG.Info("Inserted into Wordmail");

                                // check the format afterwards, otherwise we lose the selection
                                //if (!OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) {
                                //	LOG.Info("Changing format to HTML.");
                                //	currentMail.BodyFormat = OlBodyFormat.olFormatHTML;
                                //}
                                return(true);
                            }
                        } catch (Exception exportException) {
                            LOG.Error("Error exporting to the word editor, trying to do it via another method", exportException);
                        }
                    }
                    else if (isAppointment)
                    {
                        LOG.Info("Can't export to an appointment if no word editor is used");
                        return(false);
                    }
                    else
                    {
                        LOG.Info("Trying export for outlook < 2007.");
                    }
                }
                // Only use mailitem as it should be filled!!
                LOG.InfoFormat("Item '{0}' has format: {1}", mailItem.Subject, mailItem.BodyFormat);

                string contentID;
                if (outlookVersion.Major >= OUTLOOK_2007)
                {
                    contentID = Guid.NewGuid().ToString();
                }
                else
                {
                    LOG.Info("Older Outlook (<2007) found, using filename as contentid.");
                    contentID = Path.GetFileName(tmpFile);
                }

                // Use this to change the format, it will probably lose the current selection.
                //if (!OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) {
                //	LOG.Info("Changing format to HTML.");
                //	currentMail.BodyFormat = OlBodyFormat.olFormatHTML;
                //}

                bool inlinePossible = false;
                if (OlBodyFormat.olFormatHTML.Equals(mailItem.BodyFormat))
                {
                    // if html we can try to inline it
                    // The following might cause a security popup... can't ignore it.
                    try {
                        IHTMLDocument2 document2 = inspector.HTMLEditor as IHTMLDocument2;
                        if (document2 != null)
                        {
                            IHTMLSelectionObject selection = document2.selection;
                            if (selection != null)
                            {
                                IHTMLTxtRange range = selection.createRange();
                                if (range != null)
                                {
                                    // First paste, than attach (otherwise the range is wrong!)
                                    range.pasteHTML("<BR/><IMG border=0 hspace=0 alt=\"" + attachmentName + "\" align=baseline src=\"cid:" + contentID + "\"><BR/>");
                                    inlinePossible = true;
                                }
                                else
                                {
                                    LOG.DebugFormat("No range for '{0}'", inspector.Caption);
                                }
                            }
                            else
                            {
                                LOG.DebugFormat("No selection for '{0}'", inspector.Caption);
                            }
                        }
                        else
                        {
                            LOG.DebugFormat("No HTML editor for '{0}'", inspector.Caption);
                        }
                    } catch (Exception e) {
                        // Continue with non inline image
                        LOG.Warn("Error pasting HTML, most likely due to an ACCESS_DENIED as the user clicked no.", e);
                    }
                }

                // Create the attachment (if inlined the attachment isn't visible as attachment!)
                using (Attachment attachment = mailItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible ? 0 : 1, attachmentName)) {
                    if (outlookVersion.Major >= OUTLOOK_2007)
                    {
                        // Add the content id to the attachment, this only works for Outlook >= 2007
                        try {
                            PropertyAccessor propertyAccessor = attachment.PropertyAccessor;
                            propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID);
                        } catch {
                        }
                    }
                }
            } catch (Exception ex) {
                LOG.WarnFormat("Problem while trying to add attachment to Item '{0}' : {1}", inspector.Caption, ex);
                return(false);
            }
            try {
                inspector.Activate();
            } catch (Exception ex) {
                LOG.Warn("Problem activating inspector: ", ex);
                return(false);
            }
            LOG.Debug("Finished!");
            return(true);
        }
 public EmailDestination(string outlookInspectorCaption, OlObjectClass outlookInspectorType)
 {
     this.outlookInspectorCaption = outlookInspectorCaption;
     this.outlookInspectorType = outlookInspectorType;
 }