User menu

Home » Blog 63 » Send email using jquery ajax request and asp backend

Send email using jquery ajax request and asp backend

Here is the code that read in post submission, validate required parameters, and sends an email using asp .net language.

1) Store following asp code into sendEmail.asp

<%    
    'init variables
    dim objMail 
    Set objMail = Server.CreateObject("CDO.Message") 
    dim smtpServer, yourEmail, yourPassword
    dim emailTo, emailFrom, emailData, emailSubject, token
            
    'grab form elements
    emailTo = Request("to")
    emailSubject = Request("subject")
    emailData = Request("data")
    token = Request("token")
    
    'gmail account configuration
       emailFrom = "[email protected]"
       smtpServer = "smtp.gmail.com"
       yourPassword = "PaSsWoRd"   'replace with a valid password for account set in yourEmail
    
    'validate input
    If(emailTo <> "") And (emailFrom <> "") And (emailSubject <> "") And (emailData <> "") And (token = "someToken") And (Request.ServerVariables("REQUEST_METHOD") = "POST") Then
        'configure Mail Object
        objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer
        objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 
        objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
        objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = emailFrom
        objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = yourPassword
        objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 
        objMail.Configuration.Fields.Update 
         
        objMail.Subject= emailSubject
        objMail.From = emailFrom
        objMail.To = emailTo
        objMail.Cc = "[email protected]"
        objMail.htmlBody = emailData
        'objMail.TextBody = emailData
        objMail.Send
        Response.Write("OK")
    Else
        Response.Write("Failed ")
    End If
%>

 

2) Copy paste following code in your custom js file. It has sendEmail function with to, subject, and message parameter. This will call sendEmail.asp back-end and sends out an email. If it fails somehow, you will get an error message, so you can debug.

function sendEmail(to,subject,message) {
    var urlPath = sendEmail.asp
    var token="someToken";
    var resultMsg = "";
    $.ajax({
        async: false,
        url: urlPath,
        type: "POST",
        data: { token: token, to: to, subject: subject, data: message },
        success: function (data) {
            //check if succeed
            if (data == "OK") {
                resultMsg = "OK";
            }
            else{
                resultMsg = "Technical difficulty in sending an email. [ErrorCode: 101]";
            }
        },
        error: function (xhr, err) {
            resultMsg = "AJAX Erro. readyState: " + xhr.readyState + "status: " + xhr.status;
            resultMsg += "\nurl: " + urlPath + "\n";
            resultMsg += xhr.responseText;
        }
    }); //end AJAX
    return resultMsg;
}