ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-04-20 09:34:01  阅读:207  来源: 互联网

标签:outlook List Dynamic scc Entity party new email CRM


 

        /// <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);
            }
        }

 

标签:outlook,List,Dynamic,scc,Entity,party,new,email,CRM
来源: https://www.cnblogs.com/parkerchen/p/16168419.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有