1

I use this to send mails, I've defined some package variables which contains values:

public MailProperties(ScriptObjectModel dts)
        {
            if (
             !string.IsNullOrEmpty((string)dts.Variables["$Package::mailFrom"].Value) &&
             !string.IsNullOrEmpty((string)dts.Variables["$Package::mailTo"].Value) &&
             !string.IsNullOrEmpty((string)dts.Variables["$Package::mailPwd"].GetSensitiveValue()) &&
             !string.IsNullOrEmpty((string)dts.Variables["$Package::mailSmtp"].Value) &&
             !string.IsNullOrEmpty((string)dts.Variables["$Package::mailPort"].Value)

             )
            {
                fromMail = (string)dts.Variables["$Package::mailFrom"].Value;
                toMail = (string)dts.Variables["$Package::mailTo"].Value;
                bccMail = (string)dts.Variables["$Package::mailBcc"].Value;
                accountPassword = (string)dts.Variables["$Package::mailPwd"].GetSensitiveValue();
                accountSmtp = (string)dts.Variables["$Package::mailSmtp"].Value;
                accountSmtpPort = (string)dts.Variables["$Package::mailPort"].Value;
                useSSL = (bool)dts.Variables["$Package::useSSL"].Value;


                pathAttachment = new List<string>();
                pathAttachment.Add((string)dts.Variables["User::pathFileReject"].Value);
                pathAttachment.Add((string)dts.Variables["User::pathFileReject2"].Value);
            }
            else
            {
                throw new Exception("error text...");
            }

        }

I need to send mail to more people, so I set mailTo with mail1@gmail.com, mailBcc as mail2@gmail.com and it works, but if I set mailBcc as "mail2@gmail.com, mail3@gmail.com" or "mail2@gmail.com; mail3@gmail.com" it doesn't work, how I can do that?

EDIT: this is the sendMail function

public static void sendMail(MailProperties mailProperties, ReportETL reportETL)
        {
            MimeMessage message = new MimeMessage();
            message.From.Add(new MailboxAddress("text..", mailProperties.fromMail));
            message.To.Add(new MailboxAddress(mailProperties.toMail));
            message.Subject = "text...";

            if (!string.IsNullOrEmpty(mailProperties.bccMail))
            {
                message.Bcc.Add(new MailboxAddress(mailProperties.bccMail));
            }

            BodyBuilder bodyBuilder = new BodyBuilder();
            bodyBuilder.HtmlBody = ReportETLService.getHtmlFromReporETL(reportETL);

            mailProperties.pathAttachment.Where(x => File.Exists(x)).ToList().ForEach(y => bodyBuilder.Attachments.Add(y));

            message.Body = bodyBuilder.ToMessageBody();

            try
            {
                SmtpClient smtpClient = new SmtpClient();

                smtpClient.Connect(mailProperties.accountSmtp, int.Parse(mailProperties.accountSmtpPort), mailProperties.useSSL);
                smtpClient.Authenticate(mailProperties.fromMail, mailProperties.accountPassword);
                smtpClient.Send(message);

                smtpClient.Disconnect(true);
            }
            catch (Exception e) { throw new Exception("text... " + e.Message); }

        }
marko
  • 487
  • 1
  • 6
  • 15
  • How do you send email ? Sql ? third party app ? or another way – Bahtiyar May 28 '20 at 08:47
  • @Bahtiyar I've added the sendMail function – marko May 28 '20 at 08:49
  • Does this answer your question? [Send a single email to multiple recipients using Mailkit or mimekit](https://stackoverflow.com/questions/53456619/send-a-single-email-to-multiple-recipients-using-mailkit-or-mimekit) – PaulF May 28 '20 at 08:57

4 Answers4

1

according to documentation message.To is list type.So you can add more adress like this.

 InternetAddressList list = new InternetAddressList();

    list.add(adress1)
    list.add(adress2)
    list.add(adress3)
    list.add(adress4)
message.To.AddRange(list);
Bahtiyar
  • 192
  • 1
  • 6
  • How do you add address1, address2 from string (mail address)? Means how to convert string to InternetAddress? – Bcktr Nov 06 '21 at 14:01
0

Instead of using BCC (which would be an option, but has the problem of max count) just iterate over all mail addresses and send one for each recipient. (Not to fast or you get other problems) This way it should be "easier" to spot mails that will not get delivered.

Stejin
  • 64
  • 4
0

Well, assuming that your toMail, ccMail, and bccMail properties are just strings and not lists of strings, you could do something like this:

InternetAddressList list;

if (!string.IsNullOrEmpty(mailProperties.toMail) && InternetAddressList.TryParse (mailProperties.toMail, out list))
    message.To.AddRange(list);

if (!string.IsNullOrEmpty(mailProperties.ccMail) && InternetAddressList.TryParse (mailProperties.ccMail, out list))
    message.Cc.AddRange(list);

if (!string.IsNullOrEmpty(mailProperties.bccMail) && InternetAddressList.TryParse (mailProperties.bccMail, out list))
    message.Bcc.AddRange(list);
jstedfast
  • 35,744
  • 5
  • 97
  • 110
0

I had a similar issue and this answer helped me, i believe is the same problem: Send a single email to multiple recipients using Mailkit or mimekit.

Basically, you will want to use the AddRange function of the MimeMessageEntity passing an InternetAddressList to it, which is a list of InternetAdress class. InternetAddress is abstract and you can't have an instance for it, so you want to use a implementation like MailBoxAddress. you can create a list of your string receiver's email adresses and write the following:

InternetAddressList parsedAdresses = new();
//adresses is your list of receivers, as a list of string email adresses
foreach(string adress in adresses)
{
     parsedAdresses.Add(MailboxAddress.Parse(adress));
}
//mailMessage is your MimeMessage
mailMessage.To.AddRange(parsedAdresses); 
Joel Lopes
  • 71
  • 1
  • 3