Library code snippets
Sending email from ASP.NET
One of the most commonly performed operations that I see in web applications - aside from database actions - is sending email from code. In Active Server Pages 3.0 and earlier, this task required the use of a third-party control such as ASPMail (from ServerObjects) or ASPEmail (from Persits). Both of these COM objects work quite well, but they are no longer required when dealing with ASP.Net
The only real "requirements" are for the .Net framework and the Microsoft SMTP service to be installed on the web server. After that you can "just do it". Below is a sample page that presents a form to the user and allows them to specify the To, From, Subject, and Body of an email message, then sends the email when the "Send" button is clicked.
01: <%@page language="VB" %>
02: <%@Import Namespace="System.Web.Mail" %>
03: <HTML><BODY>
04:
05: <SCRIPT LANGUAGE="VB" RUNAT="server">
06:
07: Sub SendMail (Obj As Object, E As EventArgs)
08:
09: Dim mailObj AS new MailMessage
10:
11: mailObj.From = MsgFrom.text
12: mailObj.To = MsgTo.Text
13: mailObj.Subject = MsgSubject.Text
14: mailObj.Body = MsgBody.Text
15:
16: SmtpMail.SmtpServer = "localhost"
17: SmtpMail.Send(mailObj)
18: End Sub
19: </SCRIPT>
20:
21: <form runat="server">
22: <table>
23: <tr>
24: <td>Send To:</td>
25: <td><asp:Textbox id="MsgTo" runat="server"/></td>
26: </tr>
27: <tr>
28: <td>Send From:</td>
29: <td><asp:Textbox id="MsgFrom" runat="server"/></td>
30: </tr>
31: <tr>
32: <td>Message Subject:</td>
33: <td><asp:Textbox id="MsgSubject" runat="server"/></td>
34: </tr>
35: <tr>
36: <td>Message Body:</td>
37: <td><asp:Textbox TextMode="multiline" Rows="5" id="MsgBody" runat="server"/></td>
38: </tr>
39: <tr>
40: <td> </td>
41: <td><asp:button Text="Send" onclick="SendMail" id="Send" runat="server"/></td>
42: </tr>
43: </table>
44: </form>
45: </BODY>
Lines 20 through 43 are just some standard ASP.Net HTML controls that are used to generate an HTML form for the user. One the "Send" button on the form is clicked, the code in the SendMail subroutine (starting on line 07) is executed.
The first line in the subroutine creates a MailMessage object. This object is available to our script because we have included a reference to the "System.Web.Mail" namespace as noted on line 02. This object basically mirrors the options of a standard email message, so the naming is very intuitive. In lines 11 through 14 we set the properties of the new MailMessage object based on the values from the form variables. Once the properties are set, we only need to call SmtpMail.Send, as noted on line 16, to execute the delivery of the mail message.
There are other properties that you can set, such as the message priority, whether it should be text or HTML, and the encoding type. More information about these additional properties should be available in the ASP.Net framework documentation.
Related articles
Related discussion
-
sharepoint calendar web part with events from sql table
by tukubapi2207 (1 replies)
-
Using FedEx Web Service to Calculcate Shipping Cost
by bhora123 (4 replies)
-
Very Urgent regarding deleting the images from a folder
by rameshbandi (2 replies)
-
Dynamically Generating PDFs in .NET
by nike12 (10 replies)
-
New style of Javascript used in extenders.
by mittalpa (0 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
Err :The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for nshanmugaraj@gmail.com
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.COMException: The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for nshanmugaraj@gmail.com
Source Error:
Line 20: mailObj.Body = MsgBody.Text
Line 21: SmtpMail.SmtpServer = "localhost"
Line 22: SmtpMail.Send(mailObj)
Line 23: End Sub
Line 24: </SCRIPT>
Source File: D:\ASPdotNET\Email\WebForm1.aspx Line: 22
---
I am new to asp.net .. can u kindly rectify my doubt..
I have a problem. The script only works when the SMTP server = localhost
It wont work with my server name EIBERGEN01 or the IP adres = 192.168.3.5
Could someone explain to me why.
This thread is for discussions of Sending email from ASP.NET.