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
case commentscase comments 

setReplyTo not working correctly

I have code that redirects emails and sets the from address to the original email address. It will take the from address on the email and stick it into the reply to, so when the recipient gets it, it'll be as if it came from the original sender and they can reply back to them.  The code has been working for years but something happened and now it's not working.  Nothing has changed, and I can only suspect that it was due to a Salesforce release.  Does anybody know what I would need to change to get this to work again?  Thanks.

public with sharing class ForwardEmailController {
	public EmailMessage email {get;set;}
	public String forwardTo{get;set;}
	
	public ForwardEmailController(){
		String id = Apexpages.currentPage().getParameters().get('e');
		if (id != null){
			email =[Select id,ParentId,subject,FromName,FromAddress,TextBody,HtmlBody from EmailMessage where id =: id];
		}
	}
	
	public pageReference forward(){
		List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
		String delimiter = ';';
		if (forwardTo!= null && forwardTo!=''){
			if (forwardTo.indexOf(',')!= -1){
				delimiter = ',';
			}
			for (String s : forwardTo.split(delimiter)){
				Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	            mail.setToAddresses(new String[] {s});
	            mail.setSenderDisplayName(email.FromName);
	            mail.setSubject(email.subject);
	            mail.setReplyTo(email.FromAddress);
	            mail.setPlainTextBody(email.TextBody);
	            mail.setHtmlBody(email.HtmlBody);
	            emailList.add(mail);
			}
			Case c = [Select id,redirected__c from Case where id =: email.ParentId][0];
			c.Redirected__c = true;
			update c;
			Messaging.sendEmail(emailList);
		}
		return new PageReference('/' +email.ParentId);
	}
	
	
}

 

Best Answer chosen by Admin (Salesforce Developers) 
case commentscase comments

Turned out it was our email service that was stripping off the ReplyTo address.