I am reasonably new to C#, but not .NET development. I am new to Graph development and don’t have a whole heap of JSON experience.
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. The Error Message is :
{"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.
- 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
toRecipients { get; set; } - public List
ccRecipients { get; set; } - public List
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; }
- }
- // Define our recipient list
- List
listRecip = new List (); // 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
ccRecip = new List (); // 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
bccRecip = new List (); // 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"
- };
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sachin SinghPosted Jan 12, 2021, 11:51 AM