Send email with classic asp

Below is an example function used in my classic asp code to send e-mail with Attachments. The e-mail is written in HTML format and attachments picked up from the physical path on the Server where the Application is hosted on IIS.

I’m using a hard-coded path in the sample below for the Attachment example. You can create a dynamic string by fetching the path from the DB.

I’m using CDOSYS mail provider object below to send e-mail.

Function GenerateEmailCEF(senderemail, recipient)
  MailProvider = "CDOSYS"
  EmailFmt = 0 'For HTML, 1 is for Plain Text ' SMTP Server Config 
  Dim rsSMTPSendUsing, rsSMTPServer, rsSMTPServerPort,   rsSMTPSendUsername, rsSMTPSendPwd, rsSMTPUseSSL Dim SMTPSendUsing, SMTPServer, SMTPServerPort, SMTPSendUsername, SMTPSendPwd, SMTPUseSSL
 SMTPSendUsing = "1"
 SMTPServer = "smtp.xx.xx" 'Change as per your configuration.
 SMTPServerPort = "25"
 SMTPSendUsername = ""
 SMTPSendPwd = ""
 SMTPUseSSL = "false"

 if MailProvider = "CDONTS" then set  objMail=CreateObject("CDONTS.NewMail")

 if MailProvider = "CDOSYS" then set objMail =CreateObject("CDO.Message")

 objMail.From = senderemail
 objMail.To = recipient
 objMail.Subject = "Test Subject"
 if MailProvider = "CDONTS" then objMail.MailFormat = EmailFmt
 if MailProvider = "CDONTS" then objMail.BodyFormat = EmailFmt
 'Created only HTML format
 emailbod = "<html?<HEAD?<TITLE?</TITLE?</HEAD?<BODY?"
 emailbod = emailbod & "<BR?Mail Body."

 'Add attachments to mail.
 newfn = "c:\Test\myfile.pdf" 'Path on the Server.

 if MailProvider = "CDONTS" then execute ("objMail.AttachFile(" & newfn & ")")

 if MailProvider = "CDOSYS" then execute ("objMail.AddAttachment(" & newfn & ")")
 if MailProvider = "CDONTS" then objMail.Body = emailbod
 if MailProvider = "CDOSYS" then objMail.HTMLBody = emailbod

 objMail.BodyPart.Charset = "UTF-8"
 objMail.HTMLBodyPart.Charset = "UTF-8"
 if MailProvider = "CDOSYS" then
  objmail.Configuration.Fields.Item ("") = SMTPSendUsing
 objmail.Configuration.Fields.Item ("") = SMTPServer
 objmail.Configuration.Fields.Item ("") = SMTPServerPort
 objmail.Configuration.Fields.Item ("") = SMTPUseSSL

  if SMTPSendusername <> "" then
    objmail.Configuration.Fields.Item ("") = SMTPSendusername
 objmail.Configuration.Fields.Item ("") = SMTPSendPwd
 objmail.Configuration.Fields.Item ("") = 1
  end if

 end if
 set objMail=Nothing
End Function

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.