+ Start a Discussion
sivaextsivaext 

stop sending email to setTargetobjectId in single email message

Hi , 

I am writing single email message to code to send emails. I am dynamically constructing toAddresses to send emails. The code is working fine and sending email to toAddresses and setTargetobjectid. 

I don't want to send email to setTargetobjectId. is there any way to stop sending email to setTargetobjectid?

Thanks
Siva.
Vinit_KumarVinit_Kumar
Can you share the code which is being used to send emails ??

setTargetObjectId is mandatory if you are using setTemplateId in your code.
sivaextsivaext
Hi vinit, 

 EmailTemplate Emailtemplate=[SELECT Description, Body, Id, Name, BrandTemplateId, Markup, NamespacePrefix, TemplateStyle, Subject,
                                                TemplateType FROM EmailTemplate where name=:'Brigade CPprofile Email RejectInfo'];          
                           system.debug('$$$$$$$$$$$$$$$$'+Emailtemplate.id);           
                           Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                           String[] toAddresses = new String[] {usr.email};
                           string messageBody ;
                           message.setTemplateId(Emailtemplate.id);          
                          // message.setTemplateId('00Xf0000000QJCi');
                           message.setTargetObjectId(usr.ContactId);
                           message.setWhatId(CPprofileObj.Id);
                           message.saveAsActivity=false;
                           message.setToAddresses(toAddresses);
                           Messaging.sendEmail(new Messaging.SingleEmailMessage[] { message });


Email sending perfectly but email is sending to both targetobjectid and toAddresses etc.

I don't want to send email to targetobjectid.

Thanks & Regards
Siva.
Deb FloresDeb Flores
Hi Siva,

Were you able to find a solution to this?  I'm having the same problem.  I'd like to send an internal email as part of an exception process; I want to use a template and merge certain data from the contact record I'm handling, but I definitely don't want to send the email to the contact. 

Any advice you can offer would be greatly appreciated!

Deb
jyoti_Singhjyoti_Singh
Hi Siva ,Deb

You can remove toAddresses() from the block it will solve the issue , As it is not a required property for SingleEmailMessages.
So once toAddresses is removed , settargetobjectid() will take care of the email being sent.

Thanks
Jyoti
HareHare
Hi 

you can block setTargetObject by using setTreatTargetObjectAsRecipient(treatAsRecipient)  boolean method
 message.setTreatTargetObjectAsRecipient(false);
Yogesh MiyaniYogesh Miyani
Hi sfdcsfdc,

Thanks, i was also facing same problem but using setTreatTargetObjectAsRecipient(false); it solve
 
 
CillaCilla
@sfdcsfdcThank you, thank you!!

Also, Siva, you should mark this as solved :)