function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sachin Verma 88Sachin Verma 88 

Email Is not triggering from Apex

Hi,
I have created one class to send the email to a particular customer When I call that class the send email method gives me success is true but email is not sending to the customer. Please check my code.

Please help me with this.
public class WorksContractEmailController{

public String strMessage{get;set;}

Public WorksContractEmailController(){
    try{
    String oppId = ApexPages.currentPage().getParameters().get('id'); 
    Opportunity oppObj = [Select Id,Account.PersonContactId,Works_Contract_Sign_Off_Date__c,Customer_Email__c From Opportunity Where Id = :oppId LIMIT 1];
    SendWorksContact(oppObj);
    }catch(Exception exp){
      strMessage = exp.getMessage();
    }
}


public PageReference SendWorksContact(Opportunity objOpp){
    try{
        if(objOpp.Works_Contract_Sign_Off_Date__c != null){
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            EmailTemplate templateId = [Select Id from EmailTemplate where name = 'Works_Contract_Attached' Limit 1];
            email.setTemplateID(templateId.Id);
            PageReference pdf = Page.WorksContractVFPage;
            pdf.getParameters().put('id',objOpp.id);
            pdf.setRedirect(true);
            Blob b = pdf.getContent();
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('WorksContarct.pdf');
            efa.setBody(b);
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
            email.setToAddresses(new String[] {objOpp.Customer_Email__c});
            email.setTargetObjectId(objOpp.Account.PersonContactId);
            email.setSaveAsActivity(false);
            email.setWhatId(objOpp.Id);
            system.debug('------email----'+email);
            system.debug('------objOpp.Account.PersonContactId----'+objOpp.Account.PersonContactId);
            Messaging.SendEmailResult [] objResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email},false);
            if(objResult[0].isSuccess())
              strMessage = 'Email has been send.';
            else
             strMessage = 'Error - '+objResult[0].errors[0].message+' to send to the works contract,Please contact to your system administrator.';  
            system.debug('-----(objResult[0].isSuccess()----'+objResult[0].isSuccess());  
        }else{
            strMessage = 'Please update Works Contract sign off date.';
        }
    }catch(Exception exp){
      strMessage = exp.getMessage();
    }

 return null;

}


Thanks,
 
lazy coderlazy coder
Hey , 

Check your email deliverabilty settings  https://help.salesforce.com/articleView?id=000212386&type=1 

User-added image

Regards,
Lazy coder
Raj VakatiRaj Vakati
from the Setup -->  Email Administration-- >Deliverability -->Change access levels to All Emails 
Sachin Verma 88Sachin Verma 88
Hi,

Email Access Levels is All Email, But Email is not triggering.

Please help me for this.