Email Send through Asp.Net C#

.aspx page:

<asp:Content ID=”Content1″ ContentPlaceHolderID=”HeadContent” runat=”Server”>
<title>E-Mail Send through Asp.Net C#</title>
</asp:Content>
<asp:Content ID=”Content2″ ContentPlaceHolderID=”FeaturedContent” runat=”Server”>
<style>
.SingleNewLine {
clear: both;
width: 100%;
height: 10px;
}
</style>
<script type=”text/javascript”>
var arr = [];
$(document).ready(function () {
$(‘[id$=btnSend]’).click(function () {
$.ajax({
type: “POST”,
url: “SendMail_WithOutAttachment.aspx/SendEMail”,
data: “{‘from’:'” + $(‘[id$=txtFrom]’).val() + “‘,’password’:'” + $(‘[id$=txtPassword]’).val() + “‘,’to’:'” + $(‘[id$=txtTo]’).val() + “‘,’subject’:'” + $(‘[id$=txtSubject]’).val() + “‘,’message’:'” + $(‘[id$=txtMessage]’).val() + “‘}”,
contentType: “application/json; charset=utf-8”,
dataType: “json”,
success: function (msg) {
cleareAll();
alert(‘Success-‘ + msg);
},
error: function (msg) {
cleareAll();
alert(‘Error-‘ + msg);
}
});
});
});
function GetFileName() {
arr.push($(‘[id$=FileUploader1]’));
}

function cleareAll() {
$(‘[id$=txtFrom]’).val(”);
$(‘[id$=txtPassword]’).val(”);
$(‘[id$=txtTo]’).val(”);
$(‘[id$=txtSubject]’).val(”);
$(‘[id$=txtMessage]’).val(”);
}
</script>
</asp:Content>
<asp:Content ID=”Content3″ ContentPlaceHolderID=”MainContent” runat=”Server”>
<h1>E-Mail Send through Asp.Net C#</h1>
<div>
</div>
<table style=”width: 100%”>
<tr>
<td>From</td>
<td>
<input type=”text” id=”txtFrom” runat=”server” maxlength=”100″ />
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type=”password” id=”txtPassword” runat=”server” maxlength=”20″ />
</td>
</tr>
<tr>
<td>To</td>
<td>
<input type=”text” id=”txtTo” runat=”server” maxlength=”100″ />
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type=”text” id=”txtSubject” runat=”server” maxlength=”100″ />
</tr>
<tr>
<td>Message</td>
<td>
<input type=”text” id=”txtMessage” runat=”server” maxlength=”100″ />
</tr>

<tr>
&nbsp;
<td>
<input type=”button” value=”Send” id=”btnSend” />
</td>
</tr>
</table>
</asp:Content>

Code behind page:

[System.Web.Services.WebMethod]
public static int SendEMail(string from, string password, string to, string subject, string message)
{
SmtpClient sClient = new SmtpClient(“smtp.gmail.com”, 587);
sClient.EnableSsl = true;
sClient.Credentials = new System.Net.NetworkCredential(from, password);
MailMessage mMessage = new MailMessage(from, to, subject, message);
mMessage.IsBodyHtml = true;
try
{
sClient.Send(mMessage);
return 1;
}
catch (Exception ex)
{
return 0;
}
}

Advertisements

One thought on “Email Send through Asp.Net C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s