If you're using a Classic ASP-based application and need to send emails through an SMTP server (with authentication), the following script will help you achieve that using CDO (Collaboration Data Objects).
A working SMTP server (e.g., mail.yourdomain.com or an IP address)
SMTP credentials (username and password)
Classic ASP support enabled on your web server
Copy and paste the following code into your .asp
file. Be sure to update the configuration fields with your actual SMTP server details and email content.
<%
Dim intError
Dim strResult
Function SendMail(SMTPServer, SMTPUserName, SMTPPassword, EMailFrom, EMailTo, EMailCc, EMailBcc, EMailSubject, EMailType, EMailContent)
Dim cdoConfig
Dim cdoMessage
Dim intErr
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 ' cdoSendUsingPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTPUserName
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SMTPPassword
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 ' Or 587/465 depending on your mail server
.Update
End With
Set cdoMessage = CreateObject("CDO.Message")
With cdoMessage
Set .Configuration = cdoConfig
If Len(Trim(CStr(EMailFrom))) = 0 Then
.From = SMTPUserName
Else
.From = EMailFrom
End If
.To = EMailTo
If Len(Trim(CStr(EMailCc))) <> 0 Then .Cc = EMailCc
If Len(Trim(CStr(EMailBcc))) <> 0 Then .Bcc = EMailBcc
.Subject = EMailSubject
If EMailType = "text" Then
.TextBody = EMailContent
ElseIf EMailType = "html" Then
.HTMLBody = EMailContent
End If
.Send
intErr = Err.Number
End With
Set cdoMessage = Nothing
Set cdoConfig = Nothing
SendMail = intErr
Session("ErrDescription") = Err.Description
End Function
intError = SendMail("your-mailserver-ip", "smtp-username@domain.com", "smtp-password", "from@example.com", "to@example.com", "", "", "Test Subject", "text", "This is the body of the email.")
If intError = 0 Then
strResult = "The mail has been sent successfully."
Else
strResult = "There was a problem sending the mail."
End If
Response.Write(strResult)
%>
Parameter | Description |
---|---|
SMTPServer |
The IP or hostname of your SMTP server |
SMTPUserName |
SMTP login username |
SMTPPassword |
SMTP login password |
EMailFrom |
Sender's email address |
EMailTo |
Recipient's email address |
EMailCc |
(Optional) CC recipient(s) |
EMailBcc |
(Optional) BCC recipient(s) |
EMailSubject |
Subject line of the email |
EMailType |
"text" for plain text or "html" for HTML |
EMailContent |
The content/body of the email |
Use port 25, 465, or 587 depending on your SMTP provider.
If using port 465, you may need to configure SSL support, which isn’t covered in this basic script.
Make sure your hosting provider allows outbound SMTP connections.
Always sanitize input when sending user data via email.