Sending CDO.Message with importance
Here’s a script, well, a subroutine I wrote today for sending an email with VBSCript using the CDO.Message and CDO.Configuration COM Controls available in Windows. Having struggled a little to actually get it working (mainly through utter stupidity), I thought I’d blog it for anyone else struggling or just needing a quick fix.
Sub sendMail(strTo, strFrom, strSubject, strMessage, strAttachment)
Set cdoMail = CreateObject("CDO.Message")
Set cdoConf = CreateObject("CDO.Configuration")
cdoMail.Subject = strSubject
cdoMail.From = strFrom
cdoMail.To = strTo
cdoMail.TextBody = strMessage
If IsNull(strAttachment) Then
'Nothing
Else
cdoMail.AddAttachment strAttachment
End If
sch = "http://schemas.microsoft.com/cdo/configuration/"
cdoConf.Fields.Item(sch & "sendusing") = 2
cdoConf.Fields.Item(sch & "smtpserver") = "my.smtp.server"
cdoConf.Fields.Item(sch & "smtpserverport") = 25
cdoConf.Fields.Item(sch & "smtpauthenticate") = 0
cdoConf.Fields.Update
Set cdoMail.Configuration = cdoConf
cdoMail.Fields.Item("urn:schemas:mailheader:X-MSMail-Priority") = "High" ' For Outlook 2003
cdoMail.Fields.Item("urn:schemas:mailheader:X-Priority") = 2 ' For Outlook 2003 also
cdoMail.Fields.Item("urn:schemas:httpmail:importance") = 2 ' For Outlook Express
cdoMail.Fields.Update
cdoMail.Send
Set cdoMail = Nothing
End Sub
As if to state the obvious, if you don’t wish to add an attachment, you must state that it is null when calling the subroutine.
July 28th, 2006 at 20:19
Thank you so much for publishing this! I have been searching for over an hour for the correct way to set the importance\priority using CDO, and everybody had a different way (none of which worked!). Your sub worked perfectly for me!
July 29th, 2006 at 07:58
Thank you Bob
You are very welcome.
November 6th, 2006 at 23:38
Thank you very much for this. I was also searching for a solution and all of ‘em failed except for yours. !!
June 22nd, 2007 at 13:53
The only solution I found that worked. Kudos to you!
June 22nd, 2007 at 20:55
Voilla, it works, kudos to you….
July 31st, 2007 at 18:23
Hello - can you answer a simple one? I have a form that allows the user to enter their email address, I then want to send them a reply. How do I script the reply into the “To = “user@test.com”? Thanks.
August 1st, 2007 at 09:55
Hi Peter, this posting is actually a subroutine that you can use as many times as you like. You simply call it from within your script.
For instance, you have the subroutine as above. You then call that subroutine using the following syntax:
sendMail 'mailto@domain.com', 'mailfrom@domain.com', 'This is the subject', 'This is the message', NullYou can call the subroutine as many times as you like within the same script.
Again, the subroutine is copied in to the script, you then simply call it…
First I set the message, obviously this could be quite a long message so I find it better to break it out in to a separate variable.
sMessage = "Hello, this is a long message with line breaks" & VbCrLf & "This is the second line."sendMail "user@domain.com", "mycompany@company.com", "This is the subject", sMessage, NullsendMail "another.user@domain.com", "fromaddress@fromdomain.com", "This is the subject", sMessage, NullIf you’re sending the same message to more than one person, you can even just separate the email addresses with commas, thus:
sendMail "user1@domain.com, user2@domain.com“, “fromuser@fromdomain.com”, “This is the subject”, sMessage, NullMay 8th, 2008 at 16:19
Hi,
I used your code its working fine but priority thing is not working.
Thanks,
Shahid