-2

I am trying to automate autosending Outlook emails with VBScript. I tried following ways:

  1. Sending emails programmatically with Mail.Send command in VBScript

Attempting to send EMails programmatically does not seem to work due to security reasons. Here is my code sample:

Set WshShell = WScript.CreateObject("WScript.Shell")
Set ol=CreateObject("Outlook.Application") 
Set Mail=ol.CreateItem(0) 
Mail.to= "xx.xxx.com"
Mail.Subject = "AUTOMATED UPDATE: is ready to be booked"
Mail.HTMLBody = "please see attachments... :)"
Mail.Display 
Mail.send
ol.quit 
Set Mail = Nothing 
Set ol = Nothing 

POP-UP BLOCK I GET:

enter image description here

  1. Sending Mails with SendKeys Method (working but unstable method) Here is my code sample:

    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.run "outlook"
    Set ol=CreateObject("Outlook.Application")
    Set Mail=ol.CreateItem(0)
    Mail.to= "xxxx.com"
    Mail.Subject = "AUTOMATED UPDATE: "&file_name&" is ready to be booked"
    Mail.HTMLBody = "please see attachments... :)"
    Mail.Attachments.add "xxxx"
    Mail.Display
    WshShell.AppActivate(Mail.Subject&" - Message (HTML)")
    WshShell.SendKeys "%{s}",1
    WScript.Sleep 40000
    ol.quit
    Set Mail = Nothing
    Set ol = Nothing
    WshShell.Run "taskkill /im outlook.exe", , True
    

This method works, but supposes that no one should interrupt the process and klick something. It is very unstable and that's why I am looking for other opportunities.

Having googled till 999999 page :) of Google Search, I found the following options, but neither of them is workable in my case:

  1. Use Redemption - acc. to company policy I am not allowed to use third party application
  2. Use ClickYes - acc. to company policy I am not allowed to use third party application
  3. Send Emails through CDO - I cannot connect to SMTP Server because does not know the password therefrom and not authorized to get it
  4. I also looked if it is possible to send keys on the background window - unfortunately it is NOT possible.
  5. Going to TrustCenter of Outlook and change setting - it is not allowed to do that acc. to company policy

Can anyone advise some other workaround?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Nikolay
  • 43
  • 7
  • Why are you trying to circumvent company policy? The correct way to do this is using CDO to connect to the SMTP server, but if you don't have the credentials to connect to the SMTP server you should either, request access or just stop what you are trying to do. – user692942 Jul 14 '20 at 08:34

2 Answers2

0

The prompt will not be shown if an up-to-date antivirus app is installed.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Unfortunately, I cannot Control that as well. Code will be called from another computer. – Nikolay Jul 13 '20 at 15:53
  • 1
    Then your only options are Extended MAPI (C++ or Delphi) or Redemption. – Dmitry Streblechenko Jul 13 '20 at 18:32
  • Any attempt to script `Send` programmatically via COM from the Outlook Object Model will result in a security prompt regardless of AV or not, because of the nature of the interaction which is open to abuse. That is why CDO is the preferred approach as it's designed for automation. – user692942 Jul 14 '20 at 09:09
  • CDO is not supported and not recommended to use any longer. See https://support.microsoft.com/en-gb/help/2028411/collaboration-data-objects-cdo-1-2-1-is-not-supported-with-outlook-201 – Eugene Astafiev Jul 14 '20 at 12:00
  • @EugeneAstafiev it isn't supported with Outlook, yes, but you [wouldn't need Outlook just an SMTP server configuration](https://stackoverflow.com/a/17425519/692942). Anything else is overkill. – user692942 Jul 14 '20 at 12:18
  • Then Extended MAPI is a better choice than CDO. – Eugene Astafiev Jul 14 '20 at 14:45
  • @EugeneAstafiev except this is a VBScript question, so CDO is the recommended option. If I was a C++ programmer I probably would recommend Extended MAPI. – user692942 Jul 14 '20 at 15:15
0

You get the security prompts because Outlook is configured on the client computer in one of the following ways:

  • Uses the default Outlook security settings (that is, no Group Policy set up)
  • Uses security settings defined by Group Policy but does not have programmatic access policy applied
  • Uses security settings defined by Group Policy which is set to warn when the antivirus software is inactive or out of date

You can create a group policy to prevent security prompts from displaying if any up-to-date antivirus software is installed on the system or just turn these warning off (which is not really recommended).

Read more about that in the Security Behavior of the Outlook Object Model article.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • If they don't have access to install 3rd party software, I doubt they will be allowed to manipulate group policies. – user692942 Jul 14 '20 at 09:07
  • Administrators could set up a group policy to handle everything smoothly. – Eugene Astafiev Jul 14 '20 at 12:01
  • They could also provide access to send email programmatically via CDO via the companies SMTP server. – user692942 Jul 14 '20 at 12:16
  • They could also use third-party components to suppress security prompts like [Outlook Security Manager](https://www.add-in-express.com/outlook-security/index.php) and many more! – Eugene Astafiev Jul 14 '20 at 14:39
  • Think we are going around in circles, OP has already said that they don't have access to install 3rd party applications. – user692942 Jul 14 '20 at 15:14