I have a class which I fire with a button from opportunity detail page with a lookup field Contact to Contact obj. When i debug mail I see all info listed but it cant receive the email:

public class SendemailController {
    public String OpportunityId {get;set;}

    Public SendemailController(){
        OpportunityId = ApexPages.currentPage().getParameters().get('Id');
        system.debug('Opportunity  id->'+OpportunityId );

    Public Pagereference sendEmailFunction(){
        Opportunity getEmail = [SELECT Id, Contact__r.email FROM Opportunity WHERE id=:OpportunityId];
        if(getEmail.Contact__r.email != null) {
            String toaddress = getEmail.Contact__r.email;
            system.debug('toaddress  ->'+toaddress);  
            try {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {toaddress};
                String[] ccAddresses = new String[] {'[email protected]'};
                mail.setSubject('Testing email through apex');
                mail.setPlainTextBody('This is test email body. This mail is being sent from apex code');
                //mail.setHtmlBody('<b> This is HTML body </b>' );

                List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
                for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :OpportunityId]){
                    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                    //mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

                system.debug('fileAttachments  id->'+fileAttachments );    


                system.debug('mail.setFileAttachments  id->'+ mail);      

                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

            } catch(Exception e) {}

        PageReference reference = new PageReference('https://eu11.salesforce.com/'+ OpportunityId);
        return reference;

button (visualforce page)

<apex:page action="{!sendEmailFunction}"  Controller="SendemailController">         
    <apex:pageMessages ></apex:pageMessages><!-- included for display of errors should they occur -->         
    <apex:detail inlineEdit="true" relatedList="true"></apex:detail> <!-- included so Account detail information is visible when errors occur --> 
  • For debugging purposes, it would be helpful to remove the try/catch surrounding the code that is building the email. Your catch block is empty, so if you are getting an exception, you are 'handling' it without doing anything. Adding throw e; (to re-throw the exception so that it hopefully reaches your attention), or a simple debug statement in your catch block would also work.
    – Derek F
    Feb 27, 2017 at 14:44

1 Answer 1


Please Go To Email Administration >> Deliverability >> Select Access level >> instead of system Email select All Emails.

enter image description here

If this doesn't solve your problem
Go for testing Testing Deliverability
To test email deliverability permission needed: “Modify All Data”
1. From Setup, enter Test Deliverability in the Quick Find box, then select Test Deliverability.
2. Enter your business email address. Click Send. Salesforce sends a test message from all IP addresses to your business email address. Each test message specifies the IP address from which it was sent.
3. To make sure that you received all test messages, check your business email account.
4. If you didn’t receive all the test messages, your organization’s email administrator must whitelist the Salesforce IP ranges on your organization’s email server. Whitelisting an IP address allows the email server to receive email from an IP address that is otherwise blocked. For a current list of Salesforce IP ranges, see What are the Salesforce IP Addresses to whitelist?. Salesforce maintains separate IP addresses for inbound and outbound email. The IP addresses used for outbound email don’t accept inbound email connections.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .