Dataverse plugin to get subject, sender and recipients from RFC822 output

public class AddEmailPartiesFromCreatedAndRegarding : IPlugin
{
	
	public void Execute(IServiceProvider serviceProvider)
	{

		ITracingService tracingService =
			(ITracingService)serviceProvider.GetService(typeof(ITracingService));
		IPluginExecutionContext context = (IPluginExecutionContext)
			serviceProvider.GetService(typeof(IPluginExecutionContext));
		IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
		IOrganizationService crmService = serviceFactory.CreateOrganizationService(context.UserId);

		if (context.InputParameters.Contains("Target"))
		{
			Entity entity = (Entity)context.InputParameters["Target"];
			Entity emailMessage = crmService.Retrieve("email", entity.Id, new ColumnSet(true));

			if (!emailMessage.Contains("subcategory")) return;

			var description = (string)emailMessage["description"];
			var subject = (string)emailMessage["subject"];

			var emailUpdate = new Entity("email", emailMessage.Id);
			emailUpdate["from"] = GetSender(description);
			emailUpdate["to"] = GetRecipients(description);
			emailUpdate["cc"] = GetCCs(description);
			emailUpdate["statuscode"] = new OptionSetValue(2);
			emailUpdate["statecode"] = new OptionSetValue(1);
			emailUpdate["description"] = StripDescription(description);
			emailUpdate["subject"] = StripSubject(description, subject);
			crmService.Update(emailUpdate);
		}
	}


	// Sender: (?i)(?<=From:)[\s\S]+(?=\nSubject:)
	// Subject: (?i)(?<=Subject:)[\s\S]+(?=\nTo:)
	// Recipients: (?i)(?<=To:)[\s\S]+(?=\ncc:)
	// CC: (?i)(?<=cc:)[\s\S]+(?=\nMessage\-ID:)
	// Email: (?=\<).*(?=\>)

	static Entity[] GetRecipients(string str)
	{
		var stringData = str;
		Regex regex = new Regex(@"(?i)(?<=To:)[\s\S].*?(?=\ncc:|cc:|\nMessage-ID:|\nContent-Type:|\nX-GM-Attachments-Sync-Time:)");
		var partylist = GetPartyList(stringData, regex);
		return partylist;
	}

	static Entity[] GetCCs(string str)
	{
		var stringData = str;
		Regex regex = new Regex(@"(?<=cc:)[\s\S].*?(?=\nMessage-ID:|Message-ID:)");
		var partylist = GetPartyList(stringData, regex);
		return partylist;
	}
	static Entity[] GetSender(string str)
	{
		var stringData = str;
		var regex = new Regex(@"(?i)(?<=From:)[\s\S].*?(?=\nSubject:|Subject:|cc:)");
		var partylist = GetPartyList(stringData, regex);
		return partylist;
	}
	static string RegexMatch(string str, string regexStr)
	{
		var stringData = str;
		Regex regex = new Regex(regexStr);
		Match match = regex.Match(stringData);
		if (match.Success)
		{
			return match.Groups[0].Value;
		}
		else return str;
	}
	static string StripDescription(string str)
	{
		var stringData = str;
		var regexStr = @"(?i)(?=<html)[\S\s]+(?:<\/html>)";
		return RegexMatch(stringData, regexStr);
	}
	static string StripSubject(string str, string subject)
	{
		var stringData = str;
		var regexStr = @"(?i)(?<=Subject:)[\s\S].*?(?=\nTo:|To:)";
		var match = RegexMatch(stringData, regexStr);
		var returnValue = subject;
		if (match != stringData)
		{
			returnValue = match;
		}
		return returnValue;
	}
	static string StripEmailAddress(string str)
	{
		var stringData = str;
		Regex regex = new Regex(@"(?<=\<)[\s\S\r]+(?=\>)");
		Match match = regex.Match(stringData);
		if (match.Success)
		{
			return match.Groups[0].Value;
		}
		else return str;
	}
	static Entity[] GetPartyList(string body, Regex regex)
	{
		var stringData = body; 
		List<Entity> partyList = new List<Entity>();
		Match match = regex.Match(stringData);
		if (match.Success)
		{
			var emailString = match.Groups[0].Value;
			string[] emailArray;

			if (emailString.Contains(','))
			{
				var parsedString = emailString.Replace(",", ";");
				emailString = parsedString;
			}

			if (emailString.Contains(";"))
			{
				emailArray = emailString.Split(';');
			}
			else
			{
				emailArray = new[] { emailString };
			}

			for (var i = 0; i < emailArray.Length; i++)
			{
				var emailAddress = (StripEmailAddress(emailArray[i])).Trim();
				
				if (emailAddress.Length > 0)
				{
					var emailParty = new Entity("activityparty");
					emailParty["addressused"] = emailAddress;
					emailParty["participationtypemask"] = 2;
					partyList.Add(emailParty);
				}
				else
				{
					throw new InvalidPluginExecutionException(emailString + " " + regex.ToString());
				}
			}
			return partyList.ToArray<Entity>();
		}
		else return null;
	}
}
Filed under: Blog

No comment yet, add your voice below!


Add a Comment

Your email address will not be published.

Comment *
Name *
Email *
Website

This site uses Akismet to reduce spam. Learn how your comment data is processed.