+ Start a Discussion
Anand@SAASAnand@SAAS 

Mail.setReplyTo not working???


Tried the following in "Execute anonymous":

 

Messaging.SingleEmailMessage	mail = new Messaging.SingleEmailMessage();
mail.setReplyTo('someemail@gmail.com');
mail.setSenderDisplayName('Sender Name');
mail.saveAsActivity=false;
mail.setToaddresses(new List<String>{'recipient@gmail.com'});
mail.setTemplateId('00XQ0000000QH2B');
mail.setTargetObjectId('00580000001gXtK');
mail.setUseSignature(false);
Messaging.sendEmail(new List<Messaging.Email>{mail});

 

The email is received successfuly by when I "hit" reply it show s the email address of the "Admin" user that I executed the above code with. Should it not be "someemail@gmail.com"?

 

Best Answer chosen by Admin (Salesforce Developers) 
yudhvirmoryudhvirmor

Please check reply to email mentioend in the template you are refering in your code.

All Answers

yudhvirmoryudhvirmor

Please check reply to email mentioend in the template you are refering in your code.

This was selected as the best answer
Anand@SAASAnand@SAAS

That did do the trick. But that makes "setReplyTo" useless when using templates. 

yudhvirmoryudhvirmor

we need to decide where we want to use replyto.. or write that template also as VF page and it may meet ur requirement.

 

vijaymindvijaymind

Hi I tried to use without template too , but its still not working , when I hit the reply it goes to admin email Id :

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'myemail@gmail.com'});
mail.setReplyTo('test@gmail.com');
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Batch Process Completed');
mail.setPlainTextBody('Batch Process has completed');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

Any Idea ?

sagarvp9sagarvp9
Same here, I tried without template too, It did NOT work. But there is an easy workaround for this, as explained below
  • Navigate to Setup>> Organization-Wide Email Addresses and create a record with Email address which you would like to use it as ReplyTo for Email Notification.
  • In the Apex code, query this record and set its ID to setOrgWideEmailAddressId attribute as shown below.
List<OrgWideEmailAddress> orgWideEmailLst = [select id, address from OrgWideEmailAddress where displayname = :'No Reply' limit 1];
String orgWideEmailId = orgWideEmailLst.size()>0?orgWideEmailLst[0].id:'no-reply@email.com';
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setOrgWideEmailAddressId(orgWideEmailId);  
mail.setSubject('My Email Subject');
mail.setHtmlBody('My Email Content');
List<String> sendTo = new List<String>();
sendTo.add('sampleTo@email.com');                           
mail.setToAddresses(sendTo);
mails.add(mail);
Messaging.sendEmail(mails);
Praveen Krishna 1Praveen Krishna 1
How to set "replyto" in email template?