When you use the outlook mail client programmatically via VBA, you will encounter a message box which asks the user to allow the usage of Outlook.
This is throws a pretty big spanner in the works, if you wish to completely automate a solution and not have pesky message boxes coming up.
That's where CDO comes in:
Using code similar to the below, you are able to avoid using your mail client and send Email Message objects straight to an SMTP server.
The below function, accepts 4 strings; the email body, the FROM email address, the TO email address and the SUBJECT. It then creates an email object, and sends it via the designated SMTP server
Code:
Function Send_Email(strBody as string, strFrom as string, strTo as string, strSubject as string)
Dim iMsg As Object
Dim iConf As Object
Dim Flds As Variant
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "1.2.3.4" 'This is the IP address of the SMTP server
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
With iMsg
Set .Configuration = iConf
.To = strTo
.CC = ""
.BCC = ""
.From = strFrom
.Subject = strSubject
.TextBody = strbody
.Send
End With
Set iMsg = Nothing
Set iConf = Nothing
End Function
If you don't know the SMTP server IP address, you can get this from outlook by:
Tools -> Email Accounts -> view or change existing email accounts
Select the email account and click Change. The SMTP server address (typically DNS) will be in the Microsoft Exchange Server field.