Multiple emails not sending in CC or BCC fields of email template.

Tom Wisneski asked on September 15, 2022 18:58

Using an email template in Kentico 11. Not able to run it's scheduled task when putting in multiple email address separated by semi-colons ";". example of code being used (credentials and stmpclient stuff blocked out)

                SmtpClient emailClient = new SmtpClient("--", --);
                {
                    MailMessage message = new MailMessage(fromEmail, toEmail);
                    message.IsBodyHtml = true;
                    message.Subject = resolver.ResolveMacros(template.TemplateSubject);
                    message.Body = resolver.ResolveMacros(template.TemplateText);
                    if (!string.IsNullOrWhiteSpace(template.TemplateCc))
                    {
                        message.CC.Add(template.TemplateCc);
                    }
                    if (!string.IsNullOrWhiteSpace(template.TemplateBcc))
                    {
                        message.Bcc.Add(template.TemplateBcc);
                    }
                    if (attachment != null)
                    {
                        message.Attachments.Add(attachment);
                    }
        emailClient.Credentials = new NetworkCredential("---", "----");
                    emailClient.Send(message);

Correct Answer

Tom Wisneski answered on September 16, 2022 23:25

Dmitry...your answer was correct! Thank you. Just had to do one tweak. It works now.

foreach (var individualEmail in template.TemplateCc.Split(';')) {

  message.CC.Add(new MailAddress(individualEmail.Trim()));

}

0 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on September 16, 2022 15:56

Hi Tom,

What particular error do you have when executing this code?

0 votesVote for this answer Mark as a Correct answer

Tom Wisneski answered on September 16, 2022 16:08 (last edited on September 16, 2022 16:09)

Dimitry, the error is very basic: "An invalid character was found in the mail header: ';'." No spaces between emails in the CC field, just the separating ';'.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on September 16, 2022 16:22

What actual value can you see in template.TemplateCc? You probably should split it by ";" into actual email addresses and call message.CC.Add() in a cycle.

0 votesVote for this answer Mark as a Correct answer

Tom Wisneski answered on September 16, 2022 16:30

Apologize Dmitry -- Not sure I am clear on what you suggested. Do you mean either loop through the emails wanted in the CC field, or pass an array[] of emails separated by ';' ? Thanks.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on September 16, 2022 16:54

Something like this:

foreach (var individualEmail in template.TemplateCc.Split(';'))
{
    message.CC.Add(individualEmail);
}
0 votesVote for this answer Mark as a Correct answer

Tom Wisneski answered on September 16, 2022 17:03

Dmitry....That makes sense. Let me try this today. I will get back and put in the post here if this works (which it looks like it would). :)

0 votesVote for this answer Mark as a Correct answer

Tom Wisneski answered on September 16, 2022 22:09 (last edited on September 16, 2022 22:10)

Dmitry...still the same error; Here is the code.
SmtpClient emailClient = new SmtpClient("-", -); { MailMessage message = new MailMessage(template.TemplateFrom, template.TemplateReplyTo); message.IsBodyHtml = true; message.Subject = resolver.ResolveMacros(template.TemplateSubject); message.Body = resolver.ResolveMacros(template.TemplateText);

                    foreach (var individualEmail in template.TemplateCc.Split(';'))
                    {
                        message.CC.Add(individualEmail);
                    }

                    foreach (var individualEmail in template.TemplateBcc.Split(';'))
                    {
                        message.Bcc.Add(individualEmail);
                    }


                    if (attachment != null)
                    {
                        message.Attachments.Add(attachment);
                    }
                    emailClient.Credentials = new NetworkCredential("-", "-");
                    emailClient.Send(message);
                }




                Then here is the error, "An invalid character was found in the mail header: ';'."
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.