Tuesday, October 25, 2011

Sending mail with CDO Message

If you need to send an email from EXCEL or VBscript here is the simple method

' To Send mail
Dim objmessage
Set objmessage = CreateObject("CDO.Message")
objmessage.Subject = "NOTIFICATION EMAIL ----"
objmessage.From = "FROM MAIL FIELD"
objmessage.To = "EMAIL TO WHICH YOU NEED TO SEND"
objmessage.TextBody = "ATTENTION YOU HAVE GOTTA MAIL " & vbCrLf &SOMESTRING
objmessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objmessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "YOURSMTPSERVER"
objmessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objmessage.Configuration.Fields.Update
objmessage.Send

with above you can send mail easily and decide also what to send in "TextBody" field. In the configuration field "sendusing" has 3 Enum values
1 - Send using Pickup Folder
2 - Send using Port (default is 25)
3 - Send using Exchange

If your smtp server requires authentication you need to add "authentication" in configuration fields with username and password, also you can use SSL. Here is a simple example how (remember all must be written before Fields.Update..

http://msdn.microsoft.com/en-us/library/ms873029%28v=EXCHG.65%29.aspx

No comments:

Post a Comment