Sending email from the salesforce endpoint with .Net application

|
| By Harleen Kaur

Emailing through Salesforce in the third-party applications can be achieved through Salesforce REST endpoint services/data/v61.0/actions/standard/emailSimple
Below is the sample code in .Net for sending simple plain text email for emailing through Salesforce

public void SendEmailUsingSalesforce()
{
Dictionary mappings = new Dictionary();
var emailData = new
{
inputs = new[] {
new
{
emailAddressesArray = , // Email recipient(s)
emailSubject =, // Email subject
emailBody = , // Plain text content
senderType="OrgWideEmailAddress",
senderAddress = , // Display name of the sender
saveAsActivity = true // Save as activity record in Salesforce
}
}
};
//To send a comma separated list of the recipient’s email addresses, emailAddresses field //can be used instead of emailAddressesArray
string jsonBody = JsonConvert.SerializeObject(emailData);
string requestUrl = "{instance-url}/services/data/v61.0//actions/standard/emailSimple";
string result = Task.Run(async () => await PostSalesforceAsync(requestUrl, _salesforecToken.access_token, jsonBody)).Result;
}

public static async Task PostSalesforceAsync(string requestUrl, string accessToken, string jsonBody)
{
string responseString = string.Empty;
try
{
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
requestUrl))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer",
accessToken);
request.Content = new StringContent(jsonBody, Encoding.UTF8,
"application/json");
HttpResponseMessage response = await _httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
responseString = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(responseString))
{
responseString = "success";
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine($"Context: {requestUrl}");
}
return responseString;
}

To send the rich text, set the property sendRichBody to true in Email Json

Leave a Reply

Your email address will not be published. Required fields are marked *