You need to sign in to do that
Don't have an account?

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?
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
Hi
Please find attached a sample email sending code:-
Reference link:-
http://developer.force.com/cookbook/recipe/email-recipesa-complete-code-example
Hope this helps..
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.
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.
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">
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