This Ajax email program does not send an email. It returns: "Sent" but does not actually send the email to the web.
Here is my code:
[System.Web.Services.WebMethod]
public static string EmailData(string mailto, string mailfrom, string mailsubject, string mailmessage)
{
string result = "";
try
{
MailMessage msg = new MailMessage();
MailAddress fromAdd = new MailAddress(mailfrom);
msg.To.Add(mailto);
msg.Subject = mailsubject;
msg.From = fromAdd;
msg.Priority = MailPriority.Normal;
msg.IsBodyHtml = false;
msg.Body = mailmessage;
SmtpClient smtpClient = new SmtpClient("smtp.user.com", 25);
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("user", "pass");
smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(msg);
smtpClient.Dispose();
result = "Sent";
}
catch (Exception ex){
Console.WriteLine(ex.ToString());
result = "Did not send";
}
return result;
}
Default.aspx
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#<%= Button1.ClientID %>").click(function () {
var To = $("#<%= TextBox1.ClientID %>").val();
var From = $("#<%= TextBox2.ClientID %>").val();
var Subject = $("#<%= TextBox3.ClientID %>").val();
var Message = $("#<%= TextBox4.ClientID %>").val();
var data = { mailto: To, mailfrom: From, mailsubject: Subject, mailmessage: Message };
var json1 = JSON.stringify(data);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/EmailData",
data: json1,
dataType: "json",
success: function (result) {
$("#<%= TextBox5.ClientID %>").val(result.d);
},
error: function (req, status, err) {
$("#<%= Label1.ClientID %>").text('Something went wrong', status, err);
}
}); return false;
});
});
</script>