How to avoid Outlook Security Alert when sending Outlook message from VBScript?

耗尽温柔 提交于 2019-12-05 09:43:39

This code works on my system with Outlook 2010 to send an email with no user interaction. It's slightly brittle, in that, if the user happens to be actively working on the system (typing, clicking) when the mail is composed, it could occur that user input makes its way into the window that pops up for a split second, and either interfere with the sending of the mail, or add extra unknown characters into the body of the mail.

As long as the user on whose system this is running is aware of this, and the potential consequences of spurious keystroke interference are not business-critical, this function is acceptable.

Important to note: the key to this solution is that we do not call the MailItem.Send method. This is the method that triggers the programmatic access protection. Instead, we trigger the ALT+s shortcut, which by default, when a mail window has focus in Outlook, presses the "Send" button. If you have the default spell checking prompt enabled, this will pop up a further prompt for spellchecking. Our solution was to disable the spellchecking prompt, although I'm sure you could add some more SendKeys to click through it, since the spellchecking prompt is not a security-related dialog.

A note about UIPI (User Interface Privilege Isolation):

Outlook 2010 runs as the user who logged into the system, with a Medium integrity level. Programs which are launched by Windows Explorer, or started as a child or descendant of a program which was launched in a similar way, will also be launched with a Medium integrity level. UIPI is not effective at preventing "SendKeys" type input, as long as the user and session ID match, and the integrity level is the same or higher. In my particular environment, the user and session ID are identical, and the integrity level is the same for the VBScript host process and the Outlook process. In your environment, if any of these conditions are false, this code will not work. It is also untested on earlier or later versions of Office than version 2010.

Sub SendEmail_Outlook()
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set ol=CreateObject("Outlook.Application") 
    Set Mail=ol.CreateItem(0) 
    Mail.to= "you@example.com"
    Mail.Subject = "test"
    Mail.HTMLBody = "test"
    Mail.Display    
    WshShell.SendKeys "%s"
    Set Mail = Nothing 
    Set ol = Nothing 
End Sub

SendEmail_Outlook

Also, here is how to make this work when running the VBScript from Windows Task Scheduler. Just tick the box depicted by the red oval, "Run With Highest Privileges", to make it run with the highest possible integrity level without UAC elevation ("Medium" if you are not an administrator account).

A few options:

  1. Up-to-date antivirus software (Outlook will not display a prompt then)
  2. Extended MAPI (C++ or Delphi, does not apply in case of VB script). You can however use a wrapper like Redemption that uses Extended MAPI but is accessible from any language including VBS.
  3. A product like ClickYes.

See http://www.outlookcode.com/article.aspx?id=52 for a discussion and a list of available options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!