.NET Core

How to send email using SMTP in ASP.NET Core

In this article, we will learn how to send email using SMTP in ASP.NET Core.

Let’s get started with creating ASP.Net MVC Core Project.

1. Open Visual Studio and click on Create New Project.

2. From the Create a New Project window, select ASP.Net Core Web Application option.

3. Now you need to set a suitable Name for your project and also you can set its Location where you want to create the Project.

4. From the new ASP.Net Core Web Application Dialog, select Empty and click Create.

Now create model class named EmailModel as below

public class EmailModel
{
    public string To { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public IFormFile Attachment { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}

MailMessage Class Properties

Following are the required properties of the MailMessage class.
From – Sender’s email address.
To – Recipient(s) Email Address.
CC – Carbon Copies (if any).
BCC – Blind Carbon Copies (if any).
Subject – Subject of the Email.
Body – Body of the Email.
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any).
ReplyTo – ReplyTo Email address.
Now create controller and import the following namespaces
using System.IO; 
using System.Net; 
using System.Net.Mail;

Add below code in your controller

public class EmailController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult Index(EmailModel model)
    {
        using (MailMessage mm = new MailMessage(model.Email, model.To))
        {
            mm.Subject = model.Subject;
            mm.Body = model.Body;
            if (model.Attachment.Length > 0)
            {
                string fileName = Path.GetFileName(model.Attachment.FileName);
                mm.Attachments.Add(new Attachment(model.Attachment.OpenReadStream(), fileName));
            }
            mm.IsBodyHtml = false;
            using (SmtpClient smtp = new SmtpClient())
            {
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password);
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
                ViewBag.Message = "Email sent.";
            }
        }
 
        return View();
    }
}

View

@model Send_Email_MVC_Core.Models.EmailModel
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td style="width: 80px">
                        To:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.To)
                    </td>
                </tr>
                <tr>
                    <td>
                        Subject:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Subject)
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        Body:
                    </td>
                    <td>
                        @Html.TextAreaFor(model => model.Body, new { rows = "3", cols = "20" })
                    </td>
                </tr>
                <tr>
                    <td>
                        File Attachment:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Attachment, new { type = "file" })
                    </td>
                </tr>
                <tr>
                    <td>
                        Gmail Email:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Email)
                    </td>
                </tr>
                <tr>
                    <td>
                        Gmail Password:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Password, new { type = "password" })
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="Send"/>
                    </td>
                </tr>
            </table>
            <br/>
            <span style="color:green">@ViewBag.Message</span>
        }
    </div>
</body>
</html>

Now the project is ready to execute.

Priyank Parekh

Priyank Parekh is a Web Developer. He has got his skills in working on technologies like C#, .NET ,ASP.NET, ASP.NET MVC, ASP.NET Core , CSS, SCSS, Entity Framework, Entity Framework Core, Bootstrap, HTML, JavaScript, JQuery, LINQ, ReactJS, Angular, Web API, SQL, SQL Server.

Share
Published by
Priyank Parekh

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago