c# - 没有 cc 或 bcc 收件人时的 Microsoft Graph API 问题
问题描述
使用 REST 和 C# 构建电子邮件消息 - 无效的收件人 我对 C# 相当陌生,但不是 .NET 开发。我对 Graph 开发也很陌生。
我正在尝试构建可用于在我的组织内构建消息的代码。最终代码将成为 RPA 代码对象。
我有一个消息类,它具有为 toRecipient、ccRecipient 和 bccRecipient 定义的类。当我有 To、CC 和 Bcc 选项的数据时,创建和发送电子邮件工作正常。但是,如果我只有收件人而不是抄送或密件抄送收件人,我会收到一条错误消息。同样,我不能只发送到密件抄送,因为代码正在寻找 toRecipients 和 CC 收件人的值。
错误信息:
{ "error":{ "code":"ErrorInvalidRecipients", "message":"At least one recipient isn't valid., Recipient '' is not resolved. All recipients must be resolved before a message can be submitted." } }
如何使 cc 和 BCC 的子类可选?– 或者有没有我可以用来告诉 API cc 和 bcc 字段为空的语法?
我基于使用 Postman 创建的消息构建类,然后在我的消息类中使用选择性粘贴。
班级:
public class gMessage { public grfMessage message { get; set; } public string saveToSentItems { get; set; } } public class grfMessage { public string subject { get; set; } public Body body { get; set; } public List <ToRecipient> toRecipients { get; set; } public List<ToRecipient> ccRecipients { get; set; } public List<ToRecipient> bccRecipients { get; set; } } public class Body { public string contentType { get; set; } public string content { get; set; } } public class ToRecipient { public EmailAddress EmailAddress { get; set; } public static implicit operator List<object>(ToRecipient v) { throw new NotImplementedException(); } } public class EmailAddress { public string address { get; set; } }
输入变量:
public static string EmailRecipients = "email1@company.com";email2@company.com; public static string EmailCCList = "email3@organisation.org"; public static string EmailBCCList = "";
构建消息的代码:
// Define our recipient list List<ToRecipient> listRecip = new List<ToRecipient>(); // define new list. We will ultimately split the inbount array into this list ToRecipient RecipEmail; EmailAddress ema1; // now split our list of recipients to an array string[] ToAdds = EmailRecipients.Split(';'); foreach (var email in ToAdds) { // Build the recipients list first RecipEmail = new ToRecipient(); ema1 = new EmailAddress { address = email }; // Email Address class contains Address RecipEmail.EmailAddress = ema1; listRecip.Add(RecipEmail); // We have now added to the list in memory } //Define our CC List List<ToRecipient> ccRecip = new List<ToRecipient>(); // define new list. We will ultimately split the inbount array into this list ToRecipient ccEmail; string[] ccAdds = EmailCCList.Split(';'); foreach (var email in ccAdds) { // Build the recipients list first ccEmail = new ToRecipient(); ema1 = new EmailAddress { address = email }; // Email Address class contains Address ccEmail.EmailAddress = ema1; ccRecip.Add(ccEmail); // We have now added to the list in memory } //Define our BCC List List<ToRecipient> bccRecip = new List<ToRecipient>(); // define new list. We will ultimately split the inbount array into this list ToRecipient bccEmail; string[] bccAdds = EmailBCCList.Split(';'); foreach (var email in bccAdds) { // Build the recipients list first bccEmail = new ToRecipient(); ema1 = new EmailAddress { address = email }; // Email Address class contains Address bccEmail.EmailAddress = ema1; bccRecip.Add(bccEmail); // We have now added to the list in memory } var msgData = new gMessage() { message = new grfMessage() { subject = mSubject, body = new Body() { contentType = mContentType, content = mBody }, toRecipients = listRecip, ccRecipients = ccRecip, bccRecipients = bccRecip }, saveToSentItems = "True" };