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
Rai SahitaRai Sahita 

can we send an email with attachment which is record's related attachment?

CharuDuttCharuDutt
Hii Sahita
public class EmailAttachment{
    public static void sendEmail(){
        
        list<Account> lstAccount = [Select Id ,Name from Account];
        List<Messaging.SingleEmailMessage> emails =  new List<Messaging.SingleEmailMessage>(); 
        list<Messaging.EmailFileAttachment> emailAttachmentList = new list<Messaging.EmailFileAttachment>();
        for(Account Acc : lstAccount){
            Messaging.SingleEmailMessage emailMsg = new Messaging.SingleEmailMessage();
            emailMsg.setToAddresses(new list<String>{'CharuDutt8@gmail.Com'});
            emailMsg.setSubject('subject');
            emailMsg.setHtmlBody('body');
            list<Attachment> Att = [Select Id,Name,Body From Attachment where ParentId = :Acc.id];
            for(Attachment a: Att){
               Messaging.EmailFileAttachment meetingInviteIcs = new Messaging.EmailFileAttachment();
                meetingInviteIcs.setFileName(a.Name+'.ics');
                meetingInviteIcs.setBody(a.Body); 
                emailAttachmentList.add(meetingInviteIcs);
            }
        
        if(emailAttachmentList.size()>0){
        emailMsg.setFileAttachments(emailAttachmentList);
        }
        emails.add(emailMsg);
    }
    try{
        if(emails.size()>0){
        Messaging.sendEmail(emails);
        }
    }catch(System.EmailException ex){
        system.debug('Issue in sending email >> ' + ex.getMessage());
    }     
}
}
Please Mark It As Best Asnwer If It Helps
Thank You!
Rai SahitaRai Sahita

Hello @CharuDutt

appreciate you 

I've tried with above class, but i cound't get the attachment file while receiving the mail.
There is nothing wrong with your class

I think there is something problem with notesAndAttachments while sending to an email. i may need to make some changes in query.

Thank you.

 

CharuDuttCharuDutt
Hii Sahita
Please Close Your Query By Marking It As Best Answer So It Also Help others In Future
Thank You!