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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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.
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!
Thank you Bob 🙂 You are very welcome.
Thank you very much for this. I was also searching for a solution and all of ’em failed except for yours. !!
The only solution I found that worked. Kudos to you!
Voilla, it works, kudos to you….
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.
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', Null
You 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, Null
sendMail "another.user@domain.com", "fromaddress@fromdomain.com", "This is the subject", sMessage, Null
If 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, Null
Hi,
I used your code its working fine but priority thing is not working.
Thanks,
Shahid
Thank you very much for this code !
Priority level is correct.
It works fine
Thank you, thank you, thank you!
Please for example with more attachment!
thanks!
Thanks a ton! I couldn’t easily solve the priority issue.
You didn’t, however, include how to send LOW priority mail. After more searching and playing around I got this to work on Outlook 6 and my web based mail program at least:
ObjMail.Fields.Item(“urn:schemas:mailheader:X-MSMail-Priority”) = “Low”
ObjMail.Fields.Item(“urn:schemas:mailheader:X-Priority”) = 5
ObjMail.Fields.Item(“urn:schemas:httpmail:importance”) = 0
Thanks Sir.
iImportance = Cint(request.form(“importance”))
Select Case iImportance
Case 2
objMessage.Configuration.Fields.Item(“urn:schemas:mailheader:X-MSMail-Priority”) = “High” ‘ For Outlook 2003
Case 1
objMessage.Configuration.Fields.Item(“urn:schemas:mailheader:X-MSMail-Priority”) = “Medium” ‘ For Outlook 2003
Case 0
objMessage.Configuration.Fields.Item(“urn:schemas:mailheader:X-MSMail-Priority”) = “Low” ‘ For Outlook 2003
End Select
objMessage.Configuration.Fields.Item(“urn:schemas:mailheader:X-Priority”) = iImportance ‘ For Outlook 2003 also
objMessage.Configuration.Fields.Item(“urn:schemas:httpmail:importance”) = iImportance
The above did not work for me (script is running on a Server 2003 with Outlook 2003 installed, receiving client is Outlook 2003). But this does:
objMessage.Fields.Item(“urn:schemas:mailheader:importance”) = “high” ‘ or “normal” or “low”
objMessage.Fields.Update
(found here: http://objectmix.com/inetserver/356971-sending-cdo-message-high-importance.html)
Hi,
I am new to access and i have created an employee database. In it there are fields of passport_Expiry, Labourcard_Expiry and i am struggling since last 1 month for setting up a reminder for warning me for an expiry. Like if the passport is getting Expiry on 03.08.2012 then i want it to remind me on 03.07.2012 as an email. It should automatically generate email from outlook and forward it to concern person. Please help me with it.
Thanks
Thanks a tonne Lewis. Your code was a godsend. Yours is the only sample that worked in a whole day !!!
A decade later and it still works perfect, Thanks! I’m glad you never took this post off-line. 🙂
Thank you 🙂
most important to remember is the update at the end of setting the importance flag which I forgot and finally found. Thanks OP
Lewis Roberts, this blog post still wears a cape in 2020! Thanks for the help here. Working on VERY old Classic ASP code from like. . . the turn of the century (literally), converting all references of CDONTS.NewMail to CDO.Message.