关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复160或者20151014可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me!
听人问起怎么读取到一封邮件所有的附件,有人说附件也是存储在注释实体(annotation)中,我的记忆中是一个另外一个实体,SDK中有如下的原文:An activity mime attachment represents an attachment to an email message or an email template. The schema name for this entity is ActivityMimeAttachment. 这个意思中文翻译过来就是,一个ActivityMimeAttachment 实体记录代表着一个邮件或者一个邮件模板的附件。众说纷纭,但是实践出真知,让我用一个有图有真相的博文来说明下吧。
我们先来查看下元数据吧,这个实体的中文显示名称是 附件,但是元数据中还有另外一个实体的显示名称也叫附件,这个实体的架构名称是 Attachment ,描述是 电子邮件活动的附件。从本博文后面可以知道,邮件附件的文件信息是存储在这个实体中,那么注释的附件内容是不是存储在这个实体中呢?下片博文(Dynamics CRM中的注释(Note)及RollupRequest消息初探)再实践验证。我将这个实体(ActivityMimeAttachment)的字段属性截图放在下面:值得注意的是这个实体的 IsValidForAdvancedFind 是false,也就是不会出现在高级查询中。
我们先通过快速创建一封邮件。
邮件大致内容如下,注意我为本邮件上传了一个附件,名称为 logo.png,其实就是素格格新疆特产店的logo:
然后我参考下SDK中的章节 Sample: Create, retrieve, update, and delete an email attachment ,Sample: Retrieve email attachments for an email template 等, 使用了如下的代码:
        static void Main(string[] args)
{
var service = GetOrganizationService();
var fetchXML = string.Format(@"<fetch mapping='logical' output-format='xml-platform' version='1.0' distinct='false' no-lock='true' count='1'>
<entity name='email'>
<attribute name='subject' />
<attribute name='regardingobjectid' />
<attribute name='from' />
<attribute name='to' />
<attribute name='statuscode' />
<attribute name='activityid' />
<filter type='and'>
<condition value='微软MVP罗勇测试附件的邮件主题' attribute='subject' operator='eq' />
</filter>
</entity>
</fetch>");
EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXML));
if (ec.Entities.Count == )
{
Console.WriteLine("邮件的ID是" + ec.Entities[].GetAttributeValue<Guid>("activityid") + "下面是附件信息:");
QueryExpression query = new QueryExpression
{
EntityName = "activitymimeattachment",
ColumnSet = new ColumnSet("filename","subject","filesize","mimetype","activityid","activitymimeattachmentid","body"),
Criteria =
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "objecttypecode",
Operator = ConditionOperator.Equal,
Values = {"email"}
},
new ConditionExpression
{
AttributeName = "objectid",
Operator = ConditionOperator.Equal,
Values = {ec.Entities[].GetAttributeValue<Guid>("activityid")}
}
}
}
};
var i = ;
foreach (var entity in service.RetrieveMultiple(query).Entities)
{
Console.WriteLine("第" + i + "个附件:");
Console.WriteLine("filename:" + entity.GetAttributeValue<string>("filename"));
Console.WriteLine("filesize:" + entity.GetAttributeValue<int>("filesize"));
Console.WriteLine("mimetype:" + entity.GetAttributeValue<string>("mimetype"));
Console.WriteLine("subject:" + entity.GetAttributeValue<string>("subject"));
Console.WriteLine("activityid:" + entity.GetAttributeValue<EntityReference>("activityid").Id);
Console.WriteLine("activitymimeattachmentid:" + entity.GetAttributeValue<Guid>("activitymimeattachmentid"));
Console.WriteLine("body:" + entity.GetAttributeValue<string>("body"));
Console.WriteLine("--------------------------------------------------"); i++;
}
}
Console.WriteLine("程序运行完成!");
Console.ReadKey();
}

运行结果如下,可以得知filesize应该是以字节为单位的,附件的内容应该是以base64编码后存储等等信息:

当然如果有多个附件也是可以得,我去掉了读取显示body字段,运行结果如下:
我发现一个问题,就是这两个附件的 activityid 字段值是相同的,那么我可以用这个条件来查询出一个邮件的所有附件吗?试试看,代码如下:
       static void Main(string[] args)
{
var service = GetOrganizationService();
var fetchXML = string.Format(@"<fetch mapping='logical' output-format='xml-platform' version='1.0' distinct='false' no-lock='true' count='1'>
<entity name='email'>
<attribute name='subject' />
<attribute name='regardingobjectid' />
<attribute name='from' />
<attribute name='to' />
<attribute name='statuscode' />
<attribute name='activityid' />
<filter type='and'>
<condition value='微软MVP罗勇测试附件的邮件主题' attribute='subject' operator='eq' />
</filter>
</entity>
</fetch>");
EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXML));
if (ec.Entities.Count == )
{
Console.WriteLine("邮件的ID是" + ec.Entities[].GetAttributeValue<Guid>("activityid") + "下面是附件信息:");
QueryExpression query = new QueryExpression
{
EntityName = "activitymimeattachment",
ColumnSet = new ColumnSet("filename","subject","filesize","mimetype","activityid","activitymimeattachmentid"),
Criteria =
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "activityid",
Operator = ConditionOperator.Equal,
Values = {ec.Entities[].GetAttributeValue<Guid>("activityid")}
}
}
}
};
var i = ;
foreach (var entity in service.RetrieveMultiple(query).Entities)
{
Console.WriteLine("第" + i + "个附件:");
Console.WriteLine("filename:" + entity.GetAttributeValue<string>("filename"));
Console.WriteLine("filesize:" + entity.GetAttributeValue<int>("filesize"));
Console.WriteLine("mimetype:" + entity.GetAttributeValue<string>("mimetype"));
Console.WriteLine("subject:" + entity.GetAttributeValue<string>("subject"));
Console.WriteLine("activityid:" + entity.GetAttributeValue<EntityReference>("activityid").Id);
Console.WriteLine("activitymimeattachmentid:" + entity.GetAttributeValue<Guid>("activitymimeattachmentid"));
Console.WriteLine("--------------------------------------------------");
i++;
}
}
Console.WriteLine("程序运行完成!");
Console.ReadKey();
}
测试结果表明是可以的,后面邮件使用了现有附件以后我也进行了测试,也是可以得。
下面介绍下重复利用附件,也就是新邮件使用现有附件,我先用代码创建一封邮件,然后把现有的附件附加到新建的邮件:
        static void Main(string[] args)
{
var service = GetOrganizationService();
WhoAmIRequest whoAmIRequest = new WhoAmIRequest();
WhoAmIResponse whoAmIResponse = service.Execute(whoAmIRequest) as WhoAmIResponse;
Entity fromEntity = new Entity("activityparty");
fromEntity["partyid"] = new EntityReference("systemuser", whoAmIResponse.UserId);
Entity toEntity = new Entity("activityparty");
toEntity["partyid"] = new EntityReference("account", new Guid("858AB47F-494A-E511-80D2-000D3A802FAC"));
Entity emailEntity = new Entity("email");
emailEntity["to"] = new Entity[] { toEntity };
emailEntity["from"] = new Entity[] { fromEntity };
emailEntity["subject"] = "微软MVP罗勇测试邮件使用现有附件";
emailEntity["description"] = "<a href='http://sugege.top'>素格格新疆特产店</a>";
var emailId = service.Create(emailEntity);
var activityMimeAttachment = new Entity("activitymimeattachment");
activityMimeAttachment["objectid"] = new EntityReference("email",emailId);
activityMimeAttachment["objecttypecode"] = "email";
activityMimeAttachment["filename"] = "sugege.png";//不设置这个字段的值,附件不会显示文件名
activityMimeAttachment["filesize"] = ;//设置了这个字段的值,附件也不会显示文件大小(字节),囧
activityMimeAttachment["attachmentid"] = new Guid("b8ef5a55-1872-e511-80e6-000d3a804b9b");//该字段不能使用new EntityReference类型来赋值
service.Create(activityMimeAttachment);
Console.WriteLine("程序运行完成!");
Console.ReadKey();
}
代码运行结果是新增了一个邮件如下:这里有个问题就是,你双击打开这个邮件附件会看不到附件内容,而是一个上传附件的窗口给你,这和普通的使用新建附件不同,这个做的不好。
 
但是我如果查询这个邮件的附件信息会发现有点儿不同,比如filesize为0,mimetype为空值,囧。
 
那你可能会问如果邮件附件不是现有的附件,而是新建上传的附件,会不会有同样的问题,我们用代码来测试下:
       static void Main(string[] args)
{
var service = GetOrganizationService();
WhoAmIRequest whoAmIRequest = new WhoAmIRequest();
WhoAmIResponse whoAmIResponse = service.Execute(whoAmIRequest) as WhoAmIResponse;
Entity fromEntity = new Entity("activityparty");
fromEntity["partyid"] = new EntityReference("systemuser", whoAmIResponse.UserId);
Entity toEntity = new Entity("activityparty");
toEntity["partyid"] = new EntityReference("account", new Guid("858AB47F-494A-E511-80D2-000D3A802FAC"));
Entity emailEntity = new Entity("email");
emailEntity["to"] = new Entity[] { toEntity };
emailEntity["from"] = new Entity[] { fromEntity };
emailEntity["subject"] = "微软MVP罗勇测试邮件使用新附件";
emailEntity["description"] = "<a href='http://sugege.top'>素格格新疆特产店</a>";
var emailId = service.Create(emailEntity);
var activityMimeAttachment = new Entity("activitymimeattachment");
activityMimeAttachment["objectid"] = new EntityReference("email", emailId);
activityMimeAttachment["objecttypecode"] = "email";
activityMimeAttachment["subject"] = "附件示例";
activityMimeAttachment["filename"] = "mvpluoyong.txt";//不设置这个字段的值,附件不会显示文件名
activityMimeAttachment["body"] = System.Convert.ToBase64String(new UTF8Encoding().GetBytes(@"微软MVP罗勇测试邮件使用新附件"));
service.Create(activityMimeAttachment);
Console.WriteLine("程序运行完成!");
Console.ReadKey();
}
产生的邮件如下:
 
查询结果如下,可以知道查询出来的结果是齐的了,双击打开附件也是可以看到附件内容的,比用现有附件好。
表面上的说完了,我们稍微理解下在系统中是如何存储的,实体ActivityMimeAttachment在数据库中对应了一个表activitymimeattachment,但是这个表并没有存储附件的文件名,大小,内容等信息,而是这个表activitymimeattachment 通过 AttachmentId字段和另外一个实体Attachment对应的表Attachment关联起来,附件的具体内容都是存储在这个表中的。

Dynamics CRM邮件附件,你真的了解吗?的更多相关文章

  1. dynamics crm 365 附件上传图片并且显示。

    参考了几篇博客做的: 新增websource文件(html): <!DOCTYPE html> <html> <head> <title>注释</ ...

  2. Dynamics CRM中的注释(Note)及RollupRequest消息初探

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复161或者20151015可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 注释,这个实体的架构名称是Ann ...

  3. Dynamics CRM 2013开始推出的服务器端同步来配置邮件服务

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  4. 为Dynamics CRM的Office附件注释定制个无需下载即可在线查看的功能

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复164或者20151021可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 上一篇博客:为Dynamics ...

  5. 为Dynamics CRM注释的图片附件做个预览功能

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复163或者20151017可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! Dynamics CRM中注释可 ...

  6. Dynamics CRM 2016 的新特性

    新版本CRM (2016 with update 0.1) 发布已有几个月了,总结一下新特性,从几个方面来看: 1. 针对整合功能的新特性 (1) 增加了CRM App for Outlook. 这个 ...

  7. Dynamics CRM中的操作(action)是否是一个事务(transaction)?

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复168或者20151104可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 以前的博文 微软Dynamics ...

  8. C# MailMessage Attachment 中文名附件发邮件-Firefox中文显示正常,网页打开邮件附件中文名乱码

    一.故事 首先通过CDO.Message来获取邮件EML相关数据:邮件标题.邮件内容.邮件附件.发件人.收件人.CC主要就这么几个,其次通过MailMessage来组织邮件通过Python来发送邮件! ...

  9. Dynamics CRM模拟OAuth请求获得Token后在外部调用Web API

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复233或者20161104可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

随机推荐

  1. sqlserver update join 多关联更新

    由于程序bug,导致之前很多数据入库后信息不全,好在有基础信息表,可以通过基础信息表更新缺失字段信息 1.通过 inner join语法实现多关联更新 update a set a.name = b. ...

  2. Sqlite—插入语句(Insert)

    SQLite 的 INSERT INTO 语句用于向数据库的某个表中添加新的数据行. 基本语法:INSERT INTO TABLE_NAME VALUES (value1,value2,value3, ...

  3. canves做的时钟目前已经开源

    canves做的时钟目前已经开源 git地址: https://github.com/jidanji/canves-clock/tree/1.0.1 项目截图 时流过的时间变得有颜色,其他的没有颜色.

  4. 使用系统定时器SysTick实现精确延时微秒和毫秒函数

    SysTick定时器简介 SysTick定时器是存在于系统内核的一个滴答定时器,只要是ARM Cortex-M0/M3/M4/M7内核的MCU都包含这个定时器,它是一个24位的递减定时器,当计数到 0 ...

  5. Grafana = 可视化分析 + 监控告警

    Grafana是一个完美地分析和监控的开发平台 可以把Grafana理解为一个可视化面板(Dashboard),其实Kibana也是一个分析和可视化平台,只不过在大家的日常使用中Kibana是跟着Lo ...

  6. sklearn集成支持向量机svm.SVC参数说明

    经常用到sklearn中的SVC函数,这里把文档中的参数翻译了一些,以备不时之需. 本身这个函数也是基于libsvm实现的,所以在参数设置上有很多相似的地方.(PS: libsvm中的二次规划问题的解 ...

  7. 怎么将CAD转JPG?教你两种CAD转JPG方法

    在CAD中,对于CAD图纸格式的转换那是比较常见的了,因为CAD图纸的格式是dwg格式的,在使用的时候不是那么的方便,就需要将CAD图纸转换为偏于查看的格式.那怎么将CAD转JPG呢?具体要怎么来进行 ...

  8. 了解Bootstrap和开发响应式网站

    什么是Bootstrap? Bootstrap是Twitter推出的一个开源的用于web前端开发的工具包.它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CS ...

  9. C#NULL条件运算符

    C#6.0新增的特性 NULL条件运算符 ?. 之前我们在需要判断某个对象是否为空的是这样的 Person per = null; if (per != null) { Console.Write(& ...

  10. Mac-关于升级macOS Catalina后,终端试用问题

    xcrun: error 在终端输入 git clone *****后,提示: xcrun: error: invalid active developer path (/Library/Develo ...