none
Win 7 and MS Outlook 2010 problem RRS feed

  • Question

  • Hi,

    The following code used to work, but since upgrading to Win7 it no longer does (security issues??).  We use this code to send an outlook e-mail through another application.

    Private Sub SendOutlookEmail(ByVal txtSubject As String, ByVal txtBody As String)
    
            Dim ol As New Outlook.Application()
    
            Dim ns As Outlook.NameSpace
            Dim mail As Outlook.MAPIFolder
    
            ns = ol.GetNamespace("MAPI")
            ns.Logon(, , True, True)
    
            Dim newMail As Outlook.MailItem
            mail = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox)
    
            newMail = mail.Items.Add(Outlook.OlItemType.olMailItem)
            newMail.Subject = txtSubject
            newMail.Body = txtBody
            newMail.To = "*****"
            newMail.CC = "*****"
            newMail.SaveSentMessageFolder = mail
    
            newMail.Send()
        End Sub

    If Outlook is closed, it works ok, but then the user has to right click on the icon in the bottom right 'hidden icons' to exit Outlook manually.  If outlook is already open, an error message occurs.  I'm certain it's the Dim ol As New Outlook.Application() that is the problem, but I'm not sure how to get around it.

    From the .NET CLR forum I tried doing this to help sort out the issue of Outlook already being open:

    Dim ol As Outlook.Application
            Dim ns As Outlook.NameSpace
    
            If Process.GetProcessesByName("OUTLOOK").Length > 0 Then
                ol = DirectCast(Marshal.GetActiveObject("Outlook.Application"), Outlook.Application)
                ns = ol.GetNamespace("MAPI")
            Else
                ol = New Outlook.Application()
                ns = ol.GetNamespace("MAPI")
            End If

    But it crashes at:

    ol = DirectCast(Marshal.GetActiveObject("Outlook.Application"), Outlook.Application)

    when Outlook is already open.

    Thanks,




    Brian

    Wednesday, February 12, 2014 2:20 PM

Answers

  • Hello Brian,

    It looks like you get a standard Outlook security prompt when trying to send a message. You can read more about possible solutions in the Outlook "Object Model Guard" Security Issues for Developers article. The most widely used ways are:

    1. Use the low level code for accessing Outlook data and its functionality - Extended MAPI or any other wrappers like Redemption.

    2. Use commercial component which suppress such warnings (turn them off).  


    • Edited by Eugene Astafiev Wednesday, February 12, 2014 2:39 PM
    • Marked as answer by Khabibulla Wednesday, February 12, 2014 2:41 PM
    Wednesday, February 12, 2014 2:38 PM
  • Thanks.  It seems like running Outlook as 'Administrator' as well as the calling application (ESRI ArcMap) seems to fix the problem, still using my original code.

    I will check out the article you mention.

    Thanks,


    Brian



    • Edited by Khabibulla Wednesday, February 12, 2014 2:40 PM
    • Marked as answer by Khabibulla Wednesday, February 12, 2014 4:24 PM
    Wednesday, February 12, 2014 2:40 PM

All replies

  • Hello Brian,

    It looks like you get a standard Outlook security prompt when trying to send a message. You can read more about possible solutions in the Outlook "Object Model Guard" Security Issues for Developers article. The most widely used ways are:

    1. Use the low level code for accessing Outlook data and its functionality - Extended MAPI or any other wrappers like Redemption.

    2. Use commercial component which suppress such warnings (turn them off).  


    • Edited by Eugene Astafiev Wednesday, February 12, 2014 2:39 PM
    • Marked as answer by Khabibulla Wednesday, February 12, 2014 2:41 PM
    Wednesday, February 12, 2014 2:38 PM
  • Thanks.  It seems like running Outlook as 'Administrator' as well as the calling application (ESRI ArcMap) seems to fix the problem, still using my original code.

    I will check out the article you mention.

    Thanks,


    Brian



    • Edited by Khabibulla Wednesday, February 12, 2014 2:40 PM
    • Marked as answer by Khabibulla Wednesday, February 12, 2014 4:24 PM
    Wednesday, February 12, 2014 2:40 PM
  • Outlook and any program that calls into Outlook must be run in the same security context.

    Ken Slovak MVP - Outlook

    Wednesday, February 12, 2014 3:11 PM
    Moderator