没有抄送或密件抄送收件人时的 Microsoft Graph API 问题

Der*_*908 5 .net c# email rest

使用 REST 和 C# 构造电子邮件 - 无效收件人\n我对 C# 相当陌生,但对 .NET 开发不太熟悉。我对图形开发也相当陌生。

\n

我正在尝试构建可用于在我的组织内构建消息的代码。最终代码将成为 RPA 代码对象。

\n

我有一个消息类,其中有为 toRecipient、ccRecipient 和 bccRecipient 定义的类。当我有“收件人”、“抄送”和“密件抄送”选项的数据时,创建和发送电子邮件工作正常。但是,如果我只有“收件人”收件人而没有“抄送”或“密件抄送”收件人,我会收到一条错误消息。同样,我不能仅发送到密件抄送,因为代码正在寻找 toRecipients 和抄送收件人的值。

\n
\n

错误信息:

\n
{\n    "error":{\n        "code":"ErrorInvalidRecipients",\n        "message":"At least one recipient isn't valid., Recipient '' is not resolved. All recipients must be resolved before a message can be submitted."\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

如何使抄送和密件抄送的子类成为可选?\xe2\x80\x93 或者是否有语法可以用来告诉 API cc 和 bcc 字段为空?

\n

我基于使用 Postman 创建的消息构建该类,然后使用“选择性粘贴”到我的消息类中。

\n
\n

班级:

\n
public class gMessage {\n    public grfMessage message { get; set; }\n    public string saveToSentItems { get; set; }\n}\n\npublic class grfMessage {\n    public string subject { get; set; }\n    public Body body { get; set; }\n    public List <ToRecipient> toRecipients { get; set; }\n    public List<ToRecipient> ccRecipients { get; set; }\n    public List<ToRecipient> bccRecipients { get; set; }\n}\n\npublic class Body {\n    public string contentType { get; set; }\n    public string content { get; set; }\n}\n\npublic class ToRecipient {\n    public EmailAddress EmailAddress { get; set; }\n    public static implicit operator List<object>(ToRecipient v) {\n        throw new NotImplementedException();\n    }\n}\n\npublic class EmailAddress {\n    public string address { get; set; }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n
\n

输入变量:

\n
public static string EmailRecipients = "email1@company.com";email2@company.com;\npublic static string EmailCCList = "email3@organisation.org";\npublic static string EmailBCCList = "";\n
Run Code Online (Sandbox Code Playgroud)\n
\n
\n

构建消息的代码:

\n
// Define our recipient list \nList<ToRecipient> listRecip = new List<ToRecipient>(); // define new list. We will ultimately split the inbount array into this list\nToRecipient RecipEmail;\nEmailAddress ema1;\n\n// now split our list of recipients to an array\nstring[] ToAdds = EmailRecipients.Split(';');\nforeach (var email in ToAdds) {\n    // Build the recipients list first\n    RecipEmail = new ToRecipient();\n    ema1 = new EmailAddress {\n        address = email\n    }; // Email Address class contains Address\n    RecipEmail.EmailAddress = ema1;\n    listRecip.Add(RecipEmail); // We have now added to the list in memory\n}\n\n//Define our CC List\nList<ToRecipient> ccRecip = new List<ToRecipient>(); // define new list. We will ultimately split the inbount array into this list\nToRecipient ccEmail;\n\nstring[] ccAdds = EmailCCList.Split(';');\nforeach (var email in ccAdds) {\n    // Build the recipients list first\n    ccEmail = new ToRecipient();\n    ema1 = new EmailAddress {\n        address = email\n    }; // Email Address class contains Address\n    ccEmail.EmailAddress = ema1;\n    ccRecip.Add(ccEmail); // We have now added to the list in memory\n}\n\n//Define our BCC List\nList<ToRecipient> bccRecip = new List<ToRecipient>(); // define new list. We will ultimately split the inbount array into this list\nToRecipient bccEmail;\n\nstring[] bccAdds = EmailBCCList.Split(';');\nforeach (var email in bccAdds) {\n    // Build the recipients list first\n    bccEmail = new ToRecipient();\n    ema1 = new EmailAddress {\n        address = email\n    }; // Email Address class contains Address\n    bccEmail.EmailAddress = ema1;\n    bccRecip.Add(bccEmail); // We have now added to the list in memory\n}\n\n\nvar msgData = new gMessage() {\n    message = new grfMessage() {\n        subject = mSubject,\n        body = new Body() {\n            contentType = mContentType,\n            content = mBody\n        },\n        toRecipients = listRecip,\n        ccRecipients = ccRecip,\n        bccRecipients = bccRecip\n    },\n    saveToSentItems = "True"\n};\n
Run Code Online (Sandbox Code Playgroud)\n
\n