Search code examples
outlookexchangewebservicesblueprism

Writing code stage to determine if recipients are from the domain


I need to write a piece of code in the Code Stage in Blue Prism. I am dealing with Outlook and my current object can send out emails in Outlook.

However, what I need to do now is to check if the email addresses in to/cc/bcc are from the same domain (eg. check if all recipients' email addresses end with "@abc.com"). My current piece of code is coded in C#. Does anyone have any idea on how this can be done?

Here is what I have currently: I have a list of input parameters for fields like "to, from, cc, bcc, ..., abc_recipients_only". All the parameters are in string and the "abc_recipient_only" parameter is in a Boolean form.

The codes I have currently gives me an error saying "Compiler error at line 14: The name "Recipient" does not exist in the current context"".

    EmailMessage email = new EmailMessage(service);
    
    to = to.Replace(',',';');
    cc = cc.Replace(',',';');
    bcc = bcc.Replace(',',';');
    
    foreach(var row in to.Split(';')){
        if (abc_recipients_only == true){
            if(Recipient.AddressEntry == "abc.com"){
                email.ToRecipients.Add(row.Trim());
            }
        }
        else{
            if (string.IsNullOrEmpty(row.Trim())) continue;
            email.ToRecipients.Add(row.Trim());
        }
    }
    
    email.SendAndSaveCopy();

Solution

  • The MailItem.Recipients property returns a Recipients collection that represents all the recipients for the Outlook item. So, you can iterate over all items in the collection and check recipient's domains.

    Use the Recipients (index) method, where index is the name or index number, to return a single Recipient object. The name can be a string that represents the display name, the alias, the full SMTP email address, or the mobile phone number of the recipient. A good practice is to use the SMTP email address for a mail message, and the mobile phone number for a mobile message.

    The Recipient.Address property returns a string representing the email address of the Recipient.

    If you meet an exchange-like email address you can use the approach described in the HowTo: Convert Exchange-based email address into SMTP email address article.