Andrei Smolin

Bypass Outlook security warnings when sending email messages in MS Access

Whenever you send e-mail messages in Microsoft Access, you get Outlook security warnings like A program is trying to automatically send email:

A program is trying to automatically send email on your behalf.

To get rid of such security warning messages, use the Outlook Security Manager that effectively disables / enables the Outlook Security. With the ActiveX edition of this product you can easily switch the Outlook security off and on. Here we describe two methods of sending emails in Microsoft Access using the ActiveX edition of Outlook Security Manager.

Sending emails using the DoCmd. SendObject method

With the ActiveX edition of Outlook Security Manager you write the following simple code:

Sub SendEmailMessageViaDoCmd()
Dim SecurityManager As Object
  Set SecurityManager = CreateObject("AddInExpress.OutlookSecurityManager")
  SecurityManager.DisableSMAPIWarnings = True
  On Error GoTo Finally
  DoCmd.SendObject acSendNoObject, , ,"user@server.com", , , _
  "Message subject", "Message body", False
Finally:
  SecurityManager.DisableSMAPIWarnings = False
  Set SecurityManager = Nothing
End Sub

What does this procedure do?

At first, the Outlook Security Manager object is declared and created. Then DisableSMAPIWarnings of this object is set to true. At this moment the Outlook Security Manager disables security warnings from the so-called Simple MAPI email processing protocol, which is used whenever an email is being sent. Then your familiar DoCmd creates and sends your e-mail. And finally, DisableSMAPIWarnings is set to False, enabling security warnings from Simple MAPI. Note, this last step is important, because if you fail to re-enable the Simple MAPI Security, some other MDB or ADP opened in this MS Access session can potentially do some harm to you.

Sending emails using Outlook Object Model

With the ActiveX edition of Outlook Security Manager you send emails in the following way:

Sub SendEmailMessageViaOutlookObjectModel()
Dim olApp As Outlook.Application
  Set olApp = New Outlook.Application 'GetObject(,"Outlook.Application")
Dim email As Outlook.MailItem
  Set email = olApp.CreateItem(olMailItem)
 
Dim SecurityManager As Object
  Set SecurityManager = CreateObject("AddInExpress.OutlookSecurityManager")
  SecurityManager.ConnectTo olApp
  SecurityManager.DisableOOMWarnings = True
  On Error GoTo Finally
  email.Recipients.Add "user@server.com"
  email.Subject = "Message subject"
  email.Body = "Message body"
  email.Send
Finally:
  SecurityManager.DisableOOMWarnings = False
  SecurityManager.Disconnect olApp
  Set SecurityManager = Nothing
  Set email = Nothing
  Set olApp = Nothing
End Sub

How does this procedure work?

First off, you get the Outlook.Application object and the MailItem object (the last one represents an email in Outlook). Then you create the Outlook Security Manager object and connect it to the Outlook application. Then the DisableOOMWarnings of the Outlook Security object is set to true. The ‘OOM’ part in the property name stands for “Outlook Object Model”. At this moment the Outlook Security Manager disables Outlook Security prompts. Then you set the email properties, such as, Subject, Body, add email addresses to the Recipients collection and send the email. And finally, DisableOOMWarnings is set to False, enabling Outlook Security. Note, this last step is highly important, because in the other case you can leave your Outlook unprotected from really malicious software. Sure, it is not what you really want. That’s all for now. Hope this helps.

Andrei Smolin
Add-in Express Team Leader

Related posts:

Why Outlook is not closing when I run my add-in?

17 Comments

  • Sonthor says:

    I’ve run code but got error run-time error ‘429’: ActiveX component can’t create object, and after I click on Debug button it get Highlight Yellow at Set SecurityManager = CreateObject(“AddInExpress.OutlookSecurityManager”)

  • Andrei Smolin (Add-in Express Team) says:

    Make sure that you have the Security Manager installed on your PC.

  • Larry Dumoulin says:

    I purchased Outlook Security Manager 2007 in May 2007. I developed several MS Access 2003 and 2007 databases using your product, and have been quite happy with it.

    I have noticed something rather strange with respect to the “DisableOOMWatnings”, specifically this line in your sample;

    “SecurityManager.DisableOOMWarnings = True”

    When I leave the line in my code, it always errors when it hits that line. In testing I decided to REM out the line, and it of course does not error, however it still seems to be disabling the warnings because I do not see them when I test. However, I still see the warnings when I test on other computers, even where I have deployed the distributable files. If I restore the code to DIsableOOMWarnings, then I simply get an error.

    I am at a real loss to explain this, can yu offer any advice?

    Larry

  • Andrei Smolin (Add-in Express Team) says:

    Larry,

    Ususally, that line produces an exception if the Security Manager isn’t registered on the target PC. Please find more info about deploying Outlook Security Manager at Outlook Security Manager 2010 deployment: Summary.

    If you develop an add-in than you will not see security warnings in Outlook 2007 provided that you have an antivirus software installed.

    However, there are other scenarios in which you’ll need to use Security Manager. Say, an Exchange administrator can use some settings that cause your code to produce warnings when accessing Outlook.Namespace.CurrentUser. You can find if this is your case by looking at the About window of your Outlook; it displays either “Security Mode: Default” or “Security Mode: Administrator controlled”.

    You can send me extra details about the issue to the support e-mail address (see readme.txt or use any contact form on the web site).

  • Saurabh says:

    Hi, i am using this component to restrict the security warnings in my code. But once in a month i got this pop up. Can you please help me in this regard ?
    What other thing i need to do ? Earlier i used to get many warnings but after deploying this code the problem gets 99.9 % solved but .1 is still there.

  • Andrei Smolin (Add-in Express Team) says:

    Hi Saurabh,

    You can send me your code for review: please find the support email address in {Security Manager installation folder}\readme.txt. The business logic and sencitive info can be removed; I’ll need to look at ALL Outlook-related code, however.

  • Saurabh says:

    Thnak you for the immediate reply.
    The code which i am using is:

    public void sendEMailThroughOUTLOOK()
    {
    Outlook.Application oApp=null;
    SecurityManager m_ObjSM=null;
    try
    {

    try
    {
    oApp = new Outlook.Application();
    m_ObjSM = new SecurityManager();
    m_ObjSM.ConnectTo(oApp);
    m_ObjSM.DisableCDOWarnings = true;
    m_ObjSM.DisableOOMWarnings = true;
    m_ObjSM.DisableSMAPIWarnings = true;
    }
    catch (Exception ex1)
    {
    System.Windows.Forms.MessageBox.Show(ex1.Message);
    }
    Outlook.NameSpace oNS = oApp.GetNamespace(“mapi”);
    Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    oMsg.HTMLBody = “Hello, This is a test mail !!!”;
    oMsg.Subject = “BMW Test”;
    Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
    Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(“xxxxx@xxxxx-xx.com”);
    oRecip.Resolve();
    oMsg.Send();

    oRecip = null;
    oRecips = null;
    oMsg = null;
    oApp = null;
    }
    catch (Exception ex)
    {
    System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    }

    Please suggest if i am missing anything.

  • Andrei Smolin (Add-in Express Team) says:

    First off, I modified your comment to hide your email address: as you probably know, spammers collect email addresses whenever possible.

    Please pay attention to the template code we provide at https://www.add-in-express.com/outlook-security/:

    Dim SecurityManager As New AddinExpress.Outlook.SecurityManager
    SecurityManager.DisableOOMWarnings = True
    Try
    ‘ … any action with protected objects …
    Finally
    ‘ In any case please remember to turn on
    ‘ Outlook Security after your code,
    ‘ since now it is very easy to switch it off! :-)
    SecurityManager.DisableOOMWarnings = False
    End Try

    A) You need to use the only Disable*Warnings property: the one corresponding to the way you access Outlook. since you do this via the Outlook object model, you need to use DisableOOMWarnings only; all other properties should be removed.

    B) You should allow the warnings in the finally clause.

    In addition to the general case, your code could leave OUTLOOK.EXE hanging in the Task Manager window. I’d suggest that you use oApp.Quit.

  • Saurabh says:

    Thank you Andrei for your reply.
    Let me follow your guidelines, I will get back to you for my further concerns.

  • Saurabh says:

    One thing I want to tell is that we cannot make the outlook application as oApp.Quit. This will kill the outlook instance on the machine. We need to make it open all the time as other tasks are dependent on this.This there any other way to do this as we dont want to close the outlook process.
    Can this problem which we are facing may be of network connection of the Microsoft Exchange Server ?

  • Andrei Smolin (Add-in Express Team) says:

    Saurabh,

    Try the following code:

    public void sendEMailThroughOUTLOOK()
    {
    Outlook.Application oApp = null;
    SecurityManager m_ObjSM = null;
    Outlook._MailItem oMsg = null;
    Outlook.Recipients oRecips = null;
    Outlook.Recipient oRecip = null;
    try
    {
    oApp = new Outlook.Application();
    m_ObjSM = new SecurityManager();
    m_ObjSM.ConnectTo(oApp);
    m_ObjSM.DisableOOMWarnings = true;

    oMsg = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    oMsg.HTMLBody = “Hello, This is a test mail !!!”;
    oMsg.Subject = “BMW Test”;
    oRecips = (Outlook.Recipients)oMsg.Recipients;
    oRecip = (Outlook.Recipient)oRecips.Add(“xxxxx@xxxxx-xx.com”);
    oRecip.Resolve();
    oMsg.Send();
    }
    catch (Exception ex)
    {
    System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
    if (oMsg != null) Marshal.ReleaseComObject(oMsg); oMsg = null;
    if (oRecip != null) Marshal.ReleaseComObject(oRecip); oRecip = null;
    if (oRecips != null) Marshal.ReleaseComObject(oRecips); oRecips = null;
    if (oApp != null)
    {
    if (m_ObjSM != null)
    {
    m_ObjSM.DisableOOMWarnings = false;
    m_ObjSM.Disconnect(oApp);
    }
    Marshal.ReleaseComObject(oApp); oApp = null;
    }
    }
    }

  • Saurabh says:

    Andrei, the code runs fine now. But the testing would take around a month’s time as the pop up appears once in a month. Many thanks for your valueable replies. I will get back to you for my further concerns.

  • Andrei Smolin (Add-in Express Team) says:

    You are welcome!

  • Marcel Delapp says:

    You need to take part in a contest for one of the best blogs online. I most certainly will recommend this web site!

  • Andrei Smolin (Add-in Express Team) says:

    Marcel, thank you a lot!

  • Moreschi says:

    Hi,

    Does not work
    How do I avoid the message in my case?

    Private Sub Form_Open(Cancel As Integer)
    DoCmd.SetWarnings False
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Rstatus As String
    Dim N, S, D, E, QTDE, i
    Dim tb As Recordset
    Set tb = CurrentDb.OpenRecordset(“AC_Vencidas”)
    tb.MoveLast
    If tb.RecordCount = 0 Then
    DoCmd.Quit
    Else
    tb.MoveFirst
    QTDE = tb.RecordCount
    For i = 1 To QTDE
    N = tb(“Numero”)
    D = tb(“DataPrazo”)
    S = tb(“Setor”)
    E = tb(“Email”)
    Set OutApp = CreateObject(“Outlook.Application”)
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    OutApp.DisableSMAPIWarnings = True <—– Does not work
    With OutMail
    .to = E
    .Subject = "Ação corretiva nº: " & N & " está vencida " & D
    .Body = "Ação corretiva"
    .Send
    End With
    Next i
    End If
    tb.Close

    DoCmd.Quit
    End Sub

  • Andrei Smolin (Add-in Express Team) says:

    Hello Moreshi,

    First off, please pay attention that you need use the Security Manager in accordance with the code pattern shown at https://www.add-in-express.com/outlook-security/index.php.

    Then, “Does not work”… Please send us more details to the support email address (see readme.txt in the Security Manager installation folder). Or, you can use any contact form on our website.

Post a comment

Have any questions? Ask us right now!