Click here to Skip to main content
16,014,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
[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
HTML
<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>
Posted
Comments
Have you debugged?
ZurdoDev 10-Dec-14 13:07pm    
If there is no error then the issue is likely with SMTP.
Member 10711013 10-Dec-14 15:21pm    
using the brickpoint , and type err0r here.

1 solution

Have you added web reference into your asp.net application or you want to call hosted web service from ajax?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900