Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
Project "ASP/ASP.Net 3.5 Relaying"
    Purpose
The purpose of this project is to demonstrate how to send an email message from a website by using the SMTP service installed on the web server. The most likely use of the SMTP service in application code, whether ASP or ASP.Net 3.5, is when the application code encounters an error that you need to be aware of.


Make Sure SMTP is Setup
STEP 1

(Enlarge)


(Enlarge)
Under IIS Manager, you'll see an entry for the SMTP virtual server (it needs to be installed on the web server in order to use it). From here you can right-click on it to start it in addition to starting it under services. From here right-click on it and select properties.

NOTE: On the SMTP Server make sure the email address you may be relaying has the permission to relay.
STEP 2

(Enlarge)

Authentication properties.
STEP 3

(Enlarge)

Connection properties.
STEP 4

(Enlarge)

SMTP properties.
STEP 5

(Enlarge)

Relay properties.


Legacy ASP Code:

<%@ Import Namespace="System.Web.Mail" %>
<script runat="server">
'''''''''''''
' Mail Handler '
'''''''''''''
Public Sub mailSend()
Dim mailContainer As System.Web.Mail.SmtpMail
Dim mailContent As New System.Web.Mail.MailMessage()    
mailContainer.SmtpServer = "smtp.yoursite.com" ' IP or name of the SMTP Server
mailContent.From = "from@domain.com" ' Email address message is sent from
mailContent.To = "at@domain.com" ' Email address(s) to send to, separated by a semicolon
mailContent.BodyFormat = MailFormat.Text
mailContent.Subject = "Email Subject Line"
mailContent.Body = "Body of email message."
mailContainer.Send(mailContent)
End Sub
'''''''''''''
' Page Load '
'''''''''''''
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Send Message
mailSend()
End Sub
</script>


ASP.Net 3.5 Code:


<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server">
'''''''''''''
' Mail Handler '
'''''''''''''
Public Sub mailSend()
dim mailServer As String = "smtp.yoursite.com"
dim mailFrom As String = "from@domain.com"
dim mailTo As String = "at@domain.com"
dim mailPassword As String = "ifpasswordisrequired"
dim mailSubject As String = "Email Subject Line"
dim mailBody As String = "Body of email message."
dim mailContainer As New SmtpClient(mailServer)
dim mailContent As New MailMessage(mailFrom, mailTo, mailSubject, mailBody)
Try
                ' Try to send email anonymously
                mailContainer.Send(mailContent)
Catch ex As Exception
                if InStr(1, LCase(ex.Message), "relaying denied", vbTextCompare) = 0 AND InStr(1, LCase(ex.Message), "mailbox unavailable", vbTextCompare) = 0 Then
                                Try
                                                ' Provide email credentials and try to send message
                                                mailContainer.Credentials = New NetworkCredential(mailFrom, mailPassword) ' mailServer must allow this type of logon
                                                mailContainer.Send(mailContent)
                                Catch ex2 As Exception
                                                response.write("[ERROR 1] " & ex.Message & "<hr />" & ex.toString)
                                End Try
                Elseif InStr(1, LCase(ex.Message), "relaying denied", vbTextCompare) > 0 OR InStr(1, LCase(ex.Message), "mailbox unavailable", vbTextCompare) > 0 Then
                                ' Need to allow relaying and make sure the email address being sent to exists
                                response.write("[ERROR 2] " & ex.Message & "<hr />" & ex.toString)
                Else
                                ' Another type of error
                                response.write("[ERROR 3] " & ex.Message & "<hr />" & ex.toString)
                End if
End Try
End Sub
'''''''''''''
' Page Load '
'''''''''''''
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Send Message
mailSend()
End Sub
</script>
About Joe
Find Out Now!