Send Email From A Razor Page In .NET Core 2 Using System.Net.Mail

In order to see how to create a .NET Core Web Application with Razor Pages and retrieve data from SQL Server using Entity Framework, you can visit my previous article.

Below are the softwares/concepts used in this document.

  1. Visual Studio 2019
  2. Razor Pages
  3. .Net Core 2.0
  4. Net Core Web Application
  5. C# Language

Razor pages in .NET Core 2.0 onwards have a library that we used to have in the .NET Framework to send emails (System.Net.Mail). That means there is no longer any need to rely on third party libraries or services for email functionality in your .NET Core applications.

Open your project in Visual Studio 2019

In my case, I am opening the earlier created project where Razor pages are present.

Send Email From A Razor Page In .NET Core 2 Using System.Net.Mail

Send Email using System.Net.Mail

Open the Razor page, from which you want to send an email. In my example, I am opening Index.cshtml under “Customers” folder where my customer data is displayed.

  1. Create a new class to hold the properties for email.
    1. namespace ABC.Test  
    2. {  
    3.     public class EmailMessage  
    4.     {  
    5.         public string To { getset; }  
    6.         public string From { getset; }  
    7.         public string Subject { getset; }  
    8.         public string Body { getset; }  
    9.     }  
    10. }  
  2. Create an instance of the newly created class on the page where you want to execute the email functionality. In my scenario, I am opening Index.cshtml.cs page. Also, using the BindProperty along with the instance.
    1. public class IndexModel : PageModel  
    2.     {  
    3.         private readonly ScaffModels.TestingCLRContext _context;  
    4.   
    5.         public IndexModel(ScaffModels.TestingCLRContext context)  
    6.         {  
    7.             _context = context;  
    8.         }  
    9.   
    10.         [BindProperty]  
    11.         public EmailMessage email { getset; }  
  1. The newly created properties should be assigned to their respective form values. So, to achieve this, we will add the form tag with some textbox in the “Index.cshtml” page.
    1. @page 
    2. @model Horizon.Core.Test.Pages.Customers.IndexModel  
    3.   
    4. @{  
    5.     ViewData["Title"] = "Index";  
    6. }  
    7.   
    8. <h2>Index</h2>  
    9.   
    10. <p>  
    11.     <a asp-page="Create">Create New</a>  
    12. </p>  
    13.   
    14. <form asp-page="./Index" method="post" enctype="multipart/form-data">  
    15.     <div class="form-actions no-color">  
    16.         <p>  
    17.             From : <input type="text" asp-for="email.From" /><br />  
    18.             To : <input type="text" asp-for="email.To" /><br />  
    19.             Subject : <input type="text" asp-for="email.Subject" /><br />  
    20.             Body : <textarea asp-for="email.Body"></textarea><br />  
    21.             <button asp-page-handler="SendEmail">Send Email</button>  
    22.         </p>  
    23.     </div>  
    24. </form>  
  1. Send email by calling the SendEmailAsync method by putting below code in “Index.cshtml.cs” page. Make sure to insert the “using System.Net.Mail” directive.
    1. public async Task OnPostSendEmail()  
    2.         {  
    3.             using (var smtp = new SmtpClient("Your SMTP server address"))  
    4.             {  
    5.                 var emailMessage = new MailMessage();  
    6.                 emailMessage.From = new MailAddress(email.From);  
    7.                 emailMessage.To.Add(email.To);  
    8.                 emailMessage.Subject = email.Subject;  
    9.                 emailMessage.Body = email.Body;   
    10.              
    11.                 await smtp.SendMailAsync(emailMessage);  
    12.             }  
    13.         }  
  1. Test the files by right-clicking on the Index file and open it with a browser. Enter the From, To, Subject, and Body of the email. Then, click the “Send Email” button.

    Send Email From A Razor Page In .NET Core 2 Using System.Net.Mail
  1. Email is received at the “To” address you provided while testing.

    Send Email From A Razor Page In .NET Core 2 Using System.Net.Mail

That is it. I hope you have learned something new from this article and will utilize this in your work.


Similar Articles