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

Visualforce Email Template in apex code.

I have created a visualforce email template which is related to a custom object and recipient type is user.  I am  using this template in apex code to send the email but it does not send any email while the same email template shows correct merge field etc in case of preview.


i am using the following code to send the email


Messaging.SingleEmailMessage mail = mail.setTemplateId(templates[0].Id);


//currently loggoed in user's Id, getting value from UserInfo.getUserId()


mail.setWhatId(record.Id); //a custom object record id


mail.setReplyTo(senderEmail); //its valid email id




new Messaging.SingleEmailMessage[]{mail});

When i use debug log, it show a mail in email queue still its not sending any email.

so could any one tell me that where i am commiting a mistake??


Is it complete code?


mail.setTemplateID seems to be missing.




Actually it was there in the original code but while posting the code here. i have commited some mistake.


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



meanwhile i have tried to send email by workflow using the same Email Template and it was working so i dont understand that why it is happening??

Juan SpagnoliJuan Spagnoli

Why are you doing this?



 setTargetObjectId is a void method. How were you able to compile that code??


Did you find any solution?

I am stuck in the same scenario