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
Kannan.RamKannan.Ram 

Sender Address in Apex Email

Hi,

 

Is it possible to dynamically set the Sender Address on an email when using a SalesForce Email Template in Apex?

 

Thanks,

Kannan

blombardisblombardis

Hi Kannan,

 

Here is a snippet that may help you:

 

 

// Create a new single email message object  
    
// that will send out a single email to the addresses in the To, CC & BCC list.  
    
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

// Strings to hold the email addresses to which you are sending the email.  
    
String[] toAddresses = new String[] {'user@acme.com'}; 
String[] ccAddresses = new String[] {'smith@gmail.com'};
  

// Assign the addresses for the To and CC lists to the mail object.  
    
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);

// Specify the address used when the recipients reply to the email.   
    
mail.setReplyTo('support@acme.com');

// Specify the name used as the display name.  
    
mail.setSenderDisplayName('Salesforce Support');

// Specify the subject line for your email address.  
    
mail.setSubject('New Case Created : ' + case.Id);

// Set to True if you want to BCC yourself on the email.  
    
mail.setBccSender(false);

// Optionally append the salesforce.com email signature to the email.  
    
// The email address of the user executing the Apex Code will be used.  
    
mail.setUseSignature(false);

// Specify the text content of the email.  
    
mail.setPlainTextBody('Your Case: ' + case.Id +' has been created');

mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created<p>'+
     ' View case <a href=https://na1.salesforce.com/'+case.Id+'>click here</a>');

// Send the email you have created.  
    
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

 

This example uses more mail methods.

 

To read more:

http://www.salesforce.com/us/developer/docs/apexcode/index.htm 

and search for mail.

 

Hope it helps,

Bruno

 

 

Kannan.RamKannan.Ram

Thanks Bruno. This is my code snippet :-

 

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId('00XR0000000QQzP');
mail.setTargetObjectId(leadId);

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

 

mail.setTemplateId(templateId);

 

mail.setTargetObjectId(leadId);

 

//mail.setOrgWideEmailAddressId(mailId);

 

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

I get an error saying that the setOrgWideEmailAddressId method is incorrect or does not exist. I've tried all possible combinations but it does not accept anything.

 

Any idea how I can make this work?


Thanks,

Kannan

narsavagepnarsavagep

I'm getting the same error... did you ever find a solution to this?

narsavagepnarsavagep

I found the reason: The APEX class that I was modifying was created a while ago and had an APEX version number of 14... but that was prior to "organizational emails" -- I changed the APEX version of the code (in the MetaData) to 19, and now it works OK.

 

:Paul

Kannan.RamKannan.Ram

Yes, I think the functionality was a Summer '09 release. Any Metadata version above 17 should work.

Chidanand MChidanand M
YES. U can set it using OWD.