{"id":70923,"date":"2004-11-29T13:42:00","date_gmt":"2004-11-29T13:42:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/29\/how-can-i-attach-a-file-to-an-email-sent-using-cdo\/"},"modified":"2004-11-29T13:42:00","modified_gmt":"2004-11-29T13:42:00","slug":"how-can-i-attach-a-file-to-an-email-sent-using-cdo","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-attach-a-file-to-an-email-sent-using-cdo\/","title":{"rendered":"How Can I Attach a File to an Email Sent Using CDO?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I understand how to use CDO to send an email, but how do I include an attachment with that email?<BR><BR>&#8212; RT<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>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\u2019t eating, playing tackle football with a phalanx of nephews much bigger and &#8211; sigh &#8211; much younger than he is. It\u2019s 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.<\/P>\n<P>For those of you who aren\u2019t 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:<\/P><PRE class=\"codeSample\">Set objEmail = CreateObject(&#8220;CDO.Message&#8221;)<\/p>\n<p>objEmail.From = &#8220;helpdesk@fabrikam.com&#8221;\nobjEmail.To = &#8220;administrator@fabrikam.com&#8221;\nobjEmail.Subject = &#8220;Server down&#8221; \nobjEmail.Textbody = &#8220;Server1 is no longer accessible over the network.&#8221;<\/p>\n<p>objEmail.Configuration.Fields.Item _\n    (&#8220;http:\/\/schemas.microsoft.com\/cdo\/configuration\/sendusing&#8221;) = 2\nobjEmail.Configuration.Fields.Item _\n    (&#8220;http:\/\/schemas.microsoft.com\/cdo\/configuration\/smtpserver&#8221;) = _\n        &#8220;smtpmailer&#8221; \nobjEmail.Configuration.Fields.Item _\n    (&#8220;http:\/\/schemas.microsoft.com\/cdo\/configuration\/smtpserverport&#8221;) = 25\nobjEmail.Configuration.Fields.Update<\/p>\n<p>objEmail.Send\n<\/PRE>\n<P>We\u2019re not going to explain this script in any detail today; for a quick overview of the individual properties used here, see the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_ent_wbpa.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>. You can probably figure out the <B>From<\/B>, <B>To<\/B>, <B>Subject<\/B>, and <B>Textbody<\/B> 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\u2019ll need to change is <B>smtpserver<\/B>. In our sample code, we\u2019ve referenced an SMTP mail server we named <B>smtpmailer<\/B>; you\u2019ll need to replace that with the name of <I>your<\/I> SMTP mail server.<\/P>\n<P>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\u2019t actually connect to Microsoft; these URIs are just properties. Why are they referenced like this? To be honest, we don\u2019t know. But don\u2019t worry: your mail doesn\u2019t 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\u2019s.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">objEmail.AddAttachment &#8220;C:\\Scripts\\Output.txt&#8221;\n<\/PRE>\n<P>That\u2019s it; add this line of code to the script, and you\u2019ll have yourself an attachment. The entire script will look something like this:<\/P><PRE class=\"codeSample\">Set objEmail = CreateObject(&#8220;CDO.Message&#8221;)<\/p>\n<p>objEmail.From = &#8220;helpdesk@fabrikam.com&#8221;\nobjEmail.To = &#8220;administrator@fabrikam.com&#8221;\nobjEmail.Subject = &#8220;Server down&#8221; \nobjEmail.Textbody = &#8220;Server1 is no longer accessible over the network.&#8221;\nobjEmail.AddAttachment &#8220;C:\\Scripts\\Output.txt&#8221;<\/p>\n<p>objEmail.Configuration.Fields.Item _\n    (&#8220;http:\/\/schemas.microsoft.com\/cdo\/configuration\/sendusing&#8221;) = 2\nobjEmail.Configuration.Fields.Item _\n    (&#8220;http:\/\/schemas.microsoft.com\/cdo\/configuration\/smtpserver&#8221;) = _\n        &#8220;smtpmailer&#8221; \nobjEmail.Configuration.Fields.Item _\n    (&#8220;http:\/\/schemas.microsoft.com\/cdo\/configuration\/smtpserverport&#8221;) = 25\nobjEmail.Configuration.Fields.Update<\/p>\n<p>objEmail.Send\n<\/PRE>\n<P>As long as we have your attention, we\u2019ve had a couple people ask us how they can send email if their SMTP server requires authentication. To tell you the truth, that\u2019s a tough question for us to answer because (for various reasons) we don\u2019t have a way to test that scenario. However, adding a user name and password to your script is a good place to start:<BR><\/P><PRE class=\"codeSample\">objEmail.Configuration.Fields.Item _\n    (&#8220;http:\/\/schemas.microsoft.com\/cdo\/configuration\/sendusername&#8221;) = &#8220;fabrikam\\kenmyer&#8221;\nobjEmail.Configuration.Fields.Item _ \n    (&#8220;http:\/\/schemas.microsoft.com\/cdo\/configuration\/sendpassword&#8221;) = &#8220;&amp;gr54#wgha&#8221;\n<\/PRE>\n<P>The preceding code logs you on to the SMTP server as <B>fabrikam\\kenmyer<\/B>, with the password <B>&amp;gr54#wgha<\/B>. Note that this <I>does<\/I> send the user name and password in cleartext. Consequently, you probably don\u2019t 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 <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/exchanchor\/htms\/msexchsvr_cdo_top.asp\"><B>CDO documentation<\/B><\/A> on MSDN.<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov04\/hey1129.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov04\/hey1129.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I understand how to use CDO to send an email, but how do I include an attachment with that email?&#8212; RT 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\u2019t eating, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[28,3,182,5],"class_list":["post-70923","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-messaging-and-communication","tag-scripting-guy","tag-sending-email","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I understand how to use CDO to send an email, but how do I include an attachment with that email?&#8212; RT 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\u2019t eating, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70923"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70923\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}