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. |
(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. |
|
(Enlarge) |
Authentication properties. | |
(Enlarge) |
Connection properties. | |
(Enlarge) |
SMTP properties. | |
(Enlarge) |
Relay properties. | |
<%@ 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>