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
Mohammed MohiddinMohammed Mohiddin 

Setting From Address while sending email

Hello Everyone,

How do we set From Address while sending email using messaging.SingleEmailMessage. I have got a solution which uses setOrgWideEmailAddressId to set from address,
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'doNotReply@blahblah.com']; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); if ( owea.size() > 0 ) { mail.setOrgWideEmailAddressId(owea.get(0).Id); }


But I want to query the record ( say the contact who logged in ) and set that as From Address.

Can Anyone help me out with this?
Raj VakatiRaj Vakati
I think you can able to do it like this 

 
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'doNotReply@blahblah.com']; 


Contact c = new COntact() ;
c.LastName = UserInfo.getLastName() ;
// add all fields

insert c ; 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 if ( owea.size() > 0 ) {
 mail.setOrgWideEmailAddressId(owea.get(0).Id);
 }
 
 // at the end 
 
 delete c ;

 
Mohammed MohiddinMohammed Mohiddin
I have got a solution for it. I will Query the logged in user and assign it to Display Name. That is what I actually wanted.
Raj VakatiRaj Vakati
The problem is if you query the contains against to the user if contact is not preset you will get list has no rows to assign object.