How to create array to send email to multiple persons?

Hello Folks,

I am iterating using For Each through an excel to Extract some report using SAP and Excel. I have a list of items and their respective emails in an excel. The names are repetitive but I want the mail to be sent only once to any person even if their email id is getting repeated.

I have Included SendEmail workflow in For Each loop itself, so if i use CurrentRow(email).tostring, multiple emails are sent about every item. I do not want that . Please suggest

The list is as shown below

image

@Chetan_Wagh

  1. read excel as datatable variable e.g. “dt”
  2. assign the following to a string variable
    String.Join(";"c, dt.asEnumerable.Select(function(x) x("Email").toString).ToList.Distinct.ToList)

step 2 will create a string like this, simply pass this to your send email activity
abc@gmail.com;xyz@yahoo.com;123@outlook.com

Hi @Chetan_Wagh

→ Use Read Range Workbook to read the excel and store it in a datatable say dt_data.


→ Use the below syntax in Assign activity:

uniqueEmailID = dt_data.DefaultView.ToTable(True, "Email").AsEnumerable().Select(Function(row) row("Email").ToString()).ToArray()

uniqueEmailID is of DataType Array(System.Strirng)

→ Use For Each loop to iterate through uniqueEmailID and use Send SMTP or Send Outlook Mail Message.


Workflow:

Regards

Hi @Chetan_Wagh

Use below expression to get all the email ids in Array (Not repetitive - Only Unique)

DT.AsEnumerable().Select(Function (a) a.Field(of string)(“Email”).ToString).Distinct.ToArray()

Hope it will helps you :slight_smile:
Cheers!!

->Read the Excel using read range workbook and store in a data table.
→ Filter the data table to get Unique Emails
for eg: dtUniqueEmails = dt.DefaultView.ToTable(True, “EmailColumn”)
→ Use For Each Row and Loop Through Unique Emails
→ Inside the loop, use the Send Email activity to send emails to the corresponding email addresses.

2 Likes

Hey @Chetan_Wagh ,

->Create an empty list “EmailList” before iterating through for each loop.
→ Inside the loop use an “If” condition to check if the current row’s email address is already in the “EmailList” column or not.

Use “EmailList.Contains(CurrentRow(“email”).ToString)” condition to check for duplicates.

->If the email address is not present in the “EmailList”, then send an email using the “Send Outlook Mail Message” activity, passing the current row’s email address as the recipient.

After sending the email, add the email address to the “EmailList” using the “EmailList.Add(CurrentRow(“email”).ToString)” statement.

You can also refer the steps below:

  • Assign EmailList = New List(Of String)

  • For Each Row in data

    • If Not EmailList.Contains(row(“email”).ToString)
      • Send Outlook Mail Message (or any other email activity) (set the recipient email address using row(“email”).ToString)
      • Assign EmailList = EmailList.Append(row(“email”).ToString).ToList()

Hope it helps!!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.