Hi rani,
Using the below article i have created the example.
Now please take its reference and correct your code.
HTML
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller("MyController", function ($scope, $http, $window) {
$scope.SendEmail = function () {
$http.post("Default.aspx/SendEmail", JSON.stringify({ email: $scope.Email }), { headers: { 'Content-Type': 'application/json'} })
.then(function (response) {
$scope.Message = response.data.d;
}, function error(response) {
$scope.Message = response.responseText;
});
}
});
</script>
<div ng-app="MyApp" ng-controller="MyController">
<input type="text" name="email" ng-model="Email" />
<input type="button" name="name" value="Send" ng-click="SendEmail()" />
<br /><span style="color: Red;" ng-bind="Message"></span>
</div>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Web.Services;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Net
Imports System.Net.Mail
Imports System.Web.Services
Code
C#
[WebMethod]
public static string SendEmail(string email)
{
string username = string.Empty;
string password = string.Empty;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Username, [Password] FROM Users WHERE Email = @Email"))
{
cmd.Parameters.AddWithValue("@Email", email);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.Read())
{
username = sdr["Username"].ToString();
password = sdr["Password"].ToString();
}
}
con.Close();
}
}
if (!string.IsNullOrEmpty(password))
{
MailMessage mm = new MailMessage("sender@gmail.com", email);
mm.Subject = "Password Recovery";
mm.Body = string.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", username, password);
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<Password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
return "Password has been sent to your email address.";
}
else
{
return "This email address does not match our records.";
}
}
VB.Net
<WebMethod()>
Public Shared Function SendEmail(ByVal email As String) As String
Dim username As String = String.Empty
Dim password As String = String.Empty
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Username, [Password] FROM Users WHERE Email = @Email")
cmd.Parameters.AddWithValue("@Email", email)
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
username = sdr("Username").ToString()
password = sdr("Password").ToString()
End If
End Using
con.Close()
End Using
End Using
If Not String.IsNullOrEmpty(password) Then
Dim mm As MailMessage = New MailMessage("sender@gmail.com", email)
mm.Subject = "Password Recovery"
mm.Body = String.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", username, password)
mm.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "<Password>"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
Return "Password has been sent to your email address."
Else
Return "This email address does not match our records."
End If
End Function