简体   繁体   中英

How to send an email -ajax asp.net mvc?

I have tried to create this "send an email" for my app using ajax (it's a requirement for my project), but it's only returning json, not even the message "your message was sent". I've looked through many sites and question, but they were not helpful.

index.cshtml

@using (Ajax.BeginForm("SendEmailAjax", new AjaxOptions()
{
    HttpMethod = "Post",
    OnSuccess = "DisplayConfirmation"
}))
 {
        <label for="from">From: </label>
        <input type="text" name="FromEmail" id="from" />
        <br />
        <label for="subject">Subject: </label>
        <input type="text" name="Subject" id="subject" />
        <br />
        <label for="message">Message: </label>
        <input type="text" name="MessageText" id="message" />
        <input type="submit" value="Send" />

 }

<div id="formresult"></div>
<script src="~/Scripts/jquery-2.1.1.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

<script>
    function DisplayConfirmation(result) {
        var confirmation = "Message was sent!";
        $('#formresult').html(confirmation);
    }
</script>

emailconfirmation.cshtml, email.cshtml

@model Project.Models.MessageDetails

@{
    ViewBag.Title = "EmailConfirmation";
    Layout = "~/Views/_Layout.cshtml";
}
<h2>Congratulatons!</h2>

<div>
    You sent a message:
    <p>
        @Model.MessageText
    </p>
</div>

emailcontroller.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Web.Mvc;
using Project.Models;

namespace Project.Controllers
{
    public class EmailController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult SendEmail(MessageDetails message)
        {
            return View("EmailConfirmation", message);
        }
        [HttpPost]
        public ActionResult SendEmailAjax(MessageDetails message)
        {
            return Json(message);
        }
    }
}

Below is standard code to send email via SMTP. Modify below code according to your change and keep this code in SendEmailAjax method controller. if you are using any organization SMPT server then make sure that IT team allowed you to send email via SMTP. for testing purpose you can use gmail SMTP with your gmail account credentials.

Change or modify below code according to you change:

        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        System.Net.Mail.SmtpClient smtp = new SmtpClient("smtp.xxxxx.com");
        smtp.Timeout = 300000;
        mail.From = new MailAddress("sender@xxxxx.com");
        mail.To.Add("receiver@yyyy.com");
        mail.CC.Add("receivercc@yyyy.com");
        mail.Subject = "My Subject";
        mail.Body = "This is a testing mail";
        mail.IsBodyHtml = true;
        mail.SubjectEncoding = Encoding.UTF8;
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        smtp.Send(mail);

For gmail SMTP configuration, you need to tweak smtp configuration like below:

      SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
      smtp.Credentials = new NetworkCredential("sender@gmail.com", "password here");  
      smtp.EnableSsl = enableSSL;  
      smtp.Send(mail);  

refer this article for more details:https://www.c-sharpcorner.com/blogs/send-email-using-gmail-smtp

let me know if you need more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM