using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Email;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mail;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Configuration;
using System.IO;
using System.Runtime.Serialization.Json;
namespace DDU.CALLAPI_PLUGIN
{
public class Class1 : IPlugin
{
#region Class Member Declaration
IPluginExecutionContext pluginexecutioncontext;
IOrganizationServiceFactory servicefactory;
IOrganizationService service;
#endregion
public void Execute(IServiceProvider serviceProvider)
{
#region Class Member Definition
pluginexecutioncontext = (IPluginExecutionContext)serviceProvider.GetService((typeof(IPluginExecutionContext)));
servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService((typeof(IOrganizationServiceFactory)));
service = servicefactory.CreateOrganizationService(pluginexecutioncontext.UserId);
#endregion
Entity ObjPrimaryEntity = (Entity)pluginexecutioncontext.InputParameters["Target"];
//Entity ObjPrimaryEntity = (Entity)service.Retrieve("email", pluginexecutioncontext.PrimaryEntityId, new ColumnSet(true));
EmailActivityModel objEmail = new EmailActivityModel();
objEmail.To = ObjPrimaryEntity.GetAttributeValue<EntityCollection>("to").Entities[0].GetAttributeValue<EntityReference>("partyid").Id; //gets id
Entity obRegistration = (Entity)service.Retrieve("new_registration", objEmail.To, new ColumnSet(true));
objEmail._To = obRegistration != null ? Convert.ToString(obRegistration.Attributes["new_emailaddress"]) : string.Empty; //gets email address
objEmail.From = ObjPrimaryEntity.GetAttributeValue<EntityCollection>("from").Entities[0].GetAttributeValue<EntityReference>("partyid").Id;//gets id
Entity obSystemUser = (Entity)service.Retrieve("systemuser", objEmail.From, new ColumnSet(true));
objEmail._From = obSystemUser != null ? Convert.ToString(obSystemUser.Attributes["internalemailaddress"]) : string.Empty; //gets email address
objEmail.Subject = ObjPrimaryEntity.GetAttributeValue<string>("subject");
objEmail.body = ObjPrimaryEntity.GetAttributeValue<string>("description");
#region Post - Send email
string decodebody = System.Net.WebUtility.HtmlDecode(objEmail.body);
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "To=" + objEmail._To;
postData += ("&Subject=" + objEmail.Subject);
postData += ("&body=" + decodebody);
postData += ("&From=" + objEmail._From);
postData += ("&CC=" + objEmail.CC);
byte[] Postdata = encoding.GetBytes(postData);
#endregion
#region Prepare Web Request
// Prepare web request...
try
{
WebRequest request = WebRequest.Create("uatservice.azurewebsites.net/.../SendEmail");
request.Method = "POST";
request.ContentLength = Postdata.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream dataStream = request.GetRequestStream();
// dataStream.Length = 1073741823;
dataStream.Write(Postdata, 0, Postdata.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
string desc = ((HttpWebResponse)response).StatusDescription;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
}
catch (InvalidPluginExecutionException ex)
{
}
#endregion Prepare Web request
}
public struct PostData
{
public string _To { get; set; }
public string FromEmail { get; set; }
public string Body { get; set; }
public string Subject { get; set; }
}
}
}