O365 Send Email Through Shared Mailbox Via VBScript

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 protected]"

'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

'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 = "[email protected]"
objMessage.To = "[email protected]"
objMessage.TextBody = "So cool dude!"

'Finally, send the message

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.

If you've found this useful, you may want to sign up to our newsletter where you'll receive notices on when we post new articles and helpful "how tos". Just fill out your details below and we'll do the rest...

3 Responses

  1. According to https://docs.microsoft.com/en-US/microsoft-365/admin/email/create-a-shared-mailbox?view=o365-worldwide "Every shared mailbox has a corresponding user account. Notice how you weren't asked to provide a password when you created the shared mailbox? The account has a password, but it's system-generated (unknown). You aren't supposed to use the account to log in to the shared mailbox.". What password do we have to enter in the script?
    • Matrix7
      Hey Jan, All you need to do is log onto your O365 Admin portal and do the following: Expand Users > Click Active Users > Select Your Shared Mailbox from the list > Click the Reset Password Button Input a password and now you'll be able to use that password to both log onto a shared mailbox as well as use the VBScript I outlined to send email through it...

Leave a comment

Sign up to our newsletter where you’ll receive notices on when we post new articles and helpful “how tos” to make your IT life easier.