1. 程式人生 > 其它 >Dynamic CRM 365 呼叫CRM自動的outlook傳送郵件

Dynamic CRM 365 呼叫CRM自動的outlook傳送郵件

 

        /// <summary>
        /// 傳送郵件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="link"></param>
        /// <returns></returns>
        public ActionResults sendEmial_Business(string id, string link)
        {
            ActionResults results 
= new ActionResults(); try { Entity entity = service.Retrieve("scc_forecasted_statement", new Guid(id), new ColumnSet("scc_issend_ar", "ownerid", "scc_name", "scc_forecast_name", "scc_forecast_type", "scc_predicted_end_date_zz", "scc_remake"));
string scc_predicted_end_date = (entity.Contains("scc_predicted_end_date_zz") ? entity.GetAttributeValue<DateTime>("scc_predicted_end_date_zz").AddHours(8).ToString("yyyy-MM-dd") : ""); string scc_remake = entity.GetAttributeValue<string>("scc_remake"); List
<Guid> guidArray = new List<Guid>(); List<Guid> ownerArray = new List<Guid>(); List<string> nameArray = new List<string>(); ownerArray.Add(CrmData.GetSingleValueLabel<Guid>("systemuser", "domainname", "scc\\CRM", "systemuserid", service)); QueryExpression qe = new QueryExpression("scc_forecast_line"); qe.ColumnSet = new ColumnSet("scc_forecast_lineid", "scc_type_of_predict", "scc_customer_code", "scc_website_code", "scc_material_coding", "scc_subitems" , "scc_customer", "scc_website", "scc_product", "scc_business_first_levelid", "scc_name", "ownerid", "scc_group"); qe.Criteria.AddCondition("scc_forecasted_statement_forecast_lin", ConditionOperator.Equal, entity.Id); qe.Criteria.AddCondition("scc_scc_status", ConditionOperator.NotEqual, 2); //未完成 qe.Criteria.AddCondition("scc_type_of_predict", ConditionOperator.In, 5); //業務組的型別 var encollect = service.RetrieveAll(qe); foreach (Entity en in encollect.Entities) { #region 迴圈傳送郵件 var scc_type_of_predict = en.GetAttributeValue<OptionSetValue>("scc_type_of_predict").Value; string typename = "業務組", objname = ""; objname = en.GetAttributeValue<EntityReference>("scc_group").Name; EntityReference ownerid = en.GetAttributeValue<EntityReference>("ownerid"); if (ownerid == null) { results.Code = "309"; results.Message = $"銷售明細{en.GetAttributeValue<string>("scc_name")}對應的發郵件物件為空,請聯絡管理員!"; return results; } var linkText = "<a href = " + link + ">點選此處</a>"; var emailtemplateeditorEntity = accountRepository.GetTemplateeditorEntity(service, "銷售預測"); if (emailtemplateeditorEntity != null) { List<Guid> newguidA = new List<Guid>(); newguidA.Add(ownerid.Id); var subjectsafehtml = emailtemplateeditorEntity.GetAttributeValue<string>("subjectsafehtml"); var safehtml = emailtemplateeditorEntity.GetAttributeValue<string>("safehtml"); var title = entity.GetAttributeValue<String>("scc_forecast_name"); subjectsafehtml = subjectsafehtml.Replace("【預測名稱】", title); safehtml = safehtml.Replace("【姓名】", ownerid.Name).Replace("【預測型別】", typename).Replace("【物件】", objname).Replace("【填寫結束日期】", scc_predicted_end_date) .Replace("【預測說明】", scc_remake).Replace("【連結】", linkText); var email = accountRepository.CreateInformEmail1(ownerArray, newguidA, null, entity); email["subject"] = subjectsafehtml; email["description"] = safehtml; var emialId = service.Create(email); SendEmailRequest sendEmailreq = new SendEmailRequest { EmailId = emialId, TrackingToken = "", IssueSend = true }; SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq); newguidA.Clear(); } #endregion } } catch (Exception ex) { results.Code = ResponseCode.Code300; results.Message = ex.Message;return results; } return results; }

 

獲取模板資訊

      /// <summary>
        /// 獲得模板資訊
        /// </summary>
        /// <param name="service"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public Entity GetTemplateeditorEntity(IOrganizationService service, string title)
        {
            QueryByAttribute query = new QueryByAttribute("template");
            query.AddAttributeValue("title", title);
            //query.AddAttributeValue("statecode", 0);//可用
            query.ColumnSet = new ColumnSet(true);
            EntityCollection ec = service.RetrieveMultiple(query);
            if (ec != null && ec.Entities != null && ec.Entities.Count > 0)
            {
                return ec[0];
            }
            return null;
        }

 

郵件傳送實體賦值:

/// <param name="fromemail">發件人的Guid的List</param>
        /// <param name="tomail">收件人的Guid的List</param>
        /// <param name="ccmail">抄送人的Guid的List</param>
        /// <param name="entity">需要傳送郵件的實體,可以根據此處獲取郵件中需要的資訊</param>
        /// <returns></returns>
        public Entity CreateInformEmail1(List<Guid> fromemail, List<Guid> tomail, List<Guid> ccmail, Entity entity)
        {
            try
            {
                List<Entity> from_list = new List<Entity>();
                List<Entity> to_list = new List<Entity>();
                List<Entity> cc_list = new List<Entity>();
                //發件人
                foreach (Guid from in fromemail)
                {
                    Entity from_party = new Entity("activityparty");
                    from_party.Attributes["partyid"] = new EntityReference("systemuser", from);
                    from_party.Attributes["partyobjecttypecode"] = 8;
                    from_list.Add(from_party);
                }
                //收件人
                foreach (Guid to in tomail)
                {
                    Entity to_party = new Entity("activityparty");
                    to_party.Attributes["partyid"] = new EntityReference("systemuser", to);
                    to_party.Attributes["partyobjecttypecode"] = 8;
                    to_list.Add(to_party);
                }
                ////抄送人
                //foreach (Guid cc in ccmail)
                //{
                //    Entity cc_party = new Entity("activityparty");
                //    cc_party.Attributes["partyid"] = new EntityReference("systemuser", cc);
                //    cc_party.Attributes["partyobjecttypecode"] = 8;
                //    cc_list.Add(cc_party);
                //}

                Entity email = new Entity("email");
                email.Attributes["from"] = from_list.ToArray();
                email.Attributes["to"] = to_list.ToArray();
                email.Attributes["cc"] = cc_list.ToArray();
                //email["regardingobjectid"] = new EntityReference(entity.LogicalName, entity.Id);
                //email.Attributes["actualdurationminutes"] = 30;
                //email.Attributes["isworkflowcreated"] = false;

                return email;
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("Create inform email error: " + ex.Message);
            }
        }