+ Start a Discussion
Ankit Singh 6Ankit Singh 6 

Using fromAddress while sending mail through salesforce

Hi All

We have integrated salesforce using SOAP in C#. We are trying to send mail using salesforce templates through the code. But we are unable to provide fromAddress in the mail. Below is the code for the same : 

SingleEmailMessage message = new SingleEmailMessage();
                message.emailPriority = EmailPriority.High;
                message.saveAsActivity = false;
                message.useSignature = true;
                message.templateId = templateId;
                message.targetObjectId = CrmId;
                SingleEmailMessage[] messages = { message };
                SendEmailResult[] results = _sf.sendEmail(messages);

Please let us know a way to add fromAddress while sending mail as we are unable to find any option for that.

nagachandra knnagachandra kn

You have to configure the Organization-Wide Email address first. For that Please go to Email admistration and create a create a org wide email address. You can pass in the ID to an OrgWideEmailAddress record when calling sendEmail() for a SingleEmailMessage. For the reciever it will show this id as the sender id.

go through this link
Head In CloudHead In Cloud
             List<string> sendTo= new list<string>();
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            EmailTemplate et=[Select id,name,HtmlValue,Subject,body from EmailTemplate where DeveloperName =              'STILL_WAITING_FOR_REPLY_TO_Q_EMAIL'];
            String str =et.HtmlValue;
            Messaging.SendEmailresult [] emResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Ankit Singh 6Ankit Singh 6
The organization-wide email address will help if I have to send mails from one salesforce account. But here my case many clients are using our wepapp through salesforce and we need to go and tell each client to do so. We need to get it done through code so that it can handle all clients.Please suggest.

@Head In Cloud
We need to provide fromAddress while sending mail. We are able to get toAddress and EmailTemplate
Ruwantha  LankathilakaRuwantha Lankathilaka

Insufficiant information.  But I' ll try to answer with the little info have. 

Create an Organization-Wide Addresses
Setup -> Administration Setup -> Email Administration -> Organization-Wide Addresses menu (Salesforce will require you to confirm the address prior to using it,)

Then ,

OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'doNotReply@blahblah.com']; Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); if ( owea.size() > 0 ) { message.setOrgWideEmailAddressId(owea.get(0).Id); }

nagachandra knnagachandra kn
@Ankit when you send the mail , by default from address is set to the id of the user who is sending the mail. As you said you have many clients using the app. you can check by sending a sample mail and verify the from address.
Ankit Singh 6Ankit Singh 6
Yes, we have already tested it and by default fromAddress is set to id of user who is sending the mail But here we want a provision to set fromAddress to the id of some other user of same company