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
Brijesh_85Brijesh_85 

Set From Address for Sending Email

Hi All,

 

I have developed one apex code to send an Email for customer portal i.e. For my site. What I want is the sending of email through my application , I want to use one common email id. So Suppose user fills one form , After clicking on submit one mail should send to the currently logged in user by the application. So Its like I want to set the from address which is common for all the mails throught the system.

 

I dont have any idea where I can search for all the methods to send the mail. I have tried with mail.setFromAddress('emai_id') .It gives me error by saying that ' No Method or Signature found '.

 

Please do the needful.

 

Regards,

Brijesh Thakur

Message Edited by Brijesh_85 on 06-10-2009 06:58 AM
yagnayagna

You must have instantiated a new email object using below syntax, 

 

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

 

 

Use below specified method to set To address

 

String[] toAddresses = new String[] {'user@acme.com'};


mail.setToAddresses(toAddresses);

 

Also, make sure you have variable 'mail' in scope when you are refering to it, you might have defined it in a different apex class

 

 

 

 

Brijesh_85Brijesh_85

Dear Yagna,

 

Thanks for the reply. The code you have mentioned, I am already using for  sending email.

 

I want to set the Sender's Email Address. I have reffered various docs for sending mail but nowhere I found the code to set the From Address. Is there any method there I can set the from address i.e. sender's email address.

 

Think like that, Suppose I have registered for one community and for every update I am getting one email notification from the Community Admin. The same thing I want.

 

Please help

 

Thanks in Advance,

Brijesh Thakur

hisrinuhisrinu

Hi Brijesh,

 

As of my knowledge You can set only the Name of the Sender using setSenderDisplayName.

The Sender Email will automatically set as the logged in user Email.

Hope this will help you.

Brijesh_85Brijesh_85

Thanks Hisrinu.

 

I explored the various docs and came to know that force.com has introduced OrgWideEmailAddress. Using that we can set the from address with Sender Display Name.

 

 

Do You have any idea .How to use setOrgWideEmailId() method. I think it solves the above issue.

Please paste the code if you know about  OrgWideEmailAddress.

 

Regards,

Brijesh Thakur

yagnayagna

As you said we can use 'setOrgWideEmailAddressId(Id)' method, you have to use this method instead of 'setSenderDisplayName()'. 

 

Id value is OrgWideEmailAddres. This Id is obtained after creating a new 'Organisation-WideEmail Addresses' in Email Administration of Administration Setup section.

 

I think this feature is available in Orgs which have Summer09  release enabled.

 

For more info you can check the latest release of  Force.com Apex Code Developer's Guide (Apex Language Reference 16.0)

 

And 'Force.com Web Services API Developer's Guide'

Rasmus MenckeRasmus Mencke

OrgWide from addresses are available in the summer release and can be used in the SingleEmail API.

 

You will need to setup and verify the address before you can start using it, and make sure that the user calling it has access to it.

Brijesh_85Brijesh_85

Hi All,

 

The Problem is solved. I was getting the error saying SingleMail.setOrgWideEmailAddressId(ID) Method does not exist.

 

The error because of I was using API Version 14.0 for  Apex Class rather than 16.0. 

 

Solution :- Opened the Metadata Xml file of that Apex Class. Change the API version from 14.0 to 16.0. And the problem is solved. 

 

mail.setOrgWideEmailAddressId(Id);

 

ID : You can get from the Administrative Setup --> Email Administration --> OrganizationWideEmailAddresses.

 

First Create New. and then Verify by going to the Given Email Id. And then Click on Edit on URL you can see 15 characters long ID. Use it to set in Method  as a parameter.

 

Regards,

Brijesh Thakur

shimritshimrit

hi Brijesh_85,

 

I had the same issue and your solution worked perfectly for me too.

 

Thanks for posting it.

 

Regards, shimrit

Ajay_SFDCAjay_SFDC
Hi All ,

I got this solution from one of the forum . So just posting answer from there : - 

To do this you must first set up a dedicated email address by navigating to Setup -> Administration Setup -> Email Administration -> Organization-Wide Addresses menu. Once you have created an org-wide address (note that Salesforce will require you to confirm the address prior to using it, so if you're going to be sending things from a junk address it would be wise to set up a catch-all mailbox so you receive the confirmation email), grab the Id from the URL and use the setOrgWideEmailAddressId(Id) method on your instance of Messaging.SingleEmailMessage.

If you want to avoid hard-coding an Id, after creating your Org-Wide Address you can query them:

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);
}


Thanks ,
 Ajay