Send SMTP Gmail by a Local Script
I was reading about something different, when I stumbled upon the SendKeys Method—a scriptable method for controlling "applications that have no automation interface" by simulating keystrokes. Pretty cool.
Even cooler, however, is the following. This program prompts you for your Google-hosted mail login details (such as Gmail, Google Apps, or Google Apps for Education), then for a recipient, subject, message, and the path to an attachment (which must be less than 25 MB). It then uses the Collaboration Data Object to send the email using smtp.gmail.com. Witchcraft, I tell you.
To try this out, copy the code into a text document, and save it with the extension .wsf . Only works on Windows, AFAIK.
<package>
<job id="vbs">
<script language="VBScript">
' set shell = createobject("wscript.shell")
On Error Resume Next
Dim Subject, Body, SenderEmail, RecipientEmail, SMTPServer, SMTPusername, SMTPpassword
SMTPserver = "smtp.gmail.com"
SMTPusername = InputBox("Full Gmail or Google Apps email address")
SenderEmail = SMTPusername
SMTPpassword = InputBox("Password")
Subject = InputBox("Subject")
RecipientEmail= InputBox("Recipient")
Body = InputBox("Enter your message:")
sch = "http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(sch & "smtpauthenticate") = 1
.Item(sch & "smtpusessl") = True
.Item(sch & "smtpserver") = SMTPserver
.Item(sch & "sendusername") = SMTPusername
.Item(sch & "sendpassword") = SMTPpassword
.Item(sch & "smtpserverport") = 465
.Item(sch & "sendusing") = 2
.Item(sch & "connectiontimeout") = 100
.update
End With
Const cdoSendUsingPickup = "c:\inetpub\mailroot\pickup"
Set cdoMessage = CreateObject("CDO.Message")
With cdoMessage
Set .Configuration = cdoConfig
cdoMessage.From = SenderEmail
cdoMessage.To = RecipientEmail
cdoMessage.Subject = Subject
cdoMessage.TextBody = Body
ExecuteBlock = False
If ExecuteBlock Then
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "Text Documents|*.txt|All Files|*.*"
ObjFSO.FilterIndex = 2
InitFSO = ObjFSO.ShowOpen
If InitFSO = False Then
Wscript.Echo "Script Error: Please select a file!"
Wscript.Quit
Else
Wscript.Echo "You selected a file, great!"
End If
Wscript.Echo "You selected the file: " & ObjFSO.FileName
End If
AttachmentPath = InputBox("Full path to an attachment. Leave blank for none.")
If AttachmentPath <> "" Then
CdoMessage.AddAttachment AttachmentPath
End If
cdoMessage.Send
End With
Set cdoMessage = Nothing
Set cdoConfig = Nothing
If Err.Number <> 0 Then
Response.Write (Err.Description& "<br><br>")
end if
MsgBox "Message probably sent. At least, the script's now over."&VbCrLf&VbCrLf&"To: "&RecipientEmail&VbCrLf&"From: "&SenderEmail&VbCrLf&"Subject: "&Subject&VbCrLf&"Message: "&VbCrLf&Body&"",64,"Sendmail3.wsf"
</script>
</job>
</package>
Actually, this is the first non-hello-world program I've written for a non-web-scripting environment. I'm pretty sure it's in VBScript, which may or may not be the same thing as (or even related to) Visual Basic.
Ok, so I didn't actually write it myself. I modified the seed from bachir8k on this page. The attachment bit (and, of course, the InputBox() parts), however is all me.
As you can see there beneath If ExecuteBlock Then, I tried to get it to use the Common File Dialog, but it turns out that you need extra software for that, and I wanted this to be as pick-up-and-go as possible.

