David Behan's Web Design Ireland Blog

ASP Send E-Mail Function

This neat little ASP send mail function can be included and called to quickly send e-mails using CDONTS, CDOSYS, ASPMail, ASPEmail, Dundas Mailer or Jmail. All you need to do is copy the code below into a file, say sendmail.asp and include that file in any page you want to send a mail in.

Copy the following lines of code into sendmail.asp:

<%
'--------------------------------------
'Notes - Read Me!
'--------------------------------------
'sToName - can be any type of text string, e.g. Joe Bloggs
'sToAddress - must be a valid email address as this is the email address the mail will be sent, e.g. joe@bloggs.com
'sFromName - can be any type of text string, e.g. Mary Bloggs
'sFromAddress - must be a valid email address as this is the email address the mail will come from, e.g. mary@bloggs.com
'sSubject - can be any type of text string, e.g. July Newsletter
'sContent - this is the actual body content of the email. can be html or just text, just make sure you have set the correct mail body settings
'sFormat - enter 'html' or 'text'
'sImportance - enter 'high', 'normal' or 'low'

sComponent = "jmail" 'can be cdonts, cdosys, aspmail, aspemail, dundas, jmail
sMailServer = "mail.yourdomain.com"

FUNCTION SendMail(sToName, sToAddress, sFromName, sFromAddress, sSubject, sContent, sFormat, sImportance)

SELECT CASE sComponent

'-------------------------------
'CDONTS - www.microsoft.com
'-------------------------------
CASE "cdonts"

SET objMail = Server.CreateObject("CDONTS.NewMail")

IF sFormat = "text" THEN
objMail.BodyFormat = 1
objMail.MailFormat = 1
ELSE
objMail.BodyFormat = 0
objMail.MailFormat = 0
END IF

IF sImportance = "high" THEN
objMail.Importance = 2
ELSEIF sImportance = "low" THEN
objMail.Importance = 0
ELSE
objMail.Importance = 1
END IF

objMail.From = sFromName & "<" & sFromAddress & ">"
objMail.To = sToName & "<" & sToAddress & ">"
objMail.Subject = sSubject
objMail.Body = sContent
objMail.Send

'-------------------------------
'CDONTS - www.microsoft.com
'-------------------------------
CASE "cdosys"

SET objMail = Server.CreateObject("CDO.Message")
Set objConfig = Server.CreateObject ("CDO.Configuration")

objConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = sMailServer 'change this to your websites mail domain
objConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")  = 25
objConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
Set objMail.Configuration = objConfig

IF sFormat = "text" THEN
objMail.TextBody = sContent
ELSE
objMail.HtmlBody = sContent
END IF

IF sImportance = "high" THEN
objMail.Fields ("urn:schemas:httpmail:importance").Value = 2
objMail.Fields.Update()
ELSEIF sImportance = "low" THEN
objMail.Fields ("urn:schemas:httpmail:importance").Value = 0
objMail.Fields.Update()
ELSE
objMail.Fields ("urn:schemas:httpmail:importance").Value = 1
objMail.Fields.Update()
END IF

objMail.From = sFromName & "<" & sFromAddress & ">"
objMail.To = sToName & "<" & sToAddress & ">"
objMail.Subject = sSubject
objMail.Send

Set objMail = Nothing
Set objConfig = Nothing

'-------------------------------
'ASPMail - www.serverobjects.com
'-------------------------------
CASE "aspmail"

SET objMail = Server.CreateObject("SMTPsvg.Mailer")

IF sFormat = "text" THEN
objMail.CharSet = 2
Else
objMail.ContentType = "text/html"
End If

If sImportance = "high" Then
objMail.Priority = 1
ElseIf sImportance = "low" Then
objMail.Priority = 5
Else
objMail.Priority = 3
End If

objMail.FromName = sFromName
objMail.FromAddress= sFromAddress
objMail.RemoteHost = sMailServer
objMail.Subject = sSubject
objMail.BodyText = sContent
objMail.AddRecipient sToAddress, sToName
objMail.SendMail

'-------------------------------
'ASPEmail - www.aspemail.com
'-------------------------------
Case "aspemail"

Set objMail = Server.CreateObject("Persits.MailSender")

If sFormat = "text" Then
objMail.Ishtml = False
Else
objMail.Ishtml = True
End If

If sImportance = "high" Then
objMail.Priority = 1
ElseIf sImportance = "low" Then
objMail.Priority = 5
Else
objMail.Priority = 3
End If

objMail.Host = sMailServer
objMail.From = sFromAddress
objMail.FromName = sFromName
objMail.AddAddress sToAddress, sToName
objMail.Subject = sSubject
objMail.Body = sContent
objMail.Send

'-------------------------------
'JMail - www.dimac.net
'-------------------------------
Case "jmail"

set objMail = Server.CreateOBject("JMail.Message")

If sImportance = "high" Then
objMail.Priority = 1
ElseIf sImportance = "low" Then
objMail.Priority = 5
Else
objMail.Priority = 3
End If

If sFormat = "text" Then
objMail.body = sContent
Else
objMail.HTMLBody = sContent
END IF

objMail.Logging = true
objMail.silent = true
objMail.From = sFromAddress
objMail.FromName = sFromName
objMail.AddRecipient sToAddress, sToName
objMail.Subject = sSubject
objMail.Send(sMailServer)

'-------------------------------
'Dundas - www.dundas.com
'-------------------------------
Case "dundas"

set objMail = Server.CreateOBject("Dundas.Mailer")

If sImportance = "high" Then
objMail.Priority = 1
ElseIf sImportance = "low" Then
objMail.Priority = 5
Else
objMail.Priority = 3
End If

If sFormat = "text" Then
objMail.body = sContent
Else
objMail.htmlBody = sContent
End If

objMail.FromAddress = sFromAddress
objMail.FromName = sFromName
objMail.TOs.Add sToAddress, sToName
objMail.Subject = sSubject
objMail.SMTPRelayServers.Add sMailServer
objMail.SendMail

End Select

END FUNCTION
%>

To send the mail, include sendmail.asp and use the following lines of code:

CALL SendMail("Joe Bloggs","joe@bloggs.com","Jane Bloggs","jane@bloggs.com","Test Email","Test Content","html","high")

2 Comments (post a comment)

gravatar

Vu Quynh  spacer

I find your block on google.com.vn! It’s well, thank you!

September 6, 2007 @ 3:03 am
gravatar

David  spacer

You are most welcome! :D

September 25, 2007 @ 6:35 am

RSS feed for comments on this post · TrackBack URI

Leave a Comment