Dynamic CRM 365 调用CRM自动的outlook发送邮件


        /// 
        /// 发送邮件
        /// 
        /// 
        /// 
        /// 
        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("scc_predicted_end_date_zz").AddHours(8).ToString("yyyy-MM-dd") : "");
                string scc_remake = entity.GetAttributeValue<string>("scc_remake");

                List guidArray = new List();
                List ownerArray = new List();
                List<string> nameArray = new List<string>();
                ownerArray.Add(CrmData.GetSingleValueLabel("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("scc_type_of_predict").Value;

                    string typename = "业务组", objname = "";
                    objname = en.GetAttributeValue("scc_group").Name;
                    EntityReference ownerid = en.GetAttributeValue("ownerid");

                    if (ownerid == null)
                    {
                        results.Code = "309";
                        results.Message = $"销售明细{en.GetAttributeValue("scc_name")}对应的发邮件对象为空,请联系管理员!";
                        return results;

                    }
                    var linkText = "" + link + ">点击此处";
                    var emailtemplateeditorEntity = accountRepository.GetTemplateeditorEntity(service, "销售预测");
                    if (emailtemplateeditorEntity != null)
                    {
                        List newguidA = new List();
                        newguidA.Add(ownerid.Id);
                        var subjectsafehtml = emailtemplateeditorEntity.GetAttributeValue<string>("subjectsafehtml");
                        var safehtml = emailtemplateeditorEntity.GetAttributeValue<string>("safehtml");
                        var title = entity.GetAttributeValue("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;
        }

获取模板信息

      /// 
        /// 获得模板信息
        /// 
        /// 
        /// 
        /// 
        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;
        }

邮件发送实体赋值:

/// 发件人的Guid的List
        /// 收件人的Guid的List
        /// 抄送人的Guid的List
        /// 需要发送邮件的实体,可以根据此处获取邮件中需要的信息
        /// 
        public Entity CreateInformEmail1(List fromemail, List tomail, List ccmail, Entity entity)
        {
            try
            {
                List from_list = new List();
                List to_list = new List();
                List cc_list = new List();
                //发件人
                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);
            }
        }