Constructing Email Message using REST and C# - Invalid recipients I am reasonably new to C#, but not .NET development. I am also fairly new to Graph development.
I am trying to build the code that can be used to construct messages within my organisation. Ultimately the code will become an RPA code object.
I have a class for the message, which has classes defined for toRecipient, ccRecipient and bccRecipient. Creating and sending an email works fine when I have data for the To, CC and Bcc options. However, I get an error message if I only have To recipients and not cc or Bcc recipitents. Similarly I cannot send to bcc only, as the code is looking for values to the toRecipients and CC recipients.
Error message:
{ "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." } }
How do I make the sub classes for cc and BCC optional? – or is there syntax I can use to tell the API that cc and bcc fields are empty?
I build the class based on a message created using Postman, and then using Paste Special into my message class.
Class:
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; } }
Input variables:
public static string EmailRecipients = "email1@company.com";email2@company.com; public static string EmailCCList = "email3@organisation.org"; public static string EmailBCCList = "";
Code to build message:
// 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" };