ASP CDO Send Email Through External Server

ASP Best Practices

Use ASP and CDO to send email through an external email server with authentication.

Date : 2006-04-27
Remember when sending email was simple. Now many people find that the SMTP server that they are using isn't even on the localhost.

With all of the complicated options for sending email here is a very simple function that will send email through an external server. It will attach your attachments if you send them, it will detect if your message is HTML (as long as it has it's tag up at the top). One could easily make a better HTML detecting script, but for display purposes this works. And best of all, it sends the email. Just look for the SMTP Server, Username, and Password fields and change them to match your configuration. It's unlikely but you may also need to change the Port that is used to send the email.

Enjoy the function, and, as always, let me know if you find any way to improve on it.

function SendEmail(sFrom, sTo, sCC, sBCC, sSubject, sMessage, Attachments)
  Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
  Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
  Const cdoAnonymous = 0 'Do not authenticate
  Const cdoBasic = 1 'basic (clear-text) authentication
  Const cdoNTLM = 2 'NTLM
On ERROR Resume Next
  if sCC = "" then sCC = Null
  if sBCC = "" then sBCC = Null
  Set objMessage = CreateObject("CDO.Message")
  objMessage.Subject = sSubject
  objMessage.from = sFrom
  objMessage.To = sTo
  if NOT sCC = null then objMessage.CC = sCC
  if NOT sBCC = null then objMessage.BCC = sBCC
  if instr(1,sMessage,"<html>") < 5 then
  objMessage.HTMLBody = sMessage
    objMessage.TextBody = sMessage
  end if
  if not isEmpty(Attachments) then
    objMessage.Fields.Item("urn:schemas:mailheader:Content-Type") = "multipart/mixed"
    objMessage.MimeFormatted = true
    aAttach = split(Attachments,",")
  for each fle in aAttach
    if not isEmpty(fle) then objMessage.addAttachment("file://" & fle)
  end if
'provide the configuration information for the remote SMTP server.
  objMessage.Configuration.Fields.Item _
    ("") = 2
'Name or IP of Remote SMTP Server
  objMessage.Configuration.Fields.Item _
    ("") = "WWW.MYSERVER.COM"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
  objMessage.Configuration.Fields.Item _
    ("") = cdoBasic
'Your UserID on the SMTP server
  objMessage.Configuration.Fields.Item _
    ("") = "MYUSERNAME"
'Your password on the SMTP server
  objMessage.Configuration.Fields.Item _
    ("") = "MYPASSWORD"
'Server port (typically 25)
  objMessage.Configuration.Fields.Item _
    ("") = 25
'Use SSL for the connection (False or True)
  objMessage.Configuration.Fields.Item _
    ("") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
  objMessage.Configuration.Fields.Item _
    ("") = 60
'End remote SMTP server configuration
  if err.number > 0 then
    response.write err.number & err.message
  end if
  SendEmail =  err.number 
end function

Comments :

No comments yet
  • Search For Articles