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);
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.