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

发布于 2025-01-20 16:20:20 字数 66 浏览 6 评论 0原文

我想创建一个电子邮件记录,并将其标记为“接收”,并且可以在此新电子邮件记录中添加附件。有人知道如何使用C#做到这一点?

I want to create an email record, and will it mark as 'Received', and may be added attachment to this new email record. Anybody know how to do that using C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2025-01-27 16:20:20

有一个 官方示例代码但没有附件包括在内,所以我将在这里使用我的代码:

共有三个步骤:

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

这是示例代码:

// 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"] = "[email protected]";
Guid contactid = new Guid();
toparty["partyid"] = new EntityReference("contact", contactid);
newEmail["to"] = new Entity[] { toparty };

Entity fromparty = new Entity("activityparty");
fromparty["addressused"] = "[email protected]";
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 是一种特殊类型,您可以获取更多信息 此处

SendEmailRequest 官方文档 此处

There is an Official example code but no attachment included, so I will use my code here:

There are three steps:

  1. Create an email record.
  2. Create attachment records for email.
  3. Send this email with SendEmailRequest

Here is the sample code:

// 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"] = "[email protected]";
Guid contactid = new Guid();
toparty["partyid"] = new EntityReference("contact", contactid);
newEmail["to"] = new Entity[] { toparty };

Entity fromparty = new Entity("activityparty");
fromparty["addressused"] = "[email protected]";
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);

More ref:

activityparty is a special type, you can get more info here

SendEmailRequest official document here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文