Microsoft Graph SDK 在为用户发送草稿邮件时出现 ErrorInvalidRecipients。

huangapple go评论76阅读模式
英文:

Microsoft Graph SDK ErrorInvalidRecipients when sending draft email for user

问题

我正在尝试使用Microsoft Graph SDK 5.13.0为用户发送电子邮件。我收到了"ErrorInvalidRecipients"错误消息 - "至少有一个收件人无效。不能发送消息,因为它不包含任何收件人。"

这是我的代码:

var graphMessage = new Message
{
  Subject = "Test",
  Body = new ItemBody
  {
    ContentType = BodyType.Text,
    Content = "Test"
  },
  ToRecipients = new List<Recipient>
  {
    new Recipient
    {
      EmailAddress = new EmailAddress
      {
        Address = "***@***.com"
      }
    }
  }
};

// 创建草稿以添加大附件
var draftMessage = await client.Users["no-reply@***.com"].Messages.PostAsync(graphMessage);

// 附件将添加到 draftMessage

var body = new SendMailPostRequestBody
{
  Message = draftMessage
};

// 产生"ErrorInvalidRecipients"错误代码
await client.Users["no-reply@***.com"].SendMail.PostAsync(body);

希望这有助于你解决问题。

英文:

I am trying to send an email for a user using Microsoft Graph SDK 5.13.0. I am receiving "ErrorInvalidRecipients" - "At least one recipient is not valid., A message can't be sent because it contains no recipients".

Here's my code:

var graphMessage = new Message
{
  Subject = &quot;Test&quot;,
  Body = new ItemBody
  {
    ContentType = BodyType.Text,
    Content = &quot;Test&quot;
  },
  ToRecipients = new List&lt;Recipient&gt;
  {
    new Recipient
    {
      EmailAddress = new EmailAddress
      {
        Address = &quot;***@***.com&quot;
      }
    }
  }
};

// Create draft to be able to add large attachments
var draftMessage = await client.Users[&quot;no-reply@***.com&quot;].Messages.PostAsync(graphMessage);

// Attachments would be added to draftMessage

var body = new SendMailPostRequestBody
{
  Message = draftMessage
};

// Produces &quot;ErrorInvalidRecipients&quot; error code
await client.Users[&quot;no-reply@***.com&quot;].SendMail.PostAsync(body);

答案1

得分: 0

I didn't find anything in the documentation that mentions this yet but it turns out the error was a result of using:

我在文档中没有找到提到这一点的内容,但事实证明错误是由于使用以下方式引起的:

await client.Users["no-reply@***.com"].SendMail.PostAsync(body);

正确的方法是使用:

await client.Users["no-reply@***.com"].Messages[body.Message.Id].Send.PostAsync();

Since I was trying to send a draft message, SendMail was not working correctly (or wasn't designed for drafts). SendMail works correctly if I don't create drafts.

由于我尝试发送一封草稿邮件,SendMail 并没有正常工作(或者并不适用于草稿)。如果我不创建草稿,SendMail 就可以正常工作。

英文:

I didn't find anything in the documentation that mentions this yet but it turns out the error was a result of using:

await client.Users[&quot;no-reply@***.com&quot;].SendMail.PostAsync(body);

The correct way is to use:

await client.Users[&quot;no-reply@***.com&quot;].Messages[body.Message.Id].Send.PostAsync();

Since I was trying to send a draft message, SendMail was not working correctly (or wasn't designed for drafts). SendMail works correctly if I don't create drafts.

huangapple
  • 本文由 发表于 2023年6月13日 00:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定