Hi akhter,
I have made reference to below article to make below sample.
Refer below sample.
Web.Config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="sender@gmail.com">
<network
host="smtp.gmail.com"
port="587"
enableSsl="true"
userName="sender@gmail.com"
password="SenderGmailPassword"
defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
HTML
<input id="btnGetTime" type="button" value="Send Email" onclick="SendEmail()" />
<script type="text/javascript">
function SendEmail() {
var toEmail = "testEmail@aspforums.com";
var subject = "testSubject";
var body = "testBody";
var request;
if (window.XMLHttpRequest) {
//New browsers.
request = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
//Old IE Browsers.
request = new ActiveXObject("Microsoft.XMLHTTP");
}
if (request != null) {
var url = "Default.aspx/SendEmail";
request.open("POST", url, false);
var params = "{ toEmail: '" + toEmail + "', subject: '" + subject + "', body: '" + body + "' }";
request.setRequestHeader("Content-Type", "application/json");
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
alert(JSON.parse(request.responseText).d);
}
};
request.send(params);
}
}
</script>
Namespaces
C#
using System.Net;
using System.Net.Mail;
using System.Web.Services;
using System.Configuration;
using System.Net.Configuration;
VB.Net
Imports System.Net
Imports System.Net.Mail
Imports System.Web.Services
Imports System.Configuration
Imports System.Net.Configuration
Code
C#
[WebMethod]
public static string SendEmail(string toEmail, string subject, string body)
{
SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
using (MailMessage mm = new MailMessage(smtpSection.From, toEmail))
{
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = smtpSection.Network.Host;
smtp.EnableSsl = smtpSection.Network.EnableSsl;
NetworkCredential networkCred = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials;
smtp.Credentials = networkCred;
smtp.Port = smtpSection.Network.Port;
smtp.Send(mm);
}
return "Email sent.";
}
VB.Net
<WebMethod>
Public Shared Function SendEmail(ByVal toEmail As String, ByVal subject As String, ByVal body As String) As String
Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
Using mm As MailMessage = New MailMessage(smtpSection.From, toEmail)
mm.Subject = subject
mm.Body = body
mm.IsBodyHtml = False
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = smtpSection.Network.Host
smtp.EnableSsl = smtpSection.Network.EnableSsl
Dim networkCred As NetworkCredential = New NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
smtp.Credentials = networkCred
smtp.Port = smtpSection.Network.Port
smtp.Send(mm)
End Using
Return "Email sent."
End Function