博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
crm操作电子邮件
阅读量:6505 次
发布时间:2019-06-24

本文共 3787 字,大约阅读时间需要 12 分钟。

    using System;

    using Microsoft.Xrm.Sdk;
    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;

    /// <summary>

    /// 电子邮件
    /// </summary>
    public class EmailHelper
    {
        public static readonly string entityName = "email";
        public Guid emailId = Guid.Empty;
        public IOrganizationService service;

        /// <summary>

        /// 创建电子邮件
        /// </summary>
        public void Create()
        {
            Entity en = new Entity();
            en["subject"] = "电子邮件測试";
            en["activityId"] = Guid.NewGuid();
            emailId = service.Create(en);
        }

        /// <summary>

        /// 异步发送电子邮件
        /// </summary>
        /// <param name="query">查询的条件</param>
        public void BackgroundSendEmail(QueryBase query)
        {
            BackgroundSendEmailRequest request = new BackgroundSendEmailRequest();
            request.Query = query;
            BackgroundSendEmailResponse response = (BackgroundSendEmailResponse)service.Execute(request);
            EntityCollection entityCollection = response.EntityCollection;
            bool[] hasAttachments = response.HasAttachments;
        }

        /// <summary>

        /// 检查是否应该将传入电子邮件提升到 Microsoft Dynamics CRM 系统
        /// </summary>
        /// <param name="messageId">消息id</param>
        /// <param name="subject">主题</param>
        public void CheckPromoteEmail(string messageId,string subject)
        {
            CheckPromoteEmailRequest request = new CheckPromoteEmailRequest();
            request.MessageId = messageId;
            request.Subject = subject;
            CheckIncomingEmailResponse response = (CheckIncomingEmailResponse)service.Execute(request);
            int reasonCode = response.ReasonCode;
            bool shouldDeliver = response.ShouldDeliver;
        }

        /// <summary>

        /// 发送批量电子邮件
        /// </summary>
        /// <param name="query">查询的条件</param>
        /// <param name="templateId">模版id</param>
        public void SendBulkMail(QueryBase query,Guid templateId)
        {
            WhoAmIRequest emailSenderRequest = new WhoAmIRequest();
            WhoAmIResponse emailSenderResponse = service.Execute(emailSenderRequest) as WhoAmIResponse;
            SendBulkMailRequest request = new SendBulkMailRequest();
            request.Query = query;
            request.Sender = new EntityReference() { LogicalName = "systemuser", Id = emailSenderResponse.UserId };
            request.RequestId = Guid.Empty;
            request.RegardingType = "systemuser";
            request.TemplateId = templateId;
            SendBulkMailResponse response = (SendBulkMailResponse)service.Execute(request);
        }

        /// <summary>

        /// 使用模板发送电子邮件
        /// </summary>
        /// <param name="userId">发送人。用户id</param>
        /// <param name="contractId">接收人,联系人id</param>
        /// <param name="templateId">模版id</param>
        public void SendEmailFromTemplate(Guid userId, Guid contractId, Guid templateId)
        {
            Entity fromEn = new Entity() { LogicalName = "systemuser",Id = userId };
            Entity toEn = new Entity() { LogicalName = "contract", Id = contractId };
            Entity emailEn = new Entity() { LogicalName = entityName };
            emailEn["from"] = new Entity[] { fromEn };
            emailEn["to"] = new Entity[] { toEn };
            emailEn["subject"] = "电子邮件发送測试";
            emailEn["directioncode"] = true;
            SendEmailFromTemplateRequest request = new SendEmailFromTemplateRequest();
            request.Target = emailEn;
            request.TemplateId = templateId;
            request.RegardingId = contractId;
            request.RegardingType = "contract";
            SendEmailFromTemplateResponse response = (SendEmailFromTemplateResponse)service.Execute(request);
            Guid id = response.Id;
        }

        /// <summary>

        /// 发送电子邮件
        /// </summary>
        public void SendEmail()
        {
            SendEmailRequest request = new SendEmailRequest();
            request.EmailId = emailId;
            request.IssueSend = true;
            request.TrackingToken = "";
            SendEmailResponse response = (SendEmailResponse)service.Execute(request);
            string subject = response.Subject;
        }

        /// <summary>

        /// 获取用于对存储在 Microsoft Dynamics CRM 数据库中的用户或队列的电子邮件凭据进行加密或解密的密钥
        /// </summary>
        public void GetDecryptionKey()
        {
            GetDecryptionKeyRequest request = new GetDecryptionKeyRequest();
            GetDecryptionKeyResponse response = (GetDecryptionKeyResponse)service.Execute(request);
            string key = response.Key;
        }

        /// <summary>

        /// 删除电子邮件
        /// </summary>
        public void Delete()
        {
            service.Delete(entityName, emailId);
        }
    }

转载地址:http://wmmyo.baihongyu.com/

你可能感兴趣的文章
全景图的种类
查看>>
git 维护
查看>>
jfinal框架下使用c3P0连接池连接sql server 2008
查看>>
Jfinal Generator 不需要生成带某个前缀的表名数组的方法
查看>>
struts2中使用标签操作静态方法等
查看>>
熬夜写了一个小游戏,向SpaceX聊表敬意
查看>>
身份证工具类
查看>>
JPA增删改查,
查看>>
apache 开启 gzip 压缩服务
查看>>
python mysql
查看>>
开源 免费 java CMS - FreeCMS1.5-建站向导
查看>>
Selenium的延迟等待
查看>>
jquery 1.6以上版本 全选
查看>>
AppCan 学习
查看>>
flask框架
查看>>
《疯狂Java讲义》学习笔记(十)异常处理
查看>>
Lua(Codea) 中 table.insert 越界错误原因分析
查看>>
ELK 5.x日志分析 (二) Elasticserach 5.2 安装
查看>>
sbt配置nexus仓库
查看>>
一次奇怪的AP注册异常问题处理
查看>>