Send CDO Email Using an External SMTP Server (ASP)

Written by Paul Creedy published 8th Mar 2006 | Comment on this article

Sending a CDO email using an external SMTP server using ASP (can also be used to send using an internal SMTP email server).

Send CDO Email Using an External SMTP Server (ASP)

Introduction

It is sometimes advantageous to use an external mail server for the following reasons:

  • You want to keep your web hosting and email separate (not to have all your eggs in one basket)
  • Your host doesn't allow bulk sending of emails (see your host for their definition of bulk)
  • Reducing server load on the web site when the email are being sent
  • If the SMTP server?s IP gets blacklisted, it won?t effect normal web site hosted email.

Before you start you need access to an external SMTP server and have the domain name, username and password given to you by the server host.

In the following example substitute your details as follows:
Smtp.yoursever.com = your server domain name
Yourusername = the username given to you by the host
Yourpassword = the password given to you by the host

The Code

An example of calling the function follows. The function will return and errorcode of >0 if an error was generated. This allows you to trap and deal with the error instead of throwing a server error page at your user.

<%
Dim errorcode
errorcode=SendCDOEmail("smtp.yourserver.com","yourusername","yourpassword","subject goes here","message goes here","from email address","to email address","","priority eg 1","email format")
Response.Write(errorcode)
%>

The function itself:

<%
Function SendCDOEmail(smtpserver,username,password, strSubject, strBody,strFromAddress,strToAddress,strCc, intpriority, strBodyFormat)
'smtpserver = your smtp server name
'username = the username required for smtp authorisation
'password= the password required for smtp autorisation
'strSubject= subject text
'strBody = body text / html
'strFromAddress= from whom the email will be sent
'strToAddress= who the email will be sent to
'strCc= to who the email will be Cc'd to
'intpriority= importance of email (0=low, 1=normal, 2=high)
'strBodyFormat= either HTML or text
' response.Write("<b>Debug Code:</b> subject = " & strSubject & " , Body = "& strBody & "From = " & strFromAddress & " ,To= " & strToAddress & " , cc= " & strCc & " , priority = " & intpriority & " , Body Format = " & strBodyFormat & "<br>" & vbcrlf) 'debug code
'set up connection
' send email using cdosys
dim objCDOSysCon, objCDOSysMail
'Create the e-mail server object
Set objCDOSYSMail = Server.CreateObject("CDO.Message")
Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")
'Set and update fields properties
With objCDOSYSCon
'Outgoing SMTP server
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = username
.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = password

.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'CDO Port
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Timeout
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Fields.Update
End With
'Update the CDOSYS Configuration
Set objCDOSYSMail.Configuration = objCDOSYSCon
'Set and update email properties
With objCDOSYSMail
'0=Low, 1=Normal, 2=High
.Fields("urn:schemas:httpmail:importance").Value = intpriority '0=low, 1=normal, 2=high
.From = strFromAddress 'who the email is from
.To = strToAddress 'who the email is sent to
.Cc = strCc 'who the email is Cc'd to
'The subject of the email
.subject = strSubject
'set the email body format
if ucase(strBodyFormat) ="HTML" then
.HTMLBody = strBody
else
.textbody = strbody
end if
.fields.update
On Error Resume Next
.send
SendCDOEmail=Err
end with
'clean up
set objCDOSysMail = nothing
set objCDOSysCon = nothing
End Function
%>