Sending Mail with Login:
.ASPX CODE:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Send
mail with Uname and Password .aspx.cs" Inherits="Mail_Send_mail_with_Uname_and_Password_" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>
Simple coding forSending Mail!
</h2>
<table class="style1">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Mail ID"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtMailID" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPword" runat="server" Width="250px" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="To Address"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtToAddr" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Subject"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtSub" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Message"></asp:Label>
<br />
</td>
<td>
<asp:TextBox ID="txtMsg" runat="server" Height="73px" TextMode="MultiLine"
Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="BtnSend" runat="server" onclick="BtnSend_Click" Text="Send" />
</td>
</tr>
</table>
</div>
<asp:Label ID="Label6" runat="server"
Text="Label"></asp:Label>
</form>
</body>
</html>
.CS CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Web.SessionState;
public partial class Mail_Send_mail_with_Uname_and_Password_
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
BtnSend_Click(object sender, EventArgs e)
{
MailMessage msg;
NetworkCredential login;
SmtpClient cli;
//string str;
login
= new NetworkCredential(txtMailID.Text,
txtPword.Text);
msg =
new MailMessage();
msg.From = new MailAddress(txtMailID.Text);
msg.To.Add(new MailAddress(txtToAddr.Text));
msg.Subject = txtSub.Text;
msg.Body = txtMsg.Text;
msg.IsBodyHtml = true;
cli =
new SmtpClient("smtp.gmail.com");
//for Gmail account use smtp.gmail.com
//for yahoo account use smtp.mail.yahoo.com
cli.EnableSsl = true;
cli.UseDefaultCredentials = false;
cli.Credentials = login;
cli.Send(msg);
Label6.Text = "Mail sent
Successfully"; }}
No comments:
Post a Comment