Skip to main content
i want to send email with attachment. please tell me how to do it. this is my class

 

 

public class CustomerImpl {

    public void sendEmailToCustomer(list<Customer__C>listcust)

       {

           for (Customer__C cust : listcust){

               Messaging.SingleEmailMessage msg= new Messaging.SingleEmailMessage();

               msg.setToAddresses(new String[]{Cust.Email_Address__c});

               msg.setBccAddresses(new String[] {Cust.Bcc_Address__c});

               msg.setSubject('info about session');

               msg.setPlainTextBody(cust.comment__c);

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

           }

       }

}

 

 
3 个回答
  1. 2017年8月21日 17:34
    Please use below code and let me know if it hepls you.

     

     

    public class CustomerImpl {

     public void sendEmailToCustomer(list < Customer__C > listcust) {

      List < Messaging.SingleEmailMessage > listOfEmails = new List < Messaging.SingleEmailMessage > (); //Used to store list of emails

      for (Customer__C cust: listcust) {

       Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();

       msg.setToAddresses(new String[] {

        Cust.Email_Address__c

       });

       msg.setBccAddresses(new String[] {

        Cust.Bcc_Address__c

       });

       msg.setSubject('info about session');

       msg.setPlainTextBody(cust.comment__c);

       Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();

       attach.setBody(Blob.valueOf('Some Text'));

       attach.setFileName('TesAttachmentName.pdf');

       attach.setContentType('application/pdf');

       attach.setInline(false);

       Messaging.EmailFileAttachment[] attachment = new Messaging.EmailFileAttachment[] {

        attach

       };

       msg.setFileAttachments(attachment);

       listOfEmails.add(msg); 

      }

      Messaging.sendEmail(listOfEmails, FALSE); 

     }

    }

     

    Thanks & Regards 

     

    David Hales(1053)

     

    Kloudrac Software Pvt. Ltd.
0/9000