Today I'm going to show you how to use VBScript to send an email via an Office 365 Shared Mailbox. Why would you want to do this? This is handy when you don't have an on-premise email server that you can route email through. It's good for things like sending emails via scheduled task, after a server reboot or something of that nature.
The VBScript is pretty self exploratory as outlined in the code below. What you need to do is change the configuration values to suit your Shared Mailbox with the email address and password to ensure that the script passes the correct credentials to allow the script to successfully send through the account.
Lastly, in the block of code below at the bottom of the script, change the subject, the senders email, the "To" is the recipient and finally the email body is the content of the actual email.
Dim objMessage Dim myDate Dim myTime Set objMessage = CreateObject("CDO.Message") 'This is just populating the date variable myDate = Date() 'This is just populating the time variable myTime = Time() 'Send email using specified port (25 in our case below)... objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com" 'Server port objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'Username of the Shared Mailbox objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "email@example.com" 'Password of the Shared mailbox objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "<YourPasswordForSharedMailbox>" 'Is authentication required? 1 for yes, 0 for no objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'Does email require SSL encryption? True or False objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True 'Update the message with the settings above objMessage.Configuration.Fields.Update 'All the fields below are setting up the actual email with the Subject, Sender, To and finally Email content objMessage.Subject = "O365 Email Test: " & myDate & " " & MyTime objMessage.Sender = "firstname.lastname@example.org" objMessage.To = "email@example.com" objMessage.TextBody = "So cool dude!" 'Finally, send the message objMessage.Send
Save your text file a with a VBS extension like O365EmailAlert.vbs. Once saved, double click it and you should receive an email in the nominated "To" email account.
Hope you've found this useful...