Eric's Webspace
Sending Email with Scripting
 

Many people ask me how to automate sending an email. How you decide to automate sending an email is very personal. There is no single right way.



Automating Outlook  Before Microsoft started thinking about security, this was easy. Now every time you try to send an email, you get a warning that an external program is accessing your address book or is trying to send mail. Honestly, that's how viruses spread, and there is no difference between your legitimate mail script and a virus as far as Outlook can determine. So unless you disable Outlook's security or create a separate script that will simulate a user by pressing the "OK" button on all the security dialogs, you won't get far.
http://msdn.microsoft.com/library/en-us/dnout98/html/msdn_movs105.asp

Browser Form Email  Once again, security took most of the fun out of it. This used to be the "no programming" way to send email. HTML forms usually use the "get" or "post" methods, but a few years ago browsers started supporting "mailto" as an action. That meant everybody could use forms to collect data, take orders, and act like the big dogs! Unfortunately, it also meant every time you hit the "Submit" button on a form, you might be disclosing your email address, IP address, and name to someone you'd probably rather not have that information. As a result, browsers now warn you  when you submit a form that uses mailto for the action. And modern script security stops scripts from automatically submitting a mailto form. However, when web pages are run from your hard drive, most warnings are disabled. So this method is still good for some things. Here's a couple of how-to pages:
http://wdvl.com/Authoring/HTML/Forms/mailto.html
http://www.netmechanic.com/news/vol3/form_no4.htm

CDO  Windows 2000 workstation has CDO, but it wants to use a locally-installed SMTP server. Which only exists on servers, not workstations! However, you can make it work with an outside smtp server:
http://msdn.microsoft.com/library/en-us/cdosys/html/_cdosys_configuration_coclass.asp
Try this sample code

Dim mail 'As CDO.Message
Dim conf 'As CDO.Configuration
Const NAMESPACE = "http://schemas.microsoft.com/cdo/configuration/"

Const cdoSendUsingPort = 2
Set mail = CreateObject("CDO.Message")
Set conf = CreateObject("CDO.Configuration")
conf.Fields(NAMESPACE & "sendusing") = cdoSendUsingPort
conf.Fields(NAMESPACE & "smtpserver") = "mx.somewhere.com"
conf.Fields(NAMESPACE & "smtpserverport") = 25

'Below four lines only needed with authenticating servers (SBC DSL)
'
Const cdoBasic = 1
'conf.Fields(NAMESPACE & "sendusername") = "username"
'conf.Fields(NAMESPACE & "sendpassword") = "password"
'conf.Fields(NAMESPACE & "smtpauthenticate") = cdoBasic

conf.Fields.Update
mail.Configuration = conf
mail.TextBody = "Hello"
mail.HTMLBody = "<html><body>Hello</body></html>"
mail.AddAttachment "C:\data.zip"
mail.From = """My Name"" <my_email@myisp.com>"
mail.Subject = "Testing"
mail.To = """Some Body"" <somebody@somewhere.com>;""Another Person"" <anotherperson@somewhere.com>"
mail.Send


CDONTS  As far as I've been able to determine, it isn't good for anything unless you happen to be running your scripts on a genuine NT/2000 server  (not a workstation) with the SMTP service installed. If that's the case, you're probably already Microsoft certified and aren't reading this!

MAPI  You may hear about it, but it won't help you with scripts or batch files. MAPI is a generic interface to the default mail client on a PC. However, MAPI is accessed entirely through ordinary Windows DLL files which scripts and batch files can't manipulate.

SMTP When There is No SMTP  If you use Lotus or Exchange or some other proprietary mail system, your best bet is to try really, really hard to find an SMTP mail server! Scan your internal network on port 25 (the smtp port). Or just try telnetting to port 25 to web servers and other likely machines in your intranet (use a command like telnet www.yourcompany.com 25). If you hit a mail server, it will probably respond with "220" and some descriptive text. Just type "quit" to get out. Also see if names like "mail" or "smtp" or "mx" (for example mail.yourcompany.com, etc.) are valid. See if any of them will work as mail servers. If you have NT or newer, try running the nslookup command like this nslookup -querytype=MX yourcompany.com. That will tell you what machine(s) in your company receive incoming email from the outside world. Those same machines can probably be used to send outgoing email! If you can use the nslookup command,one foolproof trick is to find the MX machine for the domain you want to send mail to. Then use that machine as your smtp mail server when sending email to that domain (and only for email addressed for that domain). As a last resort, you can do a web search for web-based email services that offer SMTP. If you can't find anything else, try www.hotpop.com.

SMTP From the Command Line  You can send email from a simple batch file. I've used both the below command-line emailers with no problems, but prefer BLAT:
http://blat.sourceforge.net/
http://www.xwebware.com/products/commail/

SMTP via ActiveX Control  Try these links for objects which scripting can manipulate to send email :
http://tech.dimac.net/
http://www.15seconds.com/focus/Email.htm
Some of the links may provide VB sample code, while others will provide ASP sample code. VBScript isn't that much different. My personal preference is JMail from Dimac (the first link). I've used an older SMTP-only version of their product for years with no problems. 


 

Lost? Look at the site map.

Bad links? Questions? Send me mail.

Google
Yahoo
Ask Jeeves