You now have an awesome Salesforce instance that you use for your sales and marketing purposes. Your instance sends hundreds of mails weekly, many of them to prospective clients. But sometimes in a fast sales machine environment where your sales guys would be sending hundreds of bulk mails daily, it becomes very difficult to make sure that your sales agents do not send mails to some specific domains, or that they cross-check their lists every time from a common no-mailing lists.

You would come across cases like these in your business life-cycle, where you don’t want your sales prospectors to send mails to specific email addresses or even to specific domains, like unsubscribe markers, present clients, competitors, etc. Unfortunately it is very difficult to make sure through a process that your users don’t send mails to these domains, specifically for cases where your fellow SFDC users would be sending mails in bulk for marketing or prospecting.

The most simple solution is to make sure that your Salesforce instance itself is not able to send mails to said domains. Do this is not that difficult. You would just have to write a trigger.

In your salesforce org, if you want to blacklist domain name on which you don’t want to send mail from salesforce then you can use the following trigger to do this…

trigger BlackListEmailAddress1 on EmailMessage (before insert) {
    for(EmailMessage message: Trigger.New)
    {
        if((message.ToAddress.contains('gmail')) && message.Incoming == false )
        {
            message.addError('Email Alert: You have selected a receipient email from one of the blacklisted doamins please use another email address and try again');
        }
        if((message.ToAddress.contains('yahoo')) && message.Incoming == false )
        {
            message.addError('Email Alert: You have selected a receipient email from one of the blacklisted doamins please use another email address and try again');
        }
    }
}

In this trigger, we have blocked only two domains names Gmail and Yahoo. Your users won’t be able to send any mails that have Gmail or Yahoo in their domain name. If you need to block any specific ID or another domain, then all you have to do is add a new ‘if’ condition like

if((message.ToAddress.contains('example.com')) && message.Incoming == false )

This will work for all the objects where we have the option to send an email, like contacts, leads, custom objects, etc.