Dynamics CRM Plugin 发送邮件

1 创建邮件

       /// <param name="service"></param>
        /// <param name="fromemail">发件人的Guid的List</param>
        /// <param name="tomail">收件人的Guid的List</param>
        /// <param name="ccmail">抄送人的Guid的List</param>
        /// <param name="entity">需要发送邮件的实体,可以根据此处获取邮件中需要的信息</param>
        /// <returns></returns>
        private void CreateInformEmail(IOrganizationService service, List<Guid> fromemail, List<Guid> tomail, List<Guid> ccmail, Entity entity)
        {
            try
            {
                #region subject/send/to/cc
                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);
                }
 
                string subjectTxt = string.Empty;
                string fName = entity.GetAttributeValue<string>("new_name");
                if (fName == null) fName = "";
                var owner = entity.GetAttributeValue<EntityReference>("new_business_owner");
                string ownerName = string.Empty;
                if (owner != null) ownerName = owner.Name;
 
                Entity eml = new Entity("email");
                eml.Attributes["from"] = from_list.ToArray();
                eml.Attributes["to"] = to_list.ToArray();
                eml.Attributes["cc"] = cc_list.ToArray();
                eml["regardingobjectid"] = new EntityReference(entity.LogicalName, entity.Id);
                eml.Attributes["actualdurationminutes"] = 30;
                eml.Attributes["isworkflowcreated"] = false;
                //邮件主题
                subjectTxt = fName + " 合同到期提醒 Contract Expiration Reminder";
                eml.Attributes["subject"] = subjectTxt;
 
                #endregion
 
                #region 邮件模板
                StringBuilder body = new StringBuilder();
                //body.AppendLine("<p>Dear {0} ,</p>");
                body.AppendLine(string.Format("<p>Dear Mr/Ms {0} ,</p>", ownerName));//收件人的 first name
                string url = ConfigUtils.CRMExtenalUrl + ConfigUtils.CRMOrganizationName + "main.aspx?etn=" + entity.LogicalName + "&pagetype=entityrecord&id=%7B" + entity.Id + "%7D";
                body.AppendLine(string.Format("<p style='text-indent:36.0pt'>{0} 合同30天后即将到期.", fName));
                body.AppendLine(string.Format("查看详细信息,请点击链接 <a href='{0}' target='_blank'>{1}</a>.</p>", url, fName));
                body.AppendLine(string.Format("<p style='text-indent:36.0pt'> Contract {0} will be expired 30 days later.", fName));
                body.AppendLine(string.Format("For more information, please click on <a href='{0}' target='_blank'>{1}</a>.</p>", url, fName));
                body.AppendLine("<p>B. Regards</p>");
                body.AppendLine("<p>CRM</p>");
                eml.Attributes["description"] = body.ToString();
                #endregion
 
                Guid email_id = service.Create(eml);
                //return email_id;
 
                //发送
                SendEmailRequest sendEmailreq = new SendEmailRequest();
                sendEmailreq.EmailId = email_id;
                sendEmailreq.TrackingToken = "";
                sendEmailreq.IssueSend = true;
                SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("Create inform email error: " + ex.Message);
            }
        }

2.获取需要发邮件的用户

 public Guid GetMailUserId(string strDomainName)
        {
            string sql = @"select SystemUserId,DomainName FROM systemuserBase WITH(NOLOCK)
                            where DomainName = @domainName ";
 
            SqlParameter[] parameters ={
                                           new SqlParameter("@domainName",SqlDbType.NVarChar,100)
                                       };
            parameters[0].Value = strDomainName;
 
            DataSet ds = SqlHelper.ExecuteDataSet(CommandType.Text, sql, parameters);
            Guid g_OwnerId = Guid.Parse(ds.Tables[0].Rows[0]["SystemUserId"].ToString());
            //EntityReference ref_user = new EntityReference("systemuser", g_OwnerId);
 
            return g_OwnerId;
        }

from:https://blog.csdn.net/u012664198/article/details/82191140

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值