Skip to content

Instantly share code, notes, and snippets.

@adamalesandro
Last active December 6, 2021 13:17
Show Gist options
  • Save adamalesandro/e7821356128a551e7211 to your computer and use it in GitHub Desktop.
Save adamalesandro/e7821356128a551e7211 to your computer and use it in GitHub Desktop.
Sample Classic ASP function to send email through mail gun's api
<%
'---- Sample code to send email though MailGun
'---- You must populate your base url and api-key below
Function SendMailSync(toAddress, fromAddress, subject, body, htmlBody)
Dim httpPostData
Dim mailGunMessageUrl
Const MAILGUN_BASE_URL = "https://api.mailgun.net/v3/{DOMAIN HERE}"
Const MAILGUN_API_KEY = "key-{API_KEY}"
httpPostData = "from=" & fromAddress
httpPostData = httpPostData & "&to=" & toAddress
httpPostData = httpPostData & "&subject=" & subject
httpPostData = httpPostData & "&text=" & body
httpPostData = httpPostData & "&html=" & htmlBody
set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
mailGunMessageUrl = MAILGUN_BASE_URL & "/messages"
http.Open "POST", mailGunMessageUrl, false, "api", MAILGUN_API_KEY
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "Authorization", "Basic AUTH_STRING"
http.Send httpPostdata
If http.status <> 200 Then
Response.Write "An error occurred: " & http.responseText
End If
SendMailSync = http.responseText
Set http = Nothing
End Function
SendMailSync "adam@example.com", "No Reply noreply@example.com", "Test", "Test Mail", ""
%>
@prajapatichintan
Copy link

Make sure you use 465 as port for Mailgun. otherwise you will get error

CDO.Message.1 (0x80040213) The transport failed to connect to the server.

@edrimis
Copy link

edrimis commented Sep 5, 2018

Sometimes I got the error in line command http.Send httpPostdata

msxml6.dll The server name or address could not be resolved

Have any idea about that?

@computersols
Copy link

hi there,

I am trying to use mailguns smtp service with a classic asp website do you have any examples.. my code is as follows:

Set ObjSendMail = CreateObject("CDO.Message") 
'This section provides the configuration information for the remote SMTP server.
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.eu.mailgun.org"
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587 
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx"
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yyy" 
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False)
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
ObjSendMail.Configuration.Fields.Update	


ObjSendMail.Subject = "Email (through mailgun) received verification from "&request.querystring("AdminManagerEmail")
ObjSendMail.From =request.querystring("AdminManagerEmail")
ObjSendMail.TO = request.form("Email")
ObjSendMail.HTMLBody = "This is a test email to make sure you can receive emails from "&request.querystring("AdminManagerEmail")&". Please reply back."	
ObjSendMail.send

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment