I am in the process of moving one of my externally-hosted web sites to an internal host on my home network. But one of the problems I’ve been working on is how to continue sending e-mail messages without setting up my own SMTP relay host. I wanted to be able to make the process portable, so it could be used on any host as long as the server has an active Internet connection. Thanks to a post on Google Groups, I was able to do this. I’ve taken that code example and made it into a SUB so it’s a little easier to re-use as well. A small but useful modification…
'----------------------------------------------------------------
' function: send cdosys message using GMail as SMTP relay host
' from: http://groups.google.com/group/hosted-the-basics/browse_thread/thread/c6cc889c9db0a02b?pli=1
'----------------------------------------------------------------
Sub SendGmail(RecipientEmail, SenderEmail, Subject, msgBody, msgFormat)
On Error Resume Next
Dim SMTPServer, SMTPusername, SMTPpassword
SMTPserver = "smtp.gmail.com"
SMTPusername = "YOU@gmail.com"
SMTPpassword = "YOUR_PASSWORD"
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")
Set cdoMessage.Configuration = cdoConfig
cdoMessage.From = SenderEmail
cdoMessage.To = RecipientEmail
cdoMessage.Subject = Subject
If Ucase(msgFormat) = "TEXT" Then
cdoMessage.TextBody = msgBody
Else
cdoMessage.HTMLBody = msgBody
End If
cdoMessage.Send
Set cdoMessage = Nothing
Set cdoConfig = Nothing
If Err.Number <> 0 Then
Response.Write "error: " & _
err.Number & " - " & err.Description & _
"<br /><br />"
End If
End Sub
SendGmail SendTo, SendFrom, Subject, MessageBody, "TEXT"
Be sure to edit the values in RED above before trying to use this. Also, be sure to use your Gmail account as the SendFrom address value or it may not be delivered successfully. You can easily modify this to work as VBScript by changing the Response.Write statements to Wscript.Echo, if you want to.
In case you happen to be a TextPad user (like I am), I have added this to the ASP Clip Library posted on my Scripting Resources site at http://www.steinvb.net/scripting.php Yes, I know it’s a bit ironic that I post something about ASP on a page written in PHP, but that’s how my brain works anyway.
No comments:
Post a Comment