How Can I Attach a File to an Email Sent Using CDO?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I understand how to use CDO to send an email, but how do I include an attachment with that email?

— RT

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RT. And, incidentally, thanks for this question. At least one of the Scripting Guys spent most of his Thanksgiving weekend eating and, on the rare occasions when he wasn’t eating, playing tackle football with a phalanx of nephews much bigger and – sigh – much younger than he is. It’s safe to say that this Scripting Guy was hoping to ease his way back to work, and this question is a good way to start.

For those of you who aren’t familiar with CDO (short for Collaboration Data Objects) this technology provides a way for you to send email from a script. As long as you have an SMTP server located somewhere on your network, you can create and send an email using code similar to this:

Set objEmail = CreateObject(“CDO.Message”)

objEmail.From = “helpdesk@fabrikam.com” objEmail.To = “administrator@fabrikam.com” objEmail.Subject = “Server down” objEmail.Textbody = “Server1 is no longer accessible over the network.”

objEmail.Configuration.Fields.Item _ (“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2 objEmail.Configuration.Fields.Item _ (“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = _ “smtpmailer” objEmail.Configuration.Fields.Item _ (“http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25 objEmail.Configuration.Fields.Update

objEmail.Send

We’re not going to explain this script in any detail today; for a quick overview of the individual properties used here, see the Microsoft Windows 2000 Scripting Guide. You can probably figure out the From, To, Subject, and Textbody properties without too much trouble. The URIs (e.g., http://schemas.microsoft.com/cdo/configuration/sendusing) are boilerplate code that can typically be left as-is; in most cases, the only one you’ll need to change is smtpserver. In our sample code, we’ve referenced an SMTP mail server we named smtpmailer; you’ll need to replace that with the name of your SMTP mail server.

By the way, these URIs concern a lot of people: they wonder why they need to connect to Microsoft in order to send an email. The truth is, you don’t actually connect to Microsoft; these URIs are just properties. Why are they referenced like this? To be honest, we don’t know. But don’t worry: your mail doesn’t get routed through Microsoft, and no one here reads it. Trust us, we have enough problems keeping up with our own email, let alone taking time to read someone else’s.

Now, what about adding an attachment to this email? All it takes is one additional line of code, much like this line which attaches the files C:\Scripts\Output.txt to the email:

objEmail.AddAttachment “C:\Scripts\Output.txt”

That’s it; add this line of code to the script, and you’ll have yourself an attachment. The entire script will look something like this:

Set objEmail = CreateObject(“CDO.Message”)

objEmail.From = “helpdesk@fabrikam.com” objEmail.To = “administrator@fabrikam.com” objEmail.Subject = “Server down” objEmail.Textbody = “Server1 is no longer accessible over the network.” objEmail.AddAttachment “C:\Scripts\Output.txt”

objEmail.Configuration.Fields.Item _ (“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2 objEmail.Configuration.Fields.Item _ (“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = _ “smtpmailer” objEmail.Configuration.Fields.Item _ (“http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25 objEmail.Configuration.Fields.Update

objEmail.Send

As long as we have your attention, we’ve had a couple people ask us how they can send email if their SMTP server requires authentication. To tell you the truth, that’s a tough question for us to answer because (for various reasons) we don’t have a way to test that scenario. However, adding a user name and password to your script is a good place to start:

objEmail.Configuration.Fields.Item _
    (“http://schemas.microsoft.com/cdo/configuration/sendusername”) = “fabrikam\kenmyer”
objEmail.Configuration.Fields.Item _ 
    (“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “&gr54#wgha”

The preceding code logs you on to the SMTP server as fabrikam\kenmyer, with the password &gr54#wgha. Note that this does send the user name and password in cleartext. Consequently, you probably don’t want to send email using an Administrator account. Instead, create a user account that has the right to send email (but little, if anything else) and log on using that account. For more information, check out the official CDO documentation on MSDN.


0 comments

Discussion is closed.

Feedback usabilla icon