Example #1
0
 public EmailCrm(IOrganizationService service, EmailModel emailModel, ConfigEmail configemail)
 {
     _service    = service;
     _emailModel = emailModel;
     _emailModel.message.content_type = HTML;
     _emailModel.message.charset      = UTF8;
     _emailModel.message.from_email   = configemail.FromEmail;
     _configemail = configemail;
 }
Example #2
0
        protected void EmailHandlerPreSend(LocalPluginContext localContext)
        {
            //throw new InvalidPluginExecutionException("ddd");
            ConfigEmail configemail = null;

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(_unsecureString)))
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ConfigEmail));
                configemail = (ConfigEmail)ser.ReadObject(ms);
            }

            EmailModel emailModel = new EmailModel();

            emailModel.message = new Message();
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            var traceService = localContext.TracingService;
            //Check if target exists.
            var isTargetExistsAndIsEnity = context.InputParameters.Contains("EmailId") && context.InputParameters["EmailId"] is Guid;

            if (!isTargetExistsAndIsEnity)
            {
                throw new InvalidPluginExecutionException("Pre update plugin step must contain an entity target");
            }

            Guid emailid = (Guid)context.InputParameters["EmailId"];

            EmailCrm emailCrm   = new EmailCrm(service, emailModel, configemail);
            bool     issueEmail = emailCrm.CrmFieldSetEmailAgent(emailid);

            if (!issueEmail)
            {
                IAgentSend agent;
                if (configemail.Url.Contains("TestEmail"))
                {
                    agent = new AgentEmailSubmit();
                }
                else
                {
                    agent = new AgentInwiseEmailSubmit();
                }

                agent.Send(traceService, configemail, emailModel);
                context.InputParameters["IssueSend"] = issueEmail;//true if the email should be sent; otherwise, false, just record it as sent.
            }
        }
        public void Send(ITracingService log, ConfigEmail config, EmailModel emailModel)
        {
            using (MemoryStream stream1 = new MemoryStream())
            {
                string json = "", response = "";
                try
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(EmailModel));
                    ser.WriteObject(stream1, emailModel);
                    stream1.Position = 0;
                    StreamReader sr = new StreamReader(stream1);
                    // var json = Encoding.UTF8.GetString(stream1.ToArray());
                    json = sr.ReadToEnd();

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(config.Url);
                    request.Method      = "POST";
                    request.ContentType = "application/json";
                    request.Accept      = "application/json";
                    //      request.KeepAlive = true;
                    request.ContentLength = Encoding.UTF8.GetByteCount(json);
                    //    request.SendChunked = true;
                    UTF8Encoding encoding = new UTF8Encoding();
                    request.AllowWriteStreamBuffering = true;
                    byte[] bytes = encoding.GetBytes(json);
                    using (Stream writeStream = request.GetRequestStream())
                    {
                        writeStream.Write(bytes, 0, bytes.Length);
                    }

                    using (WebResponse webResponse = request.GetResponse())
                        using (Stream webStream = webResponse.GetResponseStream())
                            using (StreamReader responseReader = new StreamReader(webStream))
                            {
                                response = responseReader.ReadToEnd();
                                Console.Out.WriteLine(response);
                                responseReader.Close();
                            }
                    log.Trace("demo:::request={0} ,resposne={1}", json, response);
                }

                catch (Exception e1)
                {
                    throw new InvalidPluginExecutionException(e1.Message);
                }
            }
        }