November 30, 2022

How to send E-Mail using Background Process in Asp.net MVC

How to send E-Mail using Background Process in Asp.net MVC

Here I will explain how to send Emails using the Background process using Asp.net Core MVC.

Create Core Web APP(Model-View-Controller)

First, create a new project from Visual Studio and name it as CoreMailSend and input the other details.

Create new MVC project
How to create Zoom Meeting with Zoom API using Asp.net Core Razor Page

Update your bootstrap to the latest by visiting the Bootstrap 5 site and go to Views -> Shared -> Layout.cshtml and change the bootstrap CSS and js file.

Bootstrap link in Layout.cshtml

Create a Email Send Form on Index.cshtml

Open the Views -> Home -> index.cshtml and edit the below code

<div class="row g-5">
    <h4 class="mb-3">Send Email</h4>
    <form method="post" asp-controller="Home" asp-action="Index">
        <div class="row g-3">
            <div class="col-12">
                <input class="form-control" type="text" name="to" placeholder="Send to" required />
            </div>
            <div class="col-12">
                <input class="form-control" type="text" name="subject" placeholder="Subject" required />
            </div>
            <div class="col-12">
                <textarea class="form-control" type="text" name="body" placeholder="Message" required></textarea>
            </div>
            <div class="col-12">
                <input type="submit" value="Submit"/>
            </div>
            <div class="form-group">
                <div class="text-success">
                    @if(TempData["alert"] != null)
                    {
                        @TempData["alert"];
                    }
                </div>
            </div>
        </div>
    </form>
</div>

Create Sendmail class to add Mail Configurations

Right click on the solution and Add -> Class to create a new class and name it as Sendmail.cs and add the following code

public static void Email(string to, string subject, string body)
        {
            MailMessage mm = new();
            mm.To.Add(new MailAddress(to));
            mm.From = new MailAddress("Your Email Address");
            mm.Subject = subject;
            mm.Body = body;
            mm.IsBodyHtml = true;
            mm.Priority = MailPriority.High;
            SmtpClient smtpClient = new();
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = new NetworkCredential("Your Email Address", "Password");
            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtpClient.Send(mm);
        }

Add Post Method in HomeController

Now go to Controllers -> HomeController and add the below code

[HttpPost]
        public IActionResult Index(string to,string subject,string body)
        {
            try
            {
                Thread email = new(delegate ()
                {
                    Sendmail.Email(to, subject, body);
                });
                email.IsBackground = true;
                email.Start();
                TempData["alert"] = "Email Successfully Sent";
            }
            catch (Exception)
            {
                TempData["alert"] = "Problem Sending mail. Please check the configuration";
            }
            return Redirect("Home/Index");
        }

Now run the Application

Run the application to see the design

Application Running

Output of the application

About Author

ussain

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.