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
JitendraJitendra 

How to set the From field in Email

Hi,

I am sending the automaic Email to the users using Apex. I am able to send different emails to different user.
But i am not able to set the From field in the Apex Code.
Email is sent by the Trigger and trigger is activated by Workflow Rule.
It takes the name of last Logged in User by default to set the From field of the email.

Is there any Way to set the Name of default Workflow user or any other User?

Please suggest any solution or work around if you know.


Best Answer chosen by Admin (Salesforce Developers) 
JitendraJitendra

After, lots of trial i found that we cannot set the From field of email API.

SAlesforce automatically sets the form field by the user who was last logged in. 

All Answers

JitendraJitendra

After, lots of trial i found that we cannot set the From field of email API.

SAlesforce automatically sets the form field by the user who was last logged in. 

This was selected as the best answer
Joe_IpsenJoe_Ipsen
=====================================
https://na4.salesforce.com/help/doc/user_ed.jsp

How is the workflow alert "From:" addressed defined?

The "From:" address field, on a workflow alert email, uses the email address of the 'last modified by' user whose record edit/creation satisfied the associated workflow rule criteria.

This is also the case for a sending user merge field {!User_Username} in an email template used by a workflow rule. Where the {!User_Username} is defined by the user creating or editing a record, causing a workflow rule to trigger.
=====================================

Sadly, this appears to be working as designed and there does not appear to be any way to customize the from address . . . using Workflow, anyway.
 
 
But as you mentioned Apex, I believe there may be a way to get around this.  I have not tested this, but have found the following sections which may work to overcome this problem.

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

// The ID of the template to be merged to create this email. You must specify a value for setTemplateId, setHtmlBody, setPlainTextBody, or both setHtmlBody and setPlainTextBody.
mail.setTemplateId(ID) //need to verify syntax

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

// Specify the name used as the display name.  Optional. The name that appears on the From line of the email.
mail.setSenderDisplayName('Your Chosen Address Pretty Name');

Some useful notes:

-The email address of the user calling the sendEmail method is inserted in the From Address field of the email header. All email that is returned, bounced, or received out-of-office replies goes to the user calling the method.
-All mass email and single email messages sent with the sendEmail method count against the daily mass mail limit of the sending organization. When the daily mass mail limit for the organization is reached, all calls to the sendEmail method are rejected, and the user receives a MASS_MAIL_LIMIT_EXCEEDED error code. However, single email messages sent through the application are still allowed.
 

Joe_IpsenJoe_Ipsen

Here's a good post on this topic with a good analysis of the shortcomings of the Apex solution:

 

http://community.salesforce.com/sforce/board/message?board.id=general_development&message.id=29110

Always ThinkinAlways Thinkin

You can now set the from displayed name and email address by using Organization Wide Emails with the  setOrgWideEmailAddressId method. Dynamically choosing an Org. Wide Email is a little trickier though not to mention it requires setting up the necessary ones in the first place.

 

For my use case I had to send emails out to Contacts and set the from address to be the Contact's Owner. Since User and Org Wide Address are not related tables, I created a field on the User record and copy that User's Org Wide Address' ID to the field.

 

Note that this only works for Single Email Messages 

sandeep@Salesforcesandeep@Salesforce

Very nice help I got from this thread