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
Force.platformForce.platform 

email with attachment from static resource

Hello All,
              I am new to salesforce. i want to send email with attachment that stored in static resources. please tell me how to do it
Pradip Kr. ShuklaPradip Kr. Shukla
Hi Arati,

For this, you just need to read the document body from the static resource as a blob and then make an instance of attachment and then use the same in your email. Like below
Map<String, Blob> res = new Map<String, Blob>();
for(StaticResource resource:[SELECT Name, Body FROM StaticResource WHERE Name IN ('TC_for_France','TC_for_Italy')])
{
res.put(resource.Name, resource.Body);
}
Messaging.SingleEmailMessage[] messages = new Messaging.SingleEmailMessage[0];
for(Attachment attachmentlst1: attachmentlst)
{
// ... omitted ...
Messaging.Emailfileattachment efa1 = new Messaging.Emailfileattachment();
efa1.setFileName(TCname);
efa1.setBody(res.get(resourceName));
// ... omitted ...
messages.add(mail);
}
Messaging.sendEmail(messages);
OR
You can store your attachment in the document rather storing in the static resource and then use that.

Hope this will help you.

Thanks
Daisy ScottDaisy Scott
Hi Arati,
You should use following code to send email with attachment that stored in static resources.
public class EmailStaticResource {
public void StaticresourceDataAsEmail(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setUseSignature(false);
email.setSaveAsActivity(true);
email.setSubject('Send Email using staticResource as body of mail ');
String[] toAddresses = new String[] {'ganesh.maharana@janbask.com'};
email.setToAddresses(toAddresses);
email.setHtmlBody('<html><body>Dear devoplers<b>See the code for Email service</b></body></html>');
StaticResourcesr = [Select  Name, Id, Body From StaticResource where Name = 'Document'];
        Blob tempBlob = sr.Body;
Messaging.EmailFileAttachmentefa = new Messaging.EmailFileAttachment();
efa.setBody(tempBlob);
efa.setFileName('attachment.pdf');
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
Messaging.SingleEmailMessage[] emailList = new Messaging.SingleEmailMessage[] {email};
Messaging.sendEmail(emailList);
            }
     }


User-added image