How to Handle Email in Code

By | September 12, 2011

Just want to leave here a simple example on how to create, send and retrieve email activities.

You can download the crm prototype project with the running example including unit tests. The implementation can be found in the project Qualtech.Crm.BL and the class is named ActivityManager.cs

/// <summary>
/// Create an email activity
/// </summary>
/// <param name=”email”></param>
///emailId”>
/// <returns></returns>

public static Email CreateEmail(Email email, out Guid emailId)
{
IOrganizationService orgService = null;
SendEmailRequest sendEmailRequest = null;
SendEmailResponse sendEmailResponse = null;

Email newEmail = null;

DebugHelpers.TraceHelper(“In.”, MethodInfo.GetCurrentMethod(), RegistryManager.GetRegistryConfiguration<int>
(RegistryManager.CONFIGURATION_MEMBER_PATH, CustomConfigurationSettings.TraceOn) == 0 ? false : true, RegistryManager.GetRegistryConfiguration<string>
(RegistryManager.CONFIGURATION_MEMBER_PATH, CustomConfigurationSettings.BusinessLayerTraceFile), 0);

try
{
orgService = FacadeFactory.GetOrganizationService();

emailId = orgService.Create(email);

sendEmailRequest = new SendEmailRequest();

sendEmailRequest.EmailId = emailId;

sendEmailRequest.TrackingToken = “”;

sendEmailRequest.IssueSend = true;

sendEmailResponse = (SendEmailResponse)orgService.Execute(sendEmailRequest);

newEmail = GetEmail(emailId);
}
catch (BLException)
{

throw;

}
catch (Exception exception)
{

throw new BLException(1, exception);

}

DebugHelpers.TraceHelper(“Out.”, MethodInfo.GetCurrentMethod(), RegistryManager.GetRegistryConfiguration<int>
(RegistryManager.CONFIGURATION_MEMBER_PATH, CustomConfigurationSettings.TraceOn) == 0 ? false : true, RegistryManager.GetRegistryConfiguration<string>
(RegistryManager.CONFIGURATION_MEMBER_PATH, CustomConfigurationSettings.BusinessLayerTraceFile), 0);

return newEmail;

}

/// <summary>
/// Look up an email activity.
/// </summary>
/// <param name=”emailId”></param>
/// <returns>An Email</returns>

public static Email GetEmail(Guid emailId)
{
IOrganizationService orgService = null;
Email email = null;

DebugHelpers.TraceHelper(“In.”, MethodInfo.GetCurrentMethod(), RegistryManager.GetRegistryConfiguration<int>
(RegistryManager.CONFIGURATION_MEMBER_PATH, CustomConfigurationSettings.TraceOn) == 0 ? false : true, RegistryManager.GetRegistryConfiguration<string>
(RegistryManager.CONFIGURATION_MEMBER_PATH, CustomConfigurationSettings.BusinessLayerTraceFile), 0);

try
{
orgService = FacadeFactory.GetOrganizationService();

email = (Email)orgService.Retrieve(Email.EntityLogicalName, emailId, new ColumnSet(true));
}
catch (Exception exception)
{

throw new BLException(1, exception);

}

DebugHelpers.TraceHelper(“Out.”, MethodInfo.GetCurrentMethod(), RegistryManager.GetRegistryConfiguration<int>
(RegistryManager.CONFIGURATION_MEMBER_PATH, CustomConfigurationSettings.TraceOn) == 0 ? false : true, RegistryManager.GetRegistryConfiguration<string>
(RegistryManager.CONFIGURATION_MEMBER_PATH, CustomConfigurationSettings.BusinessLayerTraceFile), 0);

return email;

}

}

 

 

Leave a Reply