How to send an email to someone outside of CRM organization

Sending an email to someone outside the CRM organization seems like a problem most developers are struggling with. On this post we will explain how to send an email outside CRM organization. Not only that but also build a method that will send the email .

In order for us to send the email we need to be able to write the receivers in the “To” field. To do that enable “Allow messages with unresolved email recipients to be sent” setting. Here’s the path to do that Settings>Administration>System Settings>Email:

After enabling it you’ll be able to write receiver’s email in the “To” field.

As for the method here’s the code that we will use:

public static void SendEmail(string sendTo)
{
// Get a system user to send the email (From: field)
WhoAmIRequest systemUserRequest = new WhoAmIRequest();
WhoAmIResponse systemUserResponse = (WhoAmIResponse)_service.Execute(systemUserRequest);
Guid userId = systemUserResponse.UserId;
// We will send the email from this current user
Entity fromActivityParty = new Entity(“activityparty”);
// We will set where we want to send the email
var recipients = new EntityCollection();
recipients.Entities.Add(new Entity(“activityparty”)
{
[“addressused”] = sendTo
});
fromActivityParty[“partyid”] = new EntityReference(“systemuser”, userId);
// composing the email
Entity email = new Entity(“email”);
email[“from”] = new Entity[] { fromActivityParty };
email[“to”] = recipients;
email[“cc”] = recipients;
email[“subject”] = “Subject Goes Here”;
email[“description”] = “Content of the email goes here”;
email[“directioncode”] = true;
Guid emailId = _service.Create(email);
// Use the SendEmail message to send an e-mail message.
SendEmailRequest sendEmailRequest = new SendEmailRequest
{
EmailId = emailId,
TrackingToken = “”,
IssueSend = true
};
SendEmailResponse sendEmailresp = (SendEmailResponse)_service.Execute(sendEmailRequest);
Console.WriteLine(“Email sent”);
Console.ReadLine();
}