0

I have written an email service class to sendout email to multiple email ids. Which sends email to multiple email ids using smtpclient class.Here is the method which sends the emails.

private async Task SendEmail(EmailOptions emailOptions) 
{ 
    MailMessage mail = new MailMessage 
                           { 
                               Subject = emailOptions.Subject, 
                               Body = emailOptions.Body, 
                               From = new MailAddress(_mailSettings.SenderAddress, _mailSettings.SenderDisplayName), 
                               IsBodyHtml = _mailSettings.IsBodyHTML 
                           };

    foreach (var toEmail in emailOptions.ToEmails)
    {
        mail.To.Add(toEmail);
    }

    NetworkCredential networkCredential = new NetworkCredential(_mailSettings.UserName, _mailSettings.Password);

    SmtpClient smtpClient = new SmtpClient
        {
            Host = _mailSettings.Host,
            Port = _mailSettings.Port,
            EnableSsl = _mailSettings.EnableSSL,
            UseDefaultCredentials = _mailSettings.UseDefaultCredentials,
            Credentials = networkCredential
        };
       
    mail.BodyEncoding = Encoding.Default;

    await smtpClient.SendMailAsync(mail);
    smtpClient.Dispose();
}

private string GetEmailBody(string templateName) 
{
    const string templatePath = @"EmailTemplates/{0}.html";

    var body = File.ReadAllText(string.Format(templatePath, templateName));
    return body;
}

And this is the update place holder in email template method

private string UpdatePlaceHolders(string text, List<KeyValuePair<string, string>> keyValuePairs)
{
        if (!string.IsNullOrEmpty(text) && keyValuePairs != null)
        {
            foreach (var placeholder in keyValuePairs)
            {
                if (text.Contains(placeholder.Key))
                {
                    text = text.Replace(placeholder.Key, placeholder.Value);
                }
            }
        }

        return text;
}

public class EmailOption
{
        public string ToEmail { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
         public List<KeyValuePair<string, string>> PlaceHolders { get; set; }
}

Here is the emailbody method, where I am filling the template with the username from the placeholder. For each email i send the content will be same except the salutation. Hi Mr X, Hi Mr Y. I can send mails to multiple email ids but how do i change/ specify different only the salutation in the email. In my template, I have placed a placeholder

Dear <username> 

like Dear Myusername

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    try liquid email templates. https://auth0.com/docs/customize/email/email-templates/use-liquid-syntax-in-email-templates – Power Mouse Aug 17 '23 at 17:10
  • Here my issue is if i am sending mail to UserName1@gmail.com the mail body should have a salutaion "Dear UserName1" (actual username not extracted from emailid) and ,UserName22@gmail.com the mail body should have a salutaion "Dear UserName22" .. – Dhanyanjay Singh Aug 17 '23 at 17:42
  • I have the class above EmailOption – Dhanyanjay Singh Aug 17 '23 at 17:46
  • 1
    again, you can have a list/dictionalry of email,user name and apply each email to template to match name to replace. in this case eavry email to user#@xxx.com will have dear user# ... – Power Mouse Aug 17 '23 at 17:49
  • so, add a logic, when placeholder name == "userName" (or something) add "Dear " in front of value. kind of "Dear " + placeholder.Value – Power Mouse Aug 17 '23 at 17:54
  • Try using : https://www.nuget.org/packages/FormatWith Here's an example - https://stackoverflow.com/questions/733378/whats-a-good-way-of-doing-string-templating-in-net/61905250#61905250 – jimnkey Aug 17 '23 at 18:24
  • @PowerMouse You meant to loop through all the items and send mail to users one by one instead of sending a mail to multiple toemails in one go .ie foreach (var toEmail in emailOptions.ToEmails) { mail.To.Add(toEmail); } this need to be excluded and call await smtpClient.SendMailAsync(mail); i – Dhanyanjay Singh Aug 17 '23 at 18:36
  • each email contain unique email body related to that email object. yes. you need to send each email separately for each dear user. – Power Mouse Aug 18 '23 at 13:24

0 Answers0