Unresolved emails are useful in that you can send an email to someone without having them setup as a system record (contact, account, lead, queue, user, ect). Unresolved email addresses are turned off by default. They should be used with caution. If you send an email to an unresolved address it will not track the email to that lead/contact since one does not exist. It also does not leverage the email "Do not allow" flags to allow people to opt out of emails. With those considerations, there are still cases where it is very useful to send an email to someone without having to create a contact record.
I will show two example of sending emails to unresolved recipients. The first will be sent from the email form using JavaScript. The second will be done in a plugin. We will add two unresolved email addresses as CC addresses on emails sent from a contact record.
Before we begin. You must flip the setting to allow unresolved recipients. Go to Settings --> Administration --> System Settings --> Email Tab. Set Allow messages with unresolved e-mail recipients to be sent to yes.
Second, we will add a couple of fields to the contact for secondary contacts email addresses. These fields will contain our unresolved email addresses.
JavaScript Implementation
When the user clicks "Send Email" from the contact form we want the CC field to default with the two unresolved email addresses. To do this we add the following Jscript to the onload of the email form. The code will check to see if the regarding object is of type contact since this form is used for all emails. Then it adds each email address to an email object that follows the activityparty schema for unresolved email addresses. It has a type of 9206 and the email is set in the data property.
if (crmForm.FormType == 1 &&
crmForm.all.regardingobjectid.DataValue != null &&
crmForm.all.regardingobjectid.DataValue[0].typename == "contact" &&
window.opener != null &&
window.opener.document != null) {
var ar = new Array();
var emailObj = new Object();
if (window.opener.document.crmForm.all.new_secondarycontactemail1 != null &&
window.opener.document.crmForm.all.new_secondarycontactemail1.DataValue != null) {
emailObj = new Object();
emailObj['type'] = '9206';
emailObj['category'] = '3';
emailObj['data'] = window.opener.document.crmForm.all.new_secondarycontactemail1.DataValue;
emailObj['name'] = window.opener.document.crmForm.all.new_secondarycontactemail1.DataValue;
ar.push(emailObj);
}
if (window.opener.document.crmForm.all.new_secondarycontactemail2 != null &&
window.opener.document.crmForm.all.new_secondarycontactemail2.DataValue != null) {
emailObj = new Object();
emailObj['type'] = '9206';
emailObj['category'] = '3';
emailObj['data'] = window.opener.document.crmForm.all.new_secondarycontactemail2.DataValue;
emailObj['name'] = window.opener.document.crmForm.all.new_secondarycontactemail2.DataValue;
ar.push(emailObj);
}
crmForm.all.cc.DataValue = ar;
}
Lastly, publish the customization. When you click "Send Email" from the contact form your addresses will be added to the CC.
Plugin Implementation
The same functionality can be added using a plugin. The benefits of using a plugin versus a client side implementation is that you can implement consistent functionality regardless of whether the email send is triggered from a client portal, another plugin, or through the CRM UI. The JavaScript implemenation is useful in that the user can see who the email is being sent to before they click Send.
Install the plugin using the registration tool as a pre-create step for email save. This will add the CC on the initial save of the email. It is triggered regardless of whether the user clicks save or send from the email form. The code also includes duplicate checking logic. So, if the user adds the email address to the CC from the form it will not duplicate the same address in the plugin.
using System.Web;
using System.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using System.Reflection;
using System.Web.Services.Protocols;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using Microsoft.Crm.Sdk.Metadata;
using Inetium.CrmPlugins;
using System.Collections;
namespace Inetium.CrmPlugins.Plugins
{
public class CaseUnresolvedEmailPlugin : IPlugin
{
public void Execute(IPluginExecutionContext context)
{
ICrmService service = null;
try
{
if (context.InputParameters.Properties.Contains("Target") &&
context.InputParameters.Properties["Target"] is DynamicEntity)
{
DynamicEntity entityInput = context.InputParameters.Properties["Target"] as DynamicEntity;
if (entityInput.Properties.Contains("regardingobjectid"))
{
Lookup regardingObjectLookup = entityInput.Properties["regardingobjectid"] as Lookup;
service = context.CreateCrmService(true);
// Only using this for contacts
if (regardingObjectLookup.type == EntityName.contact.ToString())
{
// Pull parent contact - using helper method
DynamicEntity ctn = CRMUtilities.RetrieveById(service, EntityName.contact.ToString(), "contactid", regardingObjectLookup.Value, new ColumnSet(new string[]{"new_secondarycontactemail1", "new_secondarycontactemail2"}));
if (!ctn.Properties.Contains("new_secondarycontactemail1") && !ctn.Properties.Contains("new_secondarycontactemail2"))
return;
string email1 = ctn.Properties.Contains("new_secondarycontactemail1") ? ctn.Properties["new_secondarycontactemail1"].ToString() : "";
string email2 = ctn.Properties.Contains("new_secondarycontactemail2") ? ctn.Properties["new_secondarycontactemail2"].ToString() : "";
DynamicEntity[] partyArrayExisting = entityInput.Properties.Contains("cc") ? entityInput.Properties["cc"] as DynamicEntity[] : new DynamicEntity[] { };
List<DynamicEntity> partyList = new List<DynamicEntity>(partyArrayExisting);
if (email1 != "" && !IsEmailAlreadyInCC(email1, partyList))
{
DynamicEntity party = new DynamicEntity();
party.Name = EntityName.activityparty.ToString();
party.Properties["addressused"] = email1;
partyList.Add(party);
}
if (email2 != "" && !IsEmailAlreadyInCC(email2, partyList))
{
DynamicEntity party = new DynamicEntity();
party.Name = EntityName.activityparty.ToString();
party.Properties["addressused"] = email2;
partyList.Add(party);
}
// Generate CC's list by adding in those from contacts
if (partyList.Count > 0)
entityInput.Properties["cc"] = partyList.ToArray();
}
}
}
}
catch (SoapException se)
{
throw new Exception(se.Detail.InnerText);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (service != null)
{
service.Dispose();
service = null;
}
}
}
private bool IsEmailAlreadyInCC(string email, List<DynamicEntity> activityPartyList)
{
bool bAlreadyExists = false;
foreach (DynamicEntity existingParty in activityPartyList)
{
// compare to existing CCs
if (existingParty.Properties.Contains("addressused") &&
(existingParty.Properties["addressused"].ToString() == email))
{
bAlreadyExists = true;
break;
}
}
return bAlreadyExists;
}
}
}