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 = "
[email protected]"
objEmail.To = "
[email protected]"
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.