2

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[] {'test@email.com'};
                mail.setToAddresses(toAddresses);
                mail.setCcAddresses(ccAddresses);
                mail.setReplyTo(toaddress);
                mail.setSenderDisplayName('Name');
                mail.setSubject('Testing email through apex');
                mail.setBccSender(false);
                mail.setUseSignature(true);
                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();
                    efa.setFileName(a.Name);
                    efa.setBody(a.Body);
                    fileAttachments.add(efa);
                    //mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
                }

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

                mail.setFileAttachments(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);
        reference.setRedirect(true);
        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 --> 
</apex:page>
  • 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 '17 at 14:44
3

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.
Ref:https://help.salesforce.com/articleView?id=security_test_email_delivery.htm&type=0&language=en_US

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.