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
alex51atlasalex51atlas 

Send Email from Template, via apex, using a generic email address

Does anyone know how to send an email, from a template, via apex code, as someone other than the currently logged in user? Maybe using a company org-wide email?

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

Hi,


You can send an email through apex code and set the from address and from name in Organization wide address section in administration set up and use the addresses in apex code bu using below code snippet.

OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
EmailTemplate templateId = [Select id from EmailTemplate where name = 'Waitlisted opportunity now open'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id);
mail.setSaveAsActivity(false);
mail.setOrgWideEmailAddressId(owa.id);
allmsg.add(mail);
Messaging.sendEmail(allmsg,false);

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

All Answers

IspitaIspita

Hi 

Please find attached a sample email sending code:-

 

emailHelper.sendEmail(myContact[0].id, newCandidate[0].id);
 

  // In a separate class so that it can be used elsewhere

Global class emailHelper {

 

public static void sendEmail(ID recipient, ID candidate) {

 

  //New instance of a single email message

 Messaging.SingleEmailMessage mail =

            new Messaging.SingleEmailMessage();

 

// Who you are sending the email to

   mail.setTargetObjectId(recipient);

 

   // The email template ID used for the email

   mail.setTemplateId('00X30000001GLJj');

           

   mail.setWhatId(candidate);   

   mail.setBccSender(false);

   mail.setUseSignature(false);

   mail.setReplyTo('recruiting@acme.com');

   mail.setSenderDisplayName('HR Recruiting');

   mail.setSaveAsActivity(false); 

  

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

 

    } 

}

 

Reference link:-

http://developer.force.com/cookbook/recipe/email-recipesa-complete-code-example

 Hope this helps..

Ispita_NavatarIspita_Navatar

Hi,


You can send an email through apex code and set the from address and from name in Organization wide address section in administration set up and use the addresses in apex code bu using below code snippet.

OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
EmailTemplate templateId = [Select id from EmailTemplate where name = 'Waitlisted opportunity now open'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id);
mail.setSaveAsActivity(false);
mail.setOrgWideEmailAddressId(owa.id);
allmsg.add(mail);
Messaging.sendEmail(allmsg,false);

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

This was selected as the best answer
alex51atlasalex51atlas

Thank you Ispita, it worked. I guess I was looking at old documentation or I just overlooked it but I had not seen the setOrgWideEmailAddressID function in the API documentation.

Vaishali SinghVaishali Singh
This code can be used anywhere in the trigger, apex scheduler, or normal apex class.
        OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
        List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
        EmailTemplate emailSickEmpTemplate = [Select Id from EmailTemplate where developername = 'Email_with_list_of_sick_employees'];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTemplateID(emailSickEmpTemplate.Id); 
        mail.setTargetObjectId(Userinfo.getUserId());
        mail.setSaveAsActivity(false);
        mail.setOrgWideEmailAddressId(owa.id);
        allmsg.add(mail);
        Messaging.sendEmail(allmsg,false);

That's it, nothing less nothing more. You can definitely add optional methods but be wary of it. Also in the email template you may or may not have recepient type or any other addition parameters. It can be as simple as <messaging:emailTemplate subject="xyz">
ShivamAgNot3ShivamAgNot3
Does salesforce support record field in attach in email template when we use it from apex
jaya sai prasadjaya sai prasad
Hi,

If you are looking for an  app for how to send an email, from a template, via apex code, as someone other than the currently logged in user then Massmailer is perfect for you.

MassMailer Make Salesforce Email Limitations a Thing of the Past.
You can track your emails ,save and edit email templates on Salesforce when using MassMailer.
 By monitoring the performance of your email templates, you can optimize your past templates to improve results 
and avoid creating the same template more than once. Save time and get better results with MassMailer.

You can look the developer guide here - Developer Guide

You can try this app by installing from appexchange -  Massmailer app

You can learn more details about the product on this website massmailer.io