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
csbaacsbaa 

email with atachmanet

Hello Helpers

 

I would like to know  how  can I send email with attachment  from an apex class

 

I know  how  to send mails.

I sew articles about how  to attach a VFpage  rendered as pdf

 

but what I need is to attach a file  to my email

 

I am thinking to a static resource  

 

any suggestions?

 

regards

csbaa

 

Best Answer chosen by Admin (Salesforce Developers) 
David Lee(China Developer)David Lee(China Developer)

Hi, Please check this out:

Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
mail.setToAddresses(new List<String>{'acme@acme.com'});
mail.setSenderDisplayName('Email Demo');
mail.setReplyTo('no-reply@emaildemo.com');
mail.setSubject('Email Demo'); 
mail.setPlainTextBody('Acme');
List<Messaging.EmailFileAttachment> atts = new List<Messaging.EmailFileAttachment>();
String resourceName = 'Your static Resource name';
List<StaticResource> resources = [select Id, Name, Body from StaticResource where Name = :resourceName];
Blob emailAttachmentBody = null;
if(resources.size() > 0 && resources[0].Body != null)
{
   emailAttachmentBody = resources[0].Body;
}
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setFileName(resourceName);
attachment.setBody(emailAttachmentBody);
atts.add(attachment);
mail.setFileAttachments(atts); 
Messaging.sendEmail(new Messaging.Singleemailmessage[] {mail});

 

All Answers

neao18neao18

Go through this link:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm

 

and Get your file into a blob data type and attach that to email using setFileAttachments.

 

 

Mark as Answer if it Helped!!

Thiyagarajan.SelvarajThiyagarajan.Selvaraj

public void sendEmail(){
   
    //File Attachment
    Messaging.EmailFileAttachment att = new Messaging.EmailFileAttachment();
    att.setFileName('Your File Name');
    att.setBody(file body);
    att.setContentType('Your file content type(MIME)');
    
    
    //Single Email
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    String[] toAddress = new String[] {'List of email addresses'};
    email.setToAddresses(toAddress);
    email.setSubject('Email Subject');
    email.setPlainTextBody('body of the email');
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {att});
    
    // sends the email
    Messaging.sendEmailResult[] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    System.debug('Is Success '+result[0].isSuccess());
  }

 

Thanks,

 

Thiyagarajan Selvaraj

David Lee(China Developer)David Lee(China Developer)

Hi, Please check this out:

Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
mail.setToAddresses(new List<String>{'acme@acme.com'});
mail.setSenderDisplayName('Email Demo');
mail.setReplyTo('no-reply@emaildemo.com');
mail.setSubject('Email Demo'); 
mail.setPlainTextBody('Acme');
List<Messaging.EmailFileAttachment> atts = new List<Messaging.EmailFileAttachment>();
String resourceName = 'Your static Resource name';
List<StaticResource> resources = [select Id, Name, Body from StaticResource where Name = :resourceName];
Blob emailAttachmentBody = null;
if(resources.size() > 0 && resources[0].Body != null)
{
   emailAttachmentBody = resources[0].Body;
}
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setFileName(resourceName);
attachment.setBody(emailAttachmentBody);
atts.add(attachment);
mail.setFileAttachments(atts); 
Messaging.sendEmail(new Messaging.Singleemailmessage[] {mail});

 

This was selected as the best answer
csbaacsbaa
Thanks for the fast reply

this was the key: List<StaticResource> resources = [select Id, Name, Body from StaticResource


Regards
csaba