How to you add email to a Core 7 Web App?

Mark Phillips 0 Reputation points
2023-11-10T16:02:14.47+00:00

In .net framework its easy to add SMTP email services to an app via the _AppStart file.

Now that I'm trailling Core 7 - how do I add email to the Demo app?

Most of the guidance on the net applies to a Core API which is irrelevant as I'm just doing a simple Core App via the Add new Proejct - ASP.NET Core Web app template.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,502 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Mark Phillips 0 Reputation points
    2023-11-10T16:41:00.2366667+00:00

    Thanks, but there seems to be missing steps in this example solution.

    eg there's no Startup.cs file in the Demo install (and I don't know what it is anyway :) )

    And I don't use SendGrid - just a normal SMTP email server setup is enough for me now.

    0 comments No comments

  2. ChandraHundigamVenkat 11 Reputation points
    2023-11-10T23:38:50.1933333+00:00

    Use .NET SmptClient class as shown below

    using Microsoft.AspNetCore.Identity.UI.Services;
    using System.Net;
    using System.Net.Mail;
    using System.Threading.Tasks;
    
    namespace YourProject.Services
    {
        public class EmailSender : IEmailSender
        {
            public Task SendEmailAsync(string email, string subject, string htmlMessage)
            {
                SmtpClient client = new SmtpClient
                {
                    Port = 587,
                    Host = "smtp.gmail.com", //or another email sender provider
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential("your email sender", "password")
                };
    
                return client.SendMailAsync("your email sender", email, subject, htmlMessage);
            }
        }
    }
    

  3. Bruce (SqlWork.com) 63,746 Reputation points
    2023-11-20T16:17:27.4966667+00:00

    In min api apps, program.cs is the startup file.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.