100% found this document useful (2 votes)
285 views

Rakesh Gupta Send Mail Via SMTP Mail

To send an email from an ASP.NET web page, one must import the System.Web.Mail namespace, create an instance of the MailMessage class, set properties like From, To, Subject and Body on the MailMessage instance, and then send the message using the SmtpMail.Send method, passing in the MailMessage instance. This allows easy configuration and sending of emails directly from ASP.NET code.

Uploaded by

Rakesh Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
285 views

Rakesh Gupta Send Mail Via SMTP Mail

To send an email from an ASP.NET web page, one must import the System.Web.Mail namespace, create an instance of the MailMessage class, set properties like From, To, Subject and Body on the MailMessage instance, and then send the message using the SmtpMail.Send method, passing in the MailMessage instance. This allows easy configuration and sending of emails directly from ASP.NET code.

Uploaded by

Rakesh Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

The .

NET framework makes the task of sending email from a


Web page unbelievably easy and I'll show you how to do it in a
matter of minutes. This article presumes that you have a basic
understanding of how .NET and ASP.NET works.

1. Import the namespace, and create instances of


required classes

We are going to use the SmtpMail and MailMessage classes, which


belong to the System.Web.Mail namespace. In order to use them,
we will need to import the System.Web.Mail namespace like this:

<%@ Import Namespace="System.Web.Mail" %>

The MailMessage class provides properties and methods for


constructing an email message. To build our email message we
need to create an instance of this class:

Dim objMail As New MailMessage()

2. Set the properties of the object

Next, we have to set all the properties of the objMail object:

' The email address of the sender


objMail.From = "[email protected]"

' The email address of the recipient


objMail.To = "[email protected]"

' The email address of the Cc recipient


objMail.Cc = "[email protected]"

' The email address of the Bcc recipient


objMail.Bcc = "[email protected]"

' The format of the message - it can be MailFormat.Text


or MailFormat.Html
objMail.BodyFormat = MailFormat.Text
' The priority of the message - it can be MailPriority.High,
MailPriority,Normal or MailPriority.Low
objMail.Priority = MailPriority.High

'The subject of the message


objMail.Subject = "My first ASP.NET email"

'The message text


objMail.Body = "This is my first email sent via
ASP.NET. It was easier than I thought :)"

After all the properties (some of them are optional) of our new
email message are set properly, the only thing that's left to do is
send the message.

3. Send the Message

If you have experience sending email from classic ASP script


with CDONTS you might think that the MailMessage class has
method Send or something similar. Well, the way ASP.NET sends
email messages is a little different from ASP.

We need to use the static method Send of the SmtpMail class,


passing in our objMail instance. You might be asking "what's a
'static method'?" A static method is one that's not associated
with an instance of a type. An example of an instance of a type
is our objMail object. It is illegal to reference a static member of
a class through an instance. The proper way to do it is:

SmtpMail.Send(objMail)

If you want to specify an SMTP server that's different than the


default, you'll need to set the SmtpServer property of the SmtpMail
class:

SmtpMail.SmtpServer = "smtp.your-server.com"

Summary
In summary, to send an email from your ASP.NET page, you
need to:

import the System.Web.Mail namespace in your ASP.NET page


create an instance of the MailMessage class
set all the properties of the MailMessage instance
send the message with SmtpMail.Send method
And finally, to get you started, here's the complete, functional
code for an ASP.NET page that sends the user's feedback via
email to the Webmaster:

<%@ Page Language="VB" EnableSessionState="False"


EnableViewState="False" Trace="False" Debug="False"%>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat=server>

Sub Page_Load(Sender as Object, E as EventArgs)


If Page.IsPostBack Then
lblResponse.Text = "Your email has been sent."
End If
End Sub

Sub btn_Click(sender as Object, e as System.EventArgs)


If Request.Form("Email") <> "" Then
Dim objMail As New MailMessage()
objMail.From = "[email protected]"
objMail.To = Request.Form("Email")
objMail.Subject = Request.Form("Subject")
objMail.Body = Request.Form("Message")
objMail.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = " smtp.your-server.com"
SmtpMail.Send(objMail)
Else
lblResponse.Text = "Please enter an email address."
End If
End Sub

</script>
<html>
<head>
<style>
.main {font-family:Verdana; font-size:12px;}
.title {font-family:Verdana; font-size:18px; font-weight:bold;}
</style>
</head>
<body>
<span class="title" align="center">Send email from
an ASP.NET page</span>

<br><br><asp:Label class="main" id="lblResponse"


runat="server"/>

<form method="POST" name="MainForm" runat="server">


<table>
<tr>
<td class="main" align="right">Email:</td>
<td class="main"><input type="text"
class="main" name="Email" value=""></td>
</tr>
<tr>
<td class="main" align="right">
Subject:</td>
<td class="main"><input type="text"
class="main" name="Subject" value=""></td>
</tr>
<tr>
<td class="main" align="right"
valign="top">Message:</td>
<td class="main"><textarea name="Message"
cols="50" rows="8"></textarea></td>
</tr>

<tr>
<td class="main">&nbsp;</td>
<td class="main"><input type="Submit"
id="btnSubmit" OnServerClick="btn_Click" value="Send"
runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>

If you liked this

You might also like