SearchSearch   MemberlistMembers   RegisterRegister   ProfileProfile   Log inLog in 

Send CDO Email Using an External SMTP Server (ASP)

 
Post new topic   Reply to topic    Open Hosting Internet Solutions Forum Index -> Articles and Tutorials -> Server Side Web Development
View previous topic :: View next topic  
Author Message
Paul Creedy
Sleep Typist


Joined: 19 Jun 2002
Posts: 1238
Location: Rhondda Cynon Taff South Wales UK

PostPosted: Wed Mar 08, 2006 3:42 pm    Post subject: Send CDO Email Using an External SMTP Server (ASP) Reply with quote

Send CDO Email Using an External SMTP Server (ASP)

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

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.

Code:
<%
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:
Code:
<%
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
%>
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Open Hosting Internet Solutions Forum Index -> Articles and Tutorials -> Server Side Web Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.11 © 2001, 2002 phpBB Group
FAQClick here for help using the phpBB forum