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
DmonikaDmonika 

i have a report which is attached and sent a mail , but the problem is the attachment in mail have no content

global class OPsCMMIBatchSendMail implements Database.Batchable<Sobject>,Database.AllowsCallouts
{
        
        
       /*  pmPlan__c cpm = pmPlan__c.getInstance('replyToAddress');          
         String reply = cpm.replyTo__c; */

       global Database.QueryLocator start(Database.BatchableContext BC)
        {
            String query = 'SELECT Id,Name,DM_s_E_Mail_ID__c,PM_s_EMAIL_Id__c FROM Ops_CMMI__c';
            return Database.getQueryLocator(query);
        }
       
       global void execute(Database.BatchableContext BC, List<Ops_CMMI__c> scope)
        {
            //List<String> maill=new List<String>();
         //   Set<String> email_new = new Set<String>();       
          ApexPages.PageReference report = new  ApexPages.PageReference('/00O61000002bwj9?csv=1');                          
          Messaging.EmailFileAttachment Attachment = new Messaging.EmailFileAttachment();  
          Attachment.setFileName('CMMI Report.csv');
        
         Attachment.setBody(report.getContent());
          Attachment.setContentType('csv');
         /*for(Ops_CMMI__c mp : scope)
          {
               List<String> email=new List<String>();
                Integer i;
                String mail = mp.PM_s_EMAIL_Id__c;
               
                if(mail!=Null)
                {
                    email = mail.split(',');
                   // system.debug(email);
                }
                for(i=0;i<email.size();i++)
                {
                   
                    email_new.add(email[i]);
                   // system.debug(maill);
                }
                if(mp.DM_s_E_Mail_ID__c!=Null)
                {
                    email_new.add(mp.DM_s_E_Mail_ID__c);
                     system.debug(i);
                }       
          }*/
              Messaging.SingleEmailMessage smail = new Messaging.SingleEmailMessage();  
              //List<Messaging.SingleEmailMessage> lMail = new List<Messaging.SingleEmailMessage>();        
             /*  List<String> setTo = new List<String>(email_new); 
                 integer num = setTo.size();               
                 system.debug(num);   */     
                smail.setToAddresses(new string[]{'abc@gmail.com'});
               
                smail.setReplyTo('xyz@gmail.com' (mailto:'xyz@gmail.com'));
                String messageBody = 'Dearr,';
                       messageBody +=' ';
                       messageBody +='One action.';
                       messageBody +=' PS:  This Automation.';                      
                smail.setHtmlBody(messageBody);
                              smail.setSenderDisplayname('mo');             
                smail.setSubject('CMMI Project(s)');          
                smail.setFileAttachments(new Messaging.EmailFileAttachment[]{Attachment});
                      
           Messaging.sendEmail(new Messaging.SingleEmailMessage[]{smail});
           
        }
           
       global void finish(Database.BatchableContext BC)
        {
           
        }

}




This is how the out put is..
This is the way my output after opening the attachment.

Any help appreciated .
Thanks in advance.
CongnizentCongnizent
Hi ,

 Do one thing . Create a new visual force page renderAs contentType="text/plain" . Put Page /00O61000002bwj9?csv=1  in it

and then include new page.

Please let me know if it not works or further help needed . 

Thanks