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
shashikant pandeyshashikant pandey 

send Email with attachment not getting send

I have tried below code for send email with attachment and I am unable to receive the attachment file along with email. Even email also not getting received.Below is my code:

1. I debug the code and I got the attached file id,content-type,Name and all thing which is required but not receiving mail. If anyone can help, i will aprriciate their help. Thanks!
Apex class:
public class sendEmailAttachmentPR {
    public Pricing_Reconfirmation__c prId{get;set;}
    public String primaryContactId {get;set;}
    public String AccountId{get;set;}
    public String email{get;set;}
    
    public pagereference sendEmailAttchament(){
        
        prId = [Select Id,Name,Account__r.Id,Primary_Contact__r.id from Pricing_Reconfirmation__c where id =: ApexPages.currentPage().getParameters().get('id')];
        primaryContactId = prId.Primary_Contact__r.id;
        AccountId = prId.Account__r.Id;
        system.debug('Primary Contact Id ====>'+primaryContactId);
        system.debug('Account Id ===> '+AccountId);
        
        Contact con = [SELECT ID,Name,Email,Phone from Contact where Id =:primaryContactId];
        email = con.email;
        system.debug('Email Id  =======>'+email);
        User currentUser = [SELECT Id FROM User Where Id = :UserInfo.getUserId()];
        
        Attachment attachFile = [SELECT Id,Name,parentId,LastmodifiedDate,ContentType,body FROM Attachment where parentId=:prId.Id ORDER BY LastmodifiedDate Desc LIMIT 1];
        system.debug('Attachment File Id '+attachFile.id);
      
            Messaging.EmailFileAttachment emailAttach = new Messaging.EmailFileAttachment();
            emailAttach.setContentType(attachFile.ContentType);
            system.debug('ContentType ====> '+attachFile.ContentType);
            emailAttach.setFileName(attachFile.Name);
            system.debug('FIle Name ===>'+attachFile.Name);
            emailAttach.setInline(false);
            emailAttach.Body = attachFile.Body;
            system.debug('Body ====> '+emailAttach.Body);
            
            
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            semail.setEntityAttachments(new Id[]{attachFile.Id});
            semail.setSubject('Sending Document as attachemnt');
            String[] sendTo = new String[]{'shashikant@xxxxx.com'};
                semail.setToAddresses(sendTo);
            semail.setPlainTextBody('Please find the attached document details');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
            
            system.debug('Email Sent Successfully');
        
            //system.debug('Email Id available for Contact');
     
        return null;
    }

}
VF Page:
<apex:page controller="sendEmailAttachmentPR">
    
    <apex:form >
        <apex:pageMessages />
        <apex:commandButton value="Send Email" action="{!sendEmailAttchament}"/>
    </apex:form>  
</apex:page>

could anyone help me here?
Ankit Kalsara 6Ankit Kalsara 6
Hi Shashikant,

Below line seems to be the issue.

emailAttach.Body = attachFile.Body;

Can you try setting the parameter and see if it works ?
emailAttach.setBody(attachFile.Body;)
shashikant pandeyshashikant pandey
Above mine code is working now. Thanks for your reply!