Dynamics CRM:如何使用 C# 创建带附件的电子邮件记录

Dynamics CRM: How to create an email record with attachment using C#

我想创建一个电子邮件记录,并将其标记为 'Received',并且可能会添加附件到这个新的电子邮件记录中。有人知道如何使用 C# 做到这一点吗?

有一个 Official example 代码但没有附件,所以我将在这里使用我的代码:

分为三个步骤:

  1. 创建电子邮件记录。
  2. 为电子邮件创建附件记录。
  3. 使用 SendEmailRequest
  4. 发送此电子邮件

示例代码如下:

// 1. Create the email record.
Entity newEmail = new Entity("email");
newEmail["subject"] = "your email subject";
newEmail["description"] = "your email content";

Entity toparty = new Entity("activityparty");
toparty["addressused"] = "to@email.com";
Guid contactid = new Guid();
toparty["partyid"] = new EntityReference("contact", contactid);
newEmail["to"] = new Entity[] { toparty };

Entity fromparty = new Entity("activityparty");
fromparty["addressused"] = "from@email.com";
Guid userid = new Guid();
fromparty["partyid"] = new EntityReference("systemuser", userid);
newEmail["from"] = new Entity[] { fromparty };

Guid targetEmailId = serviceproxy.Create(newEmail);

// 2. Create Attachment for email
Entity linkedAttachment = new Entity("activitymimeattachment");
linkedAttachment.Attributes["objectid"] = new EntityReference("email", targetEmailId);
linkedAttachment.Attributes["objecttypecode"] = "email";
linkedAttachment.Attributes["filename"] = "DemoAttachment.pdf";
linkedAttachment.Attributes["mimetype"] = "application/pdf";
linkedAttachment.Attributes["body"] = "your attachment file stream in BASE64 format string";
serviceproxy.Create(linkedAttachment);

// 3. Send the email with SendEmailRequest method.
SendEmailRequest sendEmailRequest = new SendEmailRequest
{
    EmailId = targetEmailId,
    TrackingToken = "",
    IssueSend = true
};
SendEmailResponse sendEmailresp = (SendEmailResponse)serviceproxy.Execute(sendEmailRequest);

更多参考:

activityparty是一种特殊类型,您可以获取更多信息here

SendEmailRequest官方文档here