亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Sending E-Mails using ASP.NET

系統(tǒng) 2365 0

by Faisal Khan .

Overview

In this tutorial we will learn how to send e-mails of both text and HTML formats using classes of System.Web.Mail namespace.

Before delving in to the source code, I'll briefly explain the MailMessage and SmtpMail classes which we'll be using to send e-mails.

System.Web.Mail.MailMessage

An object of this class represents a single e-mail message. Properties of this class are listed below :


Properties of MailMessage

To use this class, we create a new object using the empty constructor, set some of it's properties and then send the message using static method of SmtpMail class's Send() method.

    	MailMessage mail = new MailMessage();

		mail.To = "receivers@email.address";
		mail.From = "senders@email.address";
		mail.BodyFormat = MailFormat.Text;
		mail.Subject = "Subject of this Email";
		mail.Body = "Body of this message.";

	SmtpMail.Send(mail);
  

All the code required to create and send a MailMessage object has been shown above. If you've understood this code then you don't need to go any further. Just change the values to suite your needs. In the rest of this article we will create a sendMail.aspx page which will make use of the above code.

High-end commercial email component :

i. AspEmail : Most feature-rich mailing component. Supports message queueing, embedded images, secure mail, authentication. Read more .

System.Web.Mail.SmtpMail

This class is used to *send* e-mails ( MailMessages ) using SMTP protocol. All we are interested as far as this tutorial is concerned are the two static methods ( Send() ) it provides. You just use the static method, provide a valid MailMessage object as the argument, and send it!.


Properties And Methods of SmtpMail

sendMail.aspx

It is time now that we create a simple ASP.NET page which provides a form to you, you enter the values and hit the "send" button to send the message. A confirmation message is then displayed.

Create a new .aspx page and save it as sendMail.aspx in a folder accessible by your server. Now copy and paste the following code in it :

    <%@ Page Language="C#" %>
<%@ Import NameSpace="System.Web.Mail" %>

<script runat="server">
	protected void Page_Load(Object Sender, EventArgs e) {
		if(!Page.IsPostBack)
			Message.Text = "Sending E-Mail using ASP.NET";
	}

	protected void Send_Email(Object Sender, EventArgs e) {
		MailMessage mail = new MailMessage();

			mail.To = Request.Form["to"];
			mail.From = Request.Form["from"];

			if(Request.Form["format"].Equals("text"))
				mail.BodyFormat = MailFormat.Text;
			else
				mail.BodyFormat = MailFormat.Html;

			mail.Subject = Request.Form["subject"];
			mail.Body = Request.Form["body"];

		SmtpMail.Send(mail);
		Response.Flush();

		Message.Text = "Message Sent...<br><br>" + 
				"<a href=\"sendMail.aspx\">Go Back</a>";
	}
</script>
<html>
<head>
	<title>Sending E-Mails using ASP.NET ( Part I )</title>
	<style>
	a { font-size:8pt; font-family:Tahoma; font-weight:normal;
		color:maroon; }
	</style>
	<link rel="stylesheet" href="css/demostyle.css">
</head>
<body bgcolor="white">

<p align="center" class="title">
	<asp:label id="Message" runat="server" />
</p>

<% if(!Page.IsPostBack) { %>
	<p align="center">All fields are required.</p>

<form method="post" runat="server">	
<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
	<tr>
		<td width="20%" align="right">From : </td>
		<td><input type="text" name="from"></td>
	</tr>
	<tr>
		<td align="right">To : </td>
		<td><input type="text" name="to"></td>
	</tr>
	<tr>
		<td align="right">MailFormat : </td>
		<td>
			<select name="format">
				<option value="text">Text</option>
				<option value="html">HTML</option>
			</select>
		</td>
	</tr>
	<tr>
		<td align="right">Subject : </td>
		<td><input type="text" name="subject" style="width:400;"></td>
	</tr>
	<tr>
		<td valign="top" align="right">Body : </td>
		<td>
		<textarea cols="5" rows="10" name="body" style="width:400;">
		</textarea></td>
	</tr>
	<tr>
		<td> </td>
		<td>
		<input type="submit" OnServerClick="Send_Email" runat="server" 
		class="submit" value=" " /> Send Email!
		</td>
	</tr>
</table>
</form>
<% } %>

</body>
</html>
  
Explanation

The first line tells that we will be using C# to code the ASP.NET page. Second line is an Import directive which tells the the C# compiler that we are making use of classes from System.Web.Mail namespace.

    <%@ Page Language="C#" %>
<%@ Import NameSpace="System.Web.Mail" %>
  
Explanation of sendMail.aspx code

Then we enter the script block. Notice that this script block has a "runat" attribute with a value of "server". This means that the code inside this block is meant to run on the server-side. In this code block we have two methods; Page_Load() and Send_Email().

    <script runat="server">
  

Page_Load() is actually an event which is raised when the ASP.NET page is loaded. This method is present in all ASP.NET pages, to make use of this event, all you have to do is to provide some implementation of this method ( i.e. put some code in this method ). Here in this method we are checking for the IsPostBack property of Page object. This property like it's name suggests gives a "true" value when the page has been posted back i.e. the user has pressed the submit button on the Form to post the page back.

So this Page.IsPostBack property is very useful. It allows us to put both the Form and Form handler code ( when the Form is posted back ) on the same page. What we do here is to check this property, and if user has not posted the page back then set the value of Message control to the provided one. What is a Message control? we'll see later.

    protected void Page_Load(Object Sender, EventArgs e) {
	if(!Page.IsPostBack)
		Message.Text = "Sending E-Mail using ASP.NET ( Part I )";
}
  

The second method; Send_Email() is our own event method which is called when user presses the submit button on the Form. In this method we use the same code we learned on the first page to create a MailMessage object and then send it using SmtpMail class's Send() method.

Once done, we set the value of Message control to a confirmation message and a link back to the Form.

    	protected void Send_Email(Object Sender, EventArgs e) {
		MailMessage mail = new MailMessage();

			mail.To = Request.Form["to"];
			mail.From = Request.Form["from"];

			if(Request.Form["format"].Equals("text"))
				mail.BodyFormat = MailFormat.Text;
			else
				mail.BodyFormat = MailFormat.Html;

			mail.Subject = Request.Form["subject"];
			mail.Body = Request.Form["body"];

		SmtpMail.Send(mail);
		Response.Flush();

		Message.Text = "Message Sent...<br><br>" + 
				"<a href=\"sendMail.aspx\">Go Back</a>";
	}
  

Then we close the script code block.

    </script>
  

We have been talking of Message control but where is it and what is a control? Well, a control is a server-side component which does some thing useful. The Message control is of type ASP:Label and the only reason we are using it is that we can set it's value using logic on the server-side. We will learn more about controls in some other article later.

We declare ASP:Label control on the top of the page. Notice the value of "id" attribute? this is why we have been using Message handle to set it's value.

    <p align="center" class="title">
	<asp:label id="Message" runat="server" />
</p>
  

Next we again look for Page.IsPostBack property and if the page has not been posted back then show the e-mail Form to the user.

    <% if(!Page.IsPostBack) { %>
	... all Form code here
<% } %>
  

One last thing interesting is that we said that our Send_Email() method is an event method raised on the server-side when the user presses the submit button, how? we did that by putting yet another type of server-side control. In the submit button code we put two new attributes, "runat=server" to make it a control and "OnServerClick". This "OnServerClick" event is the key and it's value is our Send_Email() method. So our Send_Email() method will be called every time user presses this submit button.

    	<input type="submit" OnServerClick="Send_Email" runat="server" 
		class="submit" value="? /> Send Email!
  

Don't worry if you cannot swallow this server-side control and event model. We will study it in detail some other time. For now let's move forward to the next page where we run this sendMail.aspx page and see the results.

Running the sendMail.aspx page
Ok, it is time to run the sendMail.aspx page. You can use a URL like http://localhost/net/sendMail.aspx on your system, provided where you have placed the sendMail.aspx page. On my system the sendMail.aspx page looked like following :


sendMail.aspx

Now enter your valid email address in the "From" and "To" fields, any text in the subject and body fields and hit the "Send Email!" button on the sendMail.aspx page on your system. If all goes well you should see a confirmation message like I did :


sendMail.aspx - Confirmation Message

If you don't get a confirmation message and an error message then most probably SMTP service is not enabled on your system. You can then provide your own SMTP server ( provided by your ISP ) and change the SmtpMail.Send(mail) line in the Send_Email() method to the one below :

    	SmtpMail.SmtpServer = "smtp.yourisp.com";
	SmtpMail.Send(mail);
  

And this time you should be able to send your e-mail. Well this is it for this tutorial, one thing to keep in mind is to provide valid email address in From and To fields and non-empty values in subject and body fields. If you don't do that you'll get a nasty ASP.NET error message on your face. This is why I have called this tutorial Part I, because there is no validation for the values entered by the user. Hopefully in Part II we'll learn how to handle a case when user didn't enter a value and pressed the "Send Email!" button or he provided an invalid destination e-mail address.

Sending E-Mails using ASP.NET


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美亚洲综合在线 | 亚洲精品乱码蜜桃久久久 | 亚洲不卡视频在线观看 | 国产精品99久久久久久人 | 中文字幕久精品免费视频 | 国产在线视频自拍 | 亚洲国产精品综合久久网络 | 精品福利国产 | 99久久免费国产精品特黄 | 免费高清在线影片一区 | 亚洲一级视频在线观看 | a网站在线观看 | 免费国产之a视频 | 天天干天天舔天天射 | 亚洲国产日韩欧美一区二区三区 | 在线亚洲成人 | 国产欧美日韩高清专区ho | 四虎影永久在线观看精品 | 伊人久久大香线焦综合四虎 | 美女久久久 | 91精品国产综合久久青草 | 国产一国产一级毛片视频 | 国产精品久久久久激情影院 | 国产成人精品午夜二三区 | 国产91精品一区二区麻豆网站 | 欧美综合图 | 亚洲国产精品一区二区久 | 亚洲a区视频 | 久久精品国产91久久麻豆自制 | s8国产成人精品视频 | 中文字幕在线看视频一区二区三区 | 国产福利在线观看永久视频 | 在线观看免费亚洲 | 国产亚洲欧美日韩v在线 | 国产精品二区高清在线 | 免费视频不卡一区二区三区 | 美女很黄很黄免费 | 99视频全部看免费观 | 欧美一级特黄乱妇高清视频 | 四虎影院最新网址 | 国产色婷婷精品综合在线手机播放 |