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, from Pricing_Reconfirmation__c where id =: ApexPages.currentPage().getParameters().get('id')];
        primaryContactId =;
        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 =;
        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 ';
            Messaging.EmailFileAttachment emailAttach = new Messaging.EmailFileAttachment();
            system.debug('ContentType ====> '+attachFile.ContentType);
            system.debug('FIle Name ===>'+attachFile.Name);
            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[]{''};
            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}"/>

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 ?
shashikant pandeyshashikant pandey
Above mine code is working now. Thanks for your reply!