0

I have such constructor of ApplicationDbContext class (I think quite standard one)

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
{
}

How I can create a new class instance with a specified connection string?

ApplicationDbContext   applicationDbContext = new ApplicationDbContext (??);
4
  • 1
    Why do you want to achieve that? Context is already loaded and register in start-up.cs file itself. Commented Jan 17, 2022 at 12:09
  • @KiranJoshi Good question:) Inside a class that implements the IEmailSender interface, I would like to save the sent e-mails. And since this is an interface, I can't change the constructor Commented Jan 17, 2022 at 17:26
  • 1
    That you can achieve separately as well. Call your send email method from the respective method and from that method itself save your email data. If you want than I can provide you an example of same. Commented Jan 18, 2022 at 4:37
  • @KiranJoshi I think that i know how it should looks but if you can pls put example. Still such approach need change at any place that that method is called, instead making modification at one place Commented Jan 18, 2022 at 17:45

1 Answer 1

1

You can save your email data like below:

/// <summary>
        /// SentBulkEmail to recipient
        /// </summary>
        /// <param name="emailModel"></param>
        /// <param name="sentBy"></param>
        public async Task<CustomResponseModel> SentBulkEmail(SendEmailModel emailModel, int sentBy = 0)
        {
            CustomResponseModel response = new CustomResponseModel();
            try
            {
                List<EmailSentHistoryModel> lstEmailSentHistory = new List<EmailSentHistoryModel>();
                Parallel.ForEach(emailModel.Recipient, (item) =>
                {
                    EmailSentHistoryModel historyModel = new EmailSentHistoryModel();
                    historyModel.EmailTemplateId = emailModel.EmailTemplateId;
                    historyModel.IsSent = Utilities.SendEmail(item, emailModel.Description, emailModel.Subject);
                    historyModel.Recipient = item;
                    historyModel.Sentdate = DateTime.UtcNow;
                    historyModel.Sentby = sentBy;
                    lstEmailSentHistory.Add(historyModel);
                });
                response = await _emailTemplateRepository.SaveEmailSentHistory(lstEmailSentHistory);
                response.Message = "Email sent successfully!!";
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = Constant.ServerError;
                _logger.LogError("{0}: SentBulkEmail() : {1}", Constant.ServiceError, ex.Message);
            }
            return response;
        }

Below is Email sent method:

public static bool SendEmail(string emailAddress, string body, string subject)
        {
            var _logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
            try
            {
                using (MailMessage mail = new MailMessage(ConfigItems.From, emailAddress))
                {
                    mail.Subject = subject;
                    mail.Body = body;
                    mail.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient()
                    {
                        Host = ConfigItems.Host,
                        EnableSsl = true
                    };
                    NetworkCredential NetworkCred = new NetworkCredential(ConfigItems.From, ConfigItems.Password);
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = NetworkCred;
                    smtp.EnableSsl = true;
                    smtp.Port = ConfigItems.Port;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.Send(mail);
                }
                return true;
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message.ToString());
                return false;
            }
        }

You can see in SentBulkEmail I used that method to get whether email and sent or not and also save the data from their as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.