我有以下用例。
客户希望转发多个联系人的特定现有电子邮件。因此,每个联系人都有包含附件的电子邮件。
所以我需要提取相关的联系人,它们是包含附件的电子邮件,然后将其发送到另一个电子邮件地址。
我可以检索相关的联系人,但是现在我不知道如何检索电子邮件,附件并将它们链接在一起,以便可以发送它们。
有人可以给我一些建议吗?这将是极好的,并受到高度赞赏。
答案 0 :(得分:1)
以下是可以执行此操作的工作流的一个示例。附件的实体称为activitymimeattachment。此代码从电子邮件中提取附件并将其转换为便笺。在您的情况下,您应该使用“ attachmentid”,“ filename”,“ mimetype”创建新电子邮件,我认为这已经足够了。祝你好运!
using System;
using System.Activities;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
namespace Dyn365Apps.CRM.Workflow
{
public class ExtractAttachmentsFromEmailAndCreateNotes : CodeActivity
{
[RequiredArgument]
[Input("Email")]
[ReferenceTarget("email")]
public InArgument<EntityReference> receivedEmail { get; set; }
[RequiredArgument]
[Input("Enquiry")]
[ReferenceTarget("incident")]
public InArgument<EntityReference> enquiry { get; set; }
protected override void Execute(CodeActivityContext context)
{
var trace = context.GetExtension<ITracingService>();
try
{
var serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
var service = serviceFactory.CreateOrganizationService(Guid.Empty); //Use current user's ID
if (service != null)
{
trace.Trace("Organization Service Created");
}
// Get Attachment Count
trace.Trace("Get Attachment Count");
var rem = receivedEmail.Get(context);
Entity email = service.Retrieve(rem.LogicalName, rem.Id, new ColumnSet("attachmentcount"));
int atc = (int)email["attachmentcount"];
trace.Trace("Attachment count = " + atc.ToString());
if (atc > 0)
{
// Get all attachments
QueryExpression queryAtt = new QueryExpression("activitymimeattachment");
queryAtt.ColumnSet = new ColumnSet(new string[] { "activityid", "attachmentid", "filename", "body", "mimetype", "subject" });
queryAtt.Criteria = new FilterExpression();
queryAtt.Criteria.FilterOperator = LogicalOperator.And;
queryAtt.Criteria.AddCondition(new ConditionExpression("activityid", ConditionOperator.Equal, email.Id));
EntityCollection eatt = service.RetrieveMultiple(queryAtt);
var entities = eatt.Entities;
trace.Trace("Entities count = " + entities.Count());
foreach (var ent in entities)
{
trace.Trace("Inside the for loop");
trace.Trace("Attributes count = " + ent.Attributes.Count());
// Instantiate an Annotation object.
Entity annotation = new Entity("annotation");
if (ent.Attributes.Contains("subject"))
{
trace.Trace("subject = " + ent.Attributes["subject"].ToString());
annotation["subject"] = ent.Attributes["subject"].ToString();
}
else
{
trace.Trace("subject not found");
annotation["subject"] = "Undefined";
}
if(ent.Attributes.Contains("filename"))
{
trace.Trace("filename = " + ent.Attributes["filename"].ToString());
annotation["filename"] = ent.Attributes["filename"].ToString();
}
else
{
trace.Trace("filename not found");
annotation["filename"] = "Undefined.txt";
}
if (ent.Attributes.Contains("mimetype"))
{
trace.Trace("mimetype = " + ent.Attributes["mimetype"].ToString());
annotation["mimetype"] = ent.Attributes["mimetype"].ToString();
}
else
{
trace.Trace("mimetype not found");
annotation["mimetype"] = "plain/text";
}
if (ent.Attributes.Contains("body"))
{
annotation["documentbody"] = ent.Attributes["body"];
}
trace.Trace("objectid = " + enquiry.Get(context).Id.ToString());
annotation["objectid"] = enquiry.Get(context);
annotation["objecttypecode"] = 112; // Case
// Create a Note with the attachment
service.Create(annotation);
}
}
}
catch (Exception ex)
{
trace.Trace("ex.Message = {0}", ex.Message);
trace.Trace("ex.StackTrace = {0}", ex.StackTrace);
}
}
}
}