Using SendGrid with Classic ASP to send emails

I recently needed to migrate my baby change location website to a new Windows server. A part of this, naturally, is setting up SMTP to allow the site to send emails when necessary.

hate setting up SMTP on Windows, it always seems like such a fight. So I opted to use SendGrid instead.

What is SendGrid?

Essentially SendGrid is a cloud based email platform that removes a lot of the headaches around sending emails from your website or app. It also provides good analytics and an API. Oh yes, and there’s a free plan which gives you up to 200 emails free per day – perfect for personal projects.

How to use SendGrid with Classic ASP

A note to Windows Server 2008 / Windows 7 users: you will first need to install CDO from Microsoft to get this to work.

Here is a function giving you an idea of how to implement SendGrid in Classic ASP applications using CDO. As you’ll be able to see from the script it is expecting various pieces of information such as the sender and recipient email addresses.

function sendEmail()
Dim iMsg
Dim iConf
Dim Flds
Dim strHTML
Const cdoSendUsingPickup = 1
set iMsg = CreateObject("CDO.Message")
set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
dim sch : sch = ""
With Flds
 .Item(sch & "sendusing") = 2
 .Item(sch & "smtpserver") = ""
 .Item(sch & "smtpserverport") =465
 .Item(sch & "smtpconnectiontimeout") = 10
 .item(sch & "smtpauthenticate") = 1 'basic auth
 .item(sch & "smtpusessl") = True
 .item(sch & "sendusername") = "YOUR_SENDGRID_USERNAME"
 .item(sch & "sendpassword") = "YOUR_SENDGRID_PASSWORD"
End With
' Build HTML for message body.
strHTML = "<html lang=""en""><head><meta http-equiv=""content-type"" content=""text/html; charset=iso-8859-1"" />" & chr(13) &_
 "<title>Email title</title></head>" & chr(13) &_
 "<body>" & chr(13) &_
 " <table border=""0"" cellpadding=""0"" cellspacing=""0"" width=""600""><tr>" &_
 "<td style=""font-family:trebuchet,trebuchet ms, arial, sans-serif; font-size:12px"">" & chr(13) &_
 " </td></tr></table>" & chr(13) &_
Dim iBP
With iMsg
 Set .Configuration = iConf
 .To = ToName & ""
 .Cc = CCemail
 .From = "FROM_NAME "
 .Subject = strSubject
 .HTMLBody = cstr(strHTML)
End With
' Clean up variables.
Set iBP = Nothing
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
end function

Any problems or questions, drop me a comment.