-1

I have written the apex code to sendmail for my project. When it is getting triggered from apex class. It is getting trigger to two email ids. One is my user email id and other is the one which I am adding in toEmail list. please find below code for same:

Public void sendEmail(){  
        try{
            system.debug('inside 3nd function');
        Booking__c booking =[Select id,CC_Email_Address__c,Account__c,OwnerId,Order_Contact__c from Booking__c where id =: Bookingid];        
        List<Attachment> sendEmailAttlist= [SELECT Id, Name, ContentType, Body FROM Attachment WHERE parentid =: ApexPages.currentPage().getParameters().get('Id')];
        Integer TotalnumofAtts=[SELECT count() FROM attachment WHERE parentid=: ApexPages.currentPage().getParameters().get('Id')];

        list<attachment> attachlst = new list<attachment>();
            for(attachment a: sendEmailAttlist){
                attachlst.add(a);
            }
        OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'customer.service.domestic@tnt.co.uk'];
        EmailTemplate etemplate1= [SELECT Name,id,body,Subject,developername FROM EmailTemplate where developername ='Attachment_Template' limit 1];   
       Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(etemplate1.Id, userinfo.getUserId(), booking.Id); 
        email.setOrgWideEmailAddressId(owea.get(0).Id);
        List<Messaging.Emailfileattachment> efaList = new list<Messaging.Emailfileattachment>();
       // if(TotalnumofAtts > 0){
            for (Integer i = 0; i < TotalnumofAtts; i++){
                    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();

                    efa.setFileName(attachlst[i].Name);                     
                    efa.setBody(attachlst[i].Body);
                    efa.setContentType(attachlst[i].ContentType);
                    efaList.add(efa);
                } 
         ToEmail= booking.CC_Email_Address__c;

         email.setToAddresses( new String[] {ToEmail} );
            email.setFileAttachments(efaList);
            //email.setTargetObjectId('0033N0000035fwn');
            email.setTargetObjectId(userinfo.getUserId());
            //email.setTreatTargetObjectAsRecipient(false);
            email.setSaveAsActivity(false);
            email.setUseSignature(false);
            if(Test.isRunningTest())
            {
            email.setPlainTextBody( 'This is the Test email sent from Booking attachment' );
                 }
            else{
                 //email.setSenderDisplayName('TNT Support');
            EmailTemplate etemplate= [SELECT Name,id,body,Subject,developername FROM EmailTemplate where developername ='Attachment_Template' limit 1];            


            email.setTemplateId(etemplate.Id);
            }


       Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); 

   // }         
  }
        catch(Exception e){}
    }  

Kindly help what could be the reason for this. I tried everything from my end and still on the wrong side.

Thanks in advance.............

  • Please edit your post to include the relevant portions of the code. – David Reed Feb 7 at 15:23
  • Thanks Actually i got this after posting question on this. Thanks for help :) – Anoop Feb 10 at 6:24
0

Apex is doing exactly what you asked it to do.

     email.setToAddresses( new String[] {ToEmail} );
        email.setFileAttachments(efaList);
        //email.setTargetObjectId('0033N0000035fwn');
        email.setTargetObjectId(userinfo.getUserId());
        //email.setTreatTargetObjectAsRecipient(false);

From the documentation for setTargetObjectId():

The ID of the contact, lead, or user to which the email will be sent

and setTreatTargetObjectAsRecipient():

If set to true, the targetObjectId (a contact, lead, or user) is the recipient of the email.

You're receiving the email because you asked Apex to send you the email.

| improve this answer | |

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