Although I often refer people to use CDO mail, I have only briefly discussed it in prior postings and I never really gave any usage examples. Today that changes!
So what exactly is CDO Mail and when is it useful?
CDO mail is one of many techniques available to developer to send e-mails.
If you are an MS Access developer you are surely aware of the SendObject method, but it will only allow one attachment and that attachment has to be an Access object (not an external file).
Another very common approach is to use Outlook automation, but with new security impositions by Microsoft it has become unreliable and now requires workarounds. Furthermore, after a recent discussion with fellow MVPs, we find out that having an outdated virus definition can stop longstanding code from working! As such, although powerful, outlook automation simply is not reliable for true production. For production tools, one must then look at integrating tools such as Outlook Redemption, but the down side to this solution is that it requires registering a COM library for it to work.
Which brings us to CDO Mail!
CDO Mail is a technique that binds you directly to your e-mail server to send out email(s).
CDO Mail Pros
- No e-mail client (Outlook, Thunderbird, …) need be installed
- Hence you bypass any e-mail client limitations and/or security restrictions
- Permits multiple attachments to be included
- Permits external documents as attachments
- Accepts both plain text and HTML formatted content
- Can be used in any programs which uses VBA (MS Access, Excel, Word, PowerPoint, MS Project, …)
- Can even be used in VBScript
CDO Mail Cons
- Requires knowledge of all the server settings (port, username, password, authentication, …)
- Does not keep a copy of sent e-mails so CCing or BCCing yourself becomes useful for archive purposes
- Does not permit attaching database objects directly, but you can export them or print them as PDFs and attach those
CDO Mail VBA Code Sample
Below is a sample CDO Mail function I created many moons ago for my own purposes and have tweak slightly over the years.