Home > front end >  .NET Core - Confirming asset has been included with production build
.NET Core - Confirming asset has been included with production build

Time:12-15

I've got a .NET Core Web API Solution with multiple projects, I'll post the structure below and the paths where necessary.

I have an Email Service that pulls a template from a path in the same project, parses it and sends it via Email.

Solution -
   Core (Project Models, Context and Interfaces)
   MainProject (Executed Project) -
      Controllers -
          EmailController.cs
   Repository
   Services -
      Email -
         templates -
            ContactEmailTemplate.html
         EmailService.cs

The email service looks like this:

using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using Core.Email.Services;
using Core.Email.Services.Models;
using Core.Services.Response;

namespace Services.Email
{
    public class EmailService : IEmailService
    {
        private const string SmtpAddress = "smtp.gmail.com";
        private const int PortNumber = 587;
        private const bool EnableSsl = true;
        private const string EmailFromAddress = ""; //Sender Email Address  
        private const string Password = ""; //Sender Password  
        private const string EmailToAddress = ""; //Receiver Email Address  
        private const string Subject = "New Email From...";

        public ServicesResponse<bool> SendEmail(EmailRequest emailRequest)
        {
            try
            {
                var mail = CreateMailMessage(emailRequest);
                
                using var smtp = new SmtpClient(SmtpAddress, PortNumber);
                smtp.Credentials = new NetworkCredential(EmailFromAddress, Password);
                smtp.EnableSsl = EnableSsl;
                smtp.Send(mail);

                return new ServicesResponse<bool>
                {
                    Data = true,
                    Exception = null,
                    HasError = false
                };   
            }
            catch (Exception exception)
            {
                return ServicesResponse<bool>.FromException(exception);
            }
        }

        private static MailMessage CreateMailMessage(EmailRequest request)
        {
            return new MailMessage
            {
                From = new MailAddress(EmailFromAddress),
                To = { EmailToAddress },
                Body = CreateMailMessageFromHtml(request),
                IsBodyHtml = true,
                Subject = Subject
            };
        }

        private static string CreateMailMessageFromHtml(EmailRequest request)
        {
            string body;
            
            using (var reader = new StreamReader(Path.Combine(AppContext.BaseDirectory, "../../../../Services/Email/templates/ContactEmailTemplate.html"))) /* <--- Is this even going to work in prod?            
  • Related