0

I have written a class which sends an email to a user using Email Templates, but I am not able to change the "From Address" of the email. For now it is being sent from my user's address and name. How can I change the "From Address"?

My current code:

EmailTemplate templateId = [Select id from EmailTemplate where name = 'User warning mail for 30 days'];     

List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();

for(User usr : listusersfor30days){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
    mail.setTemplateID(templateId.Id);  
    mail.setTargetObjectId(usr.Id); 
    mail.setSaveAsActivity(false); 
    allmsg.add(mail);             
}

Messaging.sendEmail(allmsg);

closed as unclear what you're asking by Mohith Shrivastava, Dave Humm, Lance Shi, Ratan Paul, Sebastian Kessel Jan 9 '17 at 6:12

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

You will have to create Org Wide email address and verify it. Then you can use this Org Wide Email address in you code.

For setting an email address and Name, goto Setup > Email Administration > Organisation wide address. Choose a name and email and allow it for other profiles. Link

 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
 mail.setTemplateID(templateId.Id);  
 mail.setTargetObjectId(usr.Id); 
 mail.setSaveAsActivity(false); 
 mail.setOrgWideEmailAddressId(18 Digit Id of Email Address);
 allmsg.add(mail);
  • Is there another way that I can send "From address" through code. Like, mail.fromaddress(); ? – RAHUL SRIVASTAVA Dec 28 '16 at 14:23

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