Send E-Mail Code in ASP.Net
Please follow below steps to send mail in ASP.net...
1) Create design page as below.
2. To prepare above UI follow this html code .
html xmlns="http://www.w3.org/1999/xhtml" >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> <asp:Button ID="Btnsendmessage" runat="server" Text="Send" Width="70px" style="z-index: 103; left: 197px; position: absolute; top: 210px" /> <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>
</div>
</
</</form>body>html>
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
Sub
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"Test@yahoo.com" ' Email Address.To = Receiver
.Cc = "Test@yahoo.com".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("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authenticationmesg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", creduser) 'set your username heremesg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", credpass) 'set your password hereSmtpMail.SmtpServer = "smtp.mail.yahoo.com" ' SMTP SERVER NAME.SmtpMail.Send(mesg)
End WithReturn True
Catch ex As ExceptionReturn FalseEnd TryEnd Function
Note : - .Please Chanage the Email address with password.