Net-informations.com
SiteMap  | About    

ASP.NET Email application

SMTP stands for Simple Mail Transfer Protocol . The System.Net.Mail namespace contains classes used to send e-mail to a SMTP server for delivery. The default port using SMTP is 25 , but it may vary different Mail Servers

smtp-email

The two classes here we are using for sending email are MailMessage Class and SmtpClient Class. The MailMessage class represents the content of a mail message. The SmtpClient class transmits email to the SMTP host that you designate for mail delivery.

The following ASP.NET source code shows how to send an email from a Gmail address using SMTP server. The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 and also using NetworkCredential for password based authentication.

vb.net
  SmtpServer.Port = 587
  SmtpServer.Credentials = New System.Net.NetworkCredential("username", "password")

SmtpServer.EnableSsl = True

C#
  SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
  SmtpServer.Port = 587;

SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");

The above code you have to replace username and password with you gmail user name and password. Also you have to provide the from address as your gmail address.

vb.net

  mail.From = New MailAddress("your-email-address@gmail.com")

C#

  mail.From = new MailAddress("you-email-address@gmail.com");

The following ASP.NET program using a gmail address as from address and server as gmail smtp server for sending a SMTP email message.

Default.aspx

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Label ID="Label1" runat="server" Text="To : "></asp:Label>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<asp:TextBox ID="to_txt" runat="server" Width="200px"></asp:TextBox>
		<br /><br />
		<asp:Label ID="Label2" runat="server" Text="Subject :"></asp:Label>
		&nbsp;&nbsp;&nbsp;
		<asp:TextBox ID="subject_txt" runat="server" Width="200px"></asp:TextBox>
		<br />
		<br />
		<asp:Label ID="Label3" runat="server" Text="Message :"></asp:Label>
		<br />
		<asp:TextBox ID="message_txt" runat="server" Height="117px"
		TextMode="MultiLine" Width="279px"></asp:TextBox>
		<br />
		<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
		Text="Send Mail" />
		<br />
		<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
	</div>
	</form>
</body>
</html>


Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.cs
default.aspx.vb






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.