1

I want to attach files dynamically with emails..may be computer you can say..Can we do that...I have created a VF page and using apex:inputfile...Can we send email with attachments..Please provide me some sample code.

 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 mail.setToAddresses(emailList);           
 String subject='Trend Analysis Report-PI';
 transient Messaging.EmailFileAttachment [] efaList = new  List<Messaging.EmailFileAttachment> () ;
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(filename );
        efa.setBody(fileBody);
        efa.setContentType(conType);
        efaList.add(efa) ;

        if(efaList != null) {
        mail.setFileAttachments(efaList) ;
        }
        mail.setCharset('UTF-8'); //Use Signatue
        mail.setUseSignature(true);
        // Send the email

                    mail.setSubject(subject);


        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

VF page

<apex:inputFile value="{!filebody}" fileName="{!filename}" contentType="{!conType}">

I am also getting view state error while uploading file....Can some one tell me how to dynamically attach files.

Thanks

2

You can use setFileAttachments on the Messaging.SingleEmailMessage object:

Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setBody(fileBody);
attachment.setContentType(conType);
attachment.setFileName(filename);
attachment.setinline(false);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
  • I am getting heap size error while uploading even a smaller file...maximum view state size limit (135kb) exceeded..can you help me solve that.. – miku Jan 27 '14 at 2:23
  • 1
    You can use Transient keyword for variable that are not required to post.That helps in decreasing view state. – Arun SFDC Jan 27 '14 at 13:59
  • This works, but has anyone figured out how to get HTML recorded into the task? I even tried putting all the HTML into an attachment, but the email's attachment isn't recorded in activity history. – tggagne Oct 1 '15 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.