Title: How can I define From Email In this Apex Email Service Code
Tags: apex;apex-email-service
Question: In this what I will for Set a Default From Email Address.

``` Messaging.SingleEmailMessage EmailMessage = new Messaging.SingleEmailMessage();
 EmailMessage.setTargetObjectId(opp.Applicant_Student_Record__c);
 EmailMessage.setTemplateId(et.Id);
 EmailMessage.setWhatId(opp.Id);
 EmailMessage.setSaveAsActivity(false);
 EmailMessage.setReplyTo('tdavis@example.net');
 EmailMessage.setSenderDisplayName('Development');
 EmailMessage.setFileAttachments(Attachment);
 emails.add(EmailMessage);  
```


Here is the accepted answer: To do this you must first set up a dedicated email address by navigating to Setup -> Administration Setup -> Email Administration -> Organization-Wide Addresses menu.

Setup the address(say :tdavis@example.net) and need to verify for the use.



Add the below code :

```OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'tdavis@example.net'];
Messaging.SingleEmailMessage EmailMessage = new Messaging.SingleEmailMessage(); 
if ( owea.size() &gt; 0 ) {
    EmailMessage.setOrgWideEmailAddressId(owea.get(0).Id);
}
EmailMessage.setTargetObjectId(opp.Applicant_Student_Record__c); 
EmailMessage.setTemplateId(et.Id); 
EmailMessage.setWhatId(opp.Id); 
EmailMessage.setSaveAsActivity(false); 
EmailMessage.setReplyTo('tdavis@example.net'); 
EmailMessage.setSenderDisplayName('Development'); 
EmailMessage.setFileAttachments(Attachment);
emails.add(EmailMessage);
```

Here setting up the email address at "Email Administration" will help you to set the desired mail address from whom you want to send the mail( form which you are also querying from "OrgWideEmailAddress" to get the particular one if in case you have multiple address setup).



{'source': 'StackExchangeDataset', 'subset': 'salesforce.stackexchange.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:07/06/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<apex><apex-email-service>', 'Title': 'How can I define From Email In this Apex Email Service Code', 'ViewCount': 92, 'id': 182037, 'user_AboutMe': '', 'user_AccountId': 11258684.0, 'user_DisplayName': 'Maddy', 'user_Reputation': 1.0, 'user_Views': 4.0, 'user_id': 46815.0}}