Dynamics 365 Web API电子邮件发送 [英] Dynamics 365 Web API Email send

查看:168
本文介绍了Dynamics 365 Web API电子邮件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为网站上的页面建立订阅.因此,将来的订阅者可以通过表单发布并添加到Dynamics 365 Online中的营销列表中.

I'm building a subscription for pages at a website. So a to-be subscriber post via a form and get added to a marketing list in Dynamics 365 Online.

然后从网站上的预定工作中,我请求营销列表中的联系人.

From a scheduled job at the website i then make a request of the contacts in a marketing list.

然后,我需要向他们发送一封电子邮件,说明已经使用此属性创建了一个新页面,并链接到该页面.

Then i need to send them an email that a new page have been created with this properties and a link to that page.

所以我想把这个责任变成Dynamics 365 Online.

So i would like to make that responsibility Dynamics 365 Online.

因此,我正在使用Web API和操作:SendEmailFromTemplate 不知道我是否可以使用此操作,或者是否需要创建自定义操作.

So im using the Web API and the action: SendEmailFromTemplate Not sure if i can use this action or if i need to create a custom action.

我需要传递诸如页面URL,标题和文本之类的数据.或者只是一个字符串-包含所有这些的电子邮件正文.

I would need to pass data such as URL of the page, title and Text. Or just one string - the body of the e-mail message that contains all of this.

所以我在CRM上创建了一个电子邮件模板,就像代码找到了一样!

So i have created an email template at the CRM and it semse like the code finds it!

这是我想采取的行动: https://msdn.microsoft.com/en-us/library/mt607523. aspx

This is the action i think i'm after: https://msdn.microsoft.com/en-us/library/mt607523.aspx

如果我在SOAP Service上查看文档,则实际上有一些示例: https://msdn.microsoft.com/zh-CN/library/microsoft.crm.sdk.messages.sendemailfromtemplaterequest.aspx?cs-save-lang=1&amp

If i check the documentation at the SOAP Service there is actually examples: https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.sendemailfromtemplaterequest.aspx?cs-save-lang=1&amp

但是我要使用Web API.

But i'm going with the Web API.

所以我尝试了这个:

dynamic regarding = new ExpandoObject();
var oppIndexer = regarding as IDictionary<string, Object>;
oppIndexer["contactid"] = contact.ContactId; //contact that will recive the 
email.
oppIndexer["@odata.type"] = "Microsoft.Dynamics.CRM.contact";

dynamic target = new ExpandoObject();
var targetIndexer = target as IDictionary<string, Object>;
targetIndexer["torecipients"] = "myemail@outlook.com";
targetIndexer["sender"] = "mysecondemail@businessname.com";
targetIndexer["inreplyto"] = "mysecondemail@businessname.com";
targetIndexer["subject"] = "This is the subject";
targetIndexer["description"] = "This should be the body of the email";
targetIndexer["directioncode"] = true; //outgoing
targetIndexer["@odata.type"] = "Microsoft.Dynamics.CRM.email";

dynamic sendEmailFromTemplate = new ExpandoObject();
sendEmailFromTemplate.TemplateId = Guid.Parse("my-email-template-guid-inserted-here");
sendEmailFromTemplate.Regarding = regarding;
sendEmailFromTemplate.Target = target;

api.ExecuteAction("SendEmailFromTemplate", sendEmailFromTemplate);

但是我得到一个例外:我在发送电子邮件时必须指出:该电子邮件必须至少具有一个收件人,然后才能发送".

But i get exception: "The e-mail must have at least one recipient before it can be sent" when i post it.

可能是什么问题?

我认为文档位于: https://msdn.microsoft.com/en-us/library/mt607523. aspx

只需声明目标和关于的类型应为"crmbaseentity"类型.基类...

Just declare that the type for target and regarding should be of type "crmbaseentity". A base class...

有人知道这可能是什么原因吗?

Anyone know what might cause this?

这是电子邮件实体: https://msdn.microsoft.com/en-us/library/mt608007. aspx

This is the Email Entity: https://msdn.microsoft.com/en-us/library/mt608007.aspx

对于收件人"字段,可能显示为:显示与收件人相对应的电子邮件地址".所以我不确定这是我应该使用的字段.如果文本为带有"的收件人,则将接收消息的分隔符",我将更加确定我将不得不使用此字段.

For the field "torecipients" one could read: "Shows the email addresses corresponding to the recipients". So i'm not sure this is the field i should use. If the text would be "The recipients with "," separator that will recive the message" i would be more certain that i would have to use this field.

更新: Okey,所以这是您发布的内容:

Update: Okey, so this is what you posted:

{
 "email_activity_parties": [ 
    { "partyid_systemuser@odata.bind": "/systemusers(852a441c-b544-e611-80e3-c4346bc5e750)", "participationtypemask": 1 }, 
    { "partyid_systemuser@odata.bind": "/systemusers(852a441c-b544-e611-80e3-c4346bc5e750)", "participationtypemask": 2 }
  ],
 "description": "description lorem ipsum",
 "subject": "rubrik",
 "regardingobjectid_opportunity_email@odata.bind": "/opportunities(e9e6eb64-9c4c-e611-80e4-c4346bc58294)"
}

如果我要更改此设置,使其适合我的需要.我不认为即时通讯会利用机会.还是我必须? SendEmailFromTemplate操作有三个参数: https://msdn.microsoft.com/zh-cn/library/mt607523.aspx TemplateId,关于和目标.我在下面(未经测试)更改了此代码

If i would to change this so it suits my need. I dont think im going to be using opportunities. Or must i? There are three parameters for SendEmailFromTemplate Action: https://msdn.microsoft.com/en-us/library/mt607523.aspx TemplateId, Regarding and Target. I changed this code a bit below (untested)

{
 "TemplateId": "id-for-email-template",
 "Regarding": [ { "contactid@odata.bind": "/contacts(contact-guid-X)" } ],
 "Target": {
     "email_activity_parties": [ 
        { "partyid_systemuser@odata.bind": "/systemusers(systemuser-guid-Y)", "participationtypemask": 1 }, //1 is "sender"
        { "contactid@odata.bind": "/contacts(contact-guid-X)", "participationtypemask": 2 } // 2 is "to"
      ],
     "description": "description lorem ipsum",
     "subject": "rubrik",
     "contactid@odata.bind": "/contacts(contact-guid-X)"
    }
}

我会尝试类似的东西.

推荐答案

电子邮件是一项活动,所有活动都将分为活动指针+活动方".

Email is an activity, all activities will be split into Activity Pointer + Activity Party. What you have to do is put the same contact you are using for regardingobject into toParty as well.

//create activityparty
 Entity Fromparty = new Entity("activityparty");
 Entity Toparty = new Entity("activityparty");

//To set to Contact
Toparty["partyid"]= new EntityReference("contact", _ contactid));

//From set to User
Fromparty["partyid"]= new EntityReference("systemuser", _From));

//create email Object and set attributes
Entity email = new Entity("email");

email["from"] = new Entity[] { Fromparty };
email["to"] = new Entity[] { Toparty };
email["directioncode"] = true;

//setting the Regarding as Contact
email["regardingobjectid"] = new EntityReference("contact", _contactid);

错误The e-mail must have at least one recipient before it can be sent将消失.

更新:

我们必须使用基于Web api的导航属性.

We have to use navigation property in terms of web api.

//Create email
var email = new JObject(
           new JProperty("email_activity_parties",
             new JArray(
                  new JObject(
                          new JProperty("partyid_systemuser@odata.bind", "/systemusers (<guid>)"),
          new JProperty("participationtypemask", 1)),
                     new JObject(
                           new JProperty("partyid_systemuser@odata.bind", "/systemusers(<guid>)"),
                   new JProperty("participationtypemask", 2)))),
                  new JProperty("description", txtEmail.Text.Replace("\r\n","<br>")),
                  new JProperty("subject", "Test Subject"),
                  new JProperty("regardingobjectid_opportunity_email@odata.bind", "/opportunities(<guid>)"));

这篇关于Dynamics 365 Web API电子邮件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆