Thursday, 19 July 2012

Send E-Mail Code in ASP.Net

Send E-Mail Code in ASP.Net

Hello Friends,
Please follow below steps to send mail in

1) Create design page as below.

2. To prepare above UI follow this html code .
html xmlns="" >head runat="server">
<title>Untitled Page</title>head>body><form id="form1" runat="server"><div>
<asp:TextBox ID="txtname" runat="server" style="z-index: 100; left: 197px; position: absolute; top: 96px"></asp:TextBox><asp:TextBox ID="txtemailid" runat="server" style="z-index: 101; left: 197px; position: absolute; top: 130px"></asp:TextBox><asp:TextBox ID="txtmessage" runat="server" style="z-index: 102; left: 197px; position: absolute; top: 162px" TextMode="MultiLine"></asp:TextBox>&nbsp;<asp:Button ID="Btnsendmessage" runat="server" Text="Send" Width="70px" style="z-index: 103; left: 197px; position: absolute; top: 210px" />&nbsp;<asp:Label ID="lblmsg" runat="server" style="z-index: 104; left: 197px; position: absolute; top: 241px" ></asp:Label>
<asp:Label ID="Label1" runat="server" Style="z-index: 105; left: 133px; position: absolute;top: 96px" Text="Name"></asp:Label>
<asp:Label ID="Label2" runat="server" Style="z-index: 106; left: 133px; position: absolute;top: 130px" Text="Email ID"></asp:Label>
<asp:Label ID="Label3" runat="server" Style="z-index: 108; left: 133px; position: absolute;top: 162px" Text="Message"></asp:Label>

3. On Send Button's Click event put the below Code.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Web.Mail 

Protected Sub Btnsendmessage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btnsendmessage.ClickTryIf txtname.Text = "" Thenlblmsg.Text = "Mandatory field Name "txtname.Focus()
Exit SubEnd IfIf txtemailid.Text = "" Thenlblmsg.Text = "Mandatory field Email address"txtemailid.Focus()
Exit SubEnd IfIf txtmessage.Text = "" Thenlblmsg.Text = "Mandatory field Message address"txtmessage.Focus()
Exit SubEnd Ifsendmail()

Catch ex As ExceptionEnd Try
End Sub sendmail()If EMail() Thenlblmsg.Text = "Mail Send Succsessfully"txtname.Text = ""txtemailid.Text = ""txtmessage.Text = ""txtname.Focus()
End IfEnd Sub

Function EMail()Dim Emailid As String = txtemailid.TextDim Name As String = txtName.TextDim Receiver As String = txtemailid.Text 'Dim cc As String = ""Dim bcc As String = ""Dim subject As String = "Rajesh Somvanshi: Thanks For visit us."
Dim body As String = txtmessage.TextIf sendMail(Receiver, cc, bcc, subject, body) ThenReturn TrueElseReturn FalseEnd IfEnd FunctionPublic Function sendMail(ByVal Receiver As String, ByVal cc As String, ByVal bcc As String, ByVal subject As String, ByVal body As String) As IntegerTry

.From =
Dim mesg As New MailMessageWith mesg"" ' Email Address.To = Receiver
.Cc =
"".Bcc = "".Subject = "Rajesh Somvanshi: Thank For Visit us.".Priority = MailPriority.High
.Body = body
Dim creduser As String = mesg.From.ToString() ' Enter Your Email IDDim credpass As String = "Test8888888" 'Enter Your Password Here mesg.Fields.Add("", "1") 'basic authenticationmesg.Fields.Add("", creduser) 'set your username heremesg.Fields.Add("", credpass) 'set your password hereSmtpMail.SmtpServer = "" ' SMTP SERVER NAME.SmtpMail.Send(mesg)
End WithReturn True
Catch ex As ExceptionReturn FalseEnd TryEnd Function 
Note : - .Please Chanage the Email address with password.

No comments:

Post a Comment