Need alternate to CDO.Message for Windows Server Core

FCOA
FCOA used Ask the Experts™
on
Looking for an alternate to CDO.Message for sending email in a VBScript on Windows Server Core.

NOTE: While we also use PowerShell scripts to send email, we have several substantial legacy VBScripts that carry out tasks and send mail, so fully rewriting these in PowerShell will be a longer term objective. In the interim, we're in need of a workaround to the VBScript issue. Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
The VB Script methods have not worked natively in ages, you must have installed old .net packages to get those working in 2k8/2012, so I would have thought 2016 would let you fanagle that too, but maybe they've finally removed the chance, have you looked into how you got it to work in 2012?


As for Powershell, you can just call it from inside your VBScript

Dim _SMTPServer as string = "Server IP or FQDN"
Dim _MailFrom as string = "Sending Email Address@Domain.com"
Dim _RcptTo as string = "Your Email Address@Your Domain.com"
Dim _Subject as string = "Your Email Subject"
Dim _Body as string = "Just an Example"
Dim _Attachment = "C:\temp\Completely_Optional_Attachment.ext" 

Dim _ShellSession As Object
Set _ShellSession= CreateObject("Wscript.Shell")

'Send WITH Attachment:
_ShellSession.Run("  PowerShell.exe -Command '& {Send-MailMessage -To "&_RcptTo&" -Subject "&_Subject&" -Body ( "&_Body&" | out-string )  -SmtpServer "&_SMTPServer&" -From "&_MailFrom&" -Attachment "&_Attachment&" }}' ")

'Send WITHOUT Attachment:
_ShellSession.Run("  PowerShell.exe -Command '& {Send-MailMessage -To "&_RcptTo&" -Subject "&_Subject&" -Body ( "&_Body&" | out-string )  -SmtpServer "&_SMTPServer&" -From "&_MailFrom&" }}' ")

Open in new window


I actually use this similar method from CMD as the majority of my older scripts are CMD, and I have been replacing parts that called VB Script or 3rd party utilities to send email with a call to powershell as needed.
FCOASystems Admin

Author

Commented:
Excellent!! Thanks so much! I'll give this a try.

Interestingly, it appears the CDO.Message method works fine for us through Server 2019 on the "Desktop Experience" version, but not on Server Core. This includes devices with the base install and no add-on roles or features. This is actually our first foray into Server Core.
FCOASystems Admin

Author

Commented:
FYI- I've been toying with the suggested VBScript, but it doesn't appear to be working. I started with it as-is, and that didn't work, then tried revising it from there. Thus far, no luck.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
To be completely up front we depricated and converted all pf our VB scripting about 10 yeas ago give or take, because of inconsistamcies in each OS's implementation, and mail send cdo not working most specifically.

We've been writing pure cmd scripts and a little powershell, although its finnally flipped to mostly powershell and some cmd, so I had to lift the old school shell execute example, and had no test space.

I can say the body and subject will need quotes around them, which I don't have

 are you getting any error?  might also need to run elevated, which I recall being possiblr in vb.

finally even in CMD we usually pipe the output to a cmd script that calls the powershell, or nest the powershell in the cmd and call itself, but VBA does not work well for code nesting
FCOASystems Admin

Author

Commented:
The following options work:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell -NonInteractive -ExecutionPolicy Bypass -Command ""& { Send-MailMessage -To to@domain.com -From from@domain.com -Subject 'Test Message' -Body 'This is a test.' -SMTPServer servermame.domain.com }""")

Open in new window

OR

strSMTPServer = "servername.domain.com"
strFrom = "from@domain.com"
strTo = "to@domain.com"
strSubject = "Test Message"
strBody = "This is a test."

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell -NonInteractive -ExecutionPolicy Bypass -Command ""& { Send-MailMessage -To "& strTo &" -From "& strFrom &" -Subject '"& strSubject &"' -Body '"& strBody &"' -SMTPServer "& strSMTPServer &" }""")

Open in new window

Systems Admin
Commented:
See my code examples about. Much thanks to Ben for pointing me down the right path!
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Glad to help!  :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial