Using a Asp.NET Core Web application, it is very easy to send an email and get the SMTP settings (SMTP Server, From Address and Alias(Display Name)) from file.
The purpose of this blog is about how to get SMTP settings from file, so email content (Subject and Message) is simple. Follow the below steps for that.
For this example, we are going to use,
- Microsoft Visual Studio Community 2019.
Now, let’s create an Asp.NET Core Web Application,
Step 1
First, click on the option Create a New Project. After that, we select the Asp.NET Core Web Application.

Step 2
Let’s give a name to our Project.

Step 3
Let’s select the empty template and disable the option Configure for HTTPS, for this example, we don’t need to configure HTTPS.

Our project is going be like this,

Step 4
Now, we will change our Startup.cs file.
We need to configure Mvc Services and the Access to our appsettings.json file. The IConfiguration interface is responsible for access the appsettings file, it is obtained in class constructor by Dependency Injection.
Startup.cs
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- namespace WebApplicationSendEmail {
- public class Startup {
- public IConfiguration Configuration {
- get;
- }
- public Startup(IConfiguration configuration) {
- Configuration = configuration;
- }
- // This method gets called by the runtime. Use this method to add services to the container.
- // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
- public void ConfigureServices(IServiceCollection services) {
- services.AddTransient<EmailHelper>();
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
- if (env.IsDevelopment()) {
- app.UseDeveloperExceptionPage();
- }
- app.UseMvc(routes => {
- routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
- });
- }
- }
- }
Step 5
Now, we need to add SMTP settings in appsetings file.
Appsettings.file
- {
- "Logging": {
- "LogLevel": {
- "Default": "Warning"
- }
- },
- "AllowedHosts": "*",
- //SMTP settings
- "SMTP": {
- "Host": "YOUR_HOST",
- "From": "YOUR_ADDRESS",
- "Alias": "MyWebSite"
- }
- }
Don’t forget, change Host, From and Alias according you need.
Step 6
Let’s create the folders called “Controllers”, “Helper”, “Model”, “Views” and “Home”, the controller called “HomeController”, the classes called “EmailHelper”, “EmailModel” and the view called “Index”.
Right-click on Project (“WebApplicationSendEmail”) and go to Add.

Index.cshtml



Hamid KhanPosted Sep 21, 2019, 6:53 AM
Good explanation...……….Thanks for helping us