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.
This class is as follows;
- 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; }
- }
The Code to build the message is;
-
- List<ToRecipient> listRecip = new List<ToRecipient>();
- ToRecipient RecipEmail;
- EmailAddress ema1;
-
- string[] ToAdds = EmailRecipients.Split(';');
- foreach (var email in ToAdds)
- {
-
- RecipEmail = new ToRecipient();
- ema1 = new EmailAddress
- {
- address = email
- };
- RecipEmail.EmailAddress = ema1;
- listRecip.Add(RecipEmail);
- }
-
- List<ToRecipient> ccRecip = new List<ToRecipient>();
- ToRecipient ccEmail;
- string[] ccAdds = EmailCCList.Split(';');
- foreach (var email in ccAdds)
- {
-
- ccEmail = new ToRecipient();
- ema1 = new EmailAddress
- {
- address = email
- };
- ccEmail.EmailAddress = ema1;
- ccRecip.Add(ccEmail);
- }
-
- List<ToRecipient> bccRecip = new List<ToRecipient>();
- ToRecipient bccEmail;
- string[] bccAdds = EmailBCCList.Split(';');
- foreach (var email in bccAdds)
- {
-
- bccEmail = new ToRecipient();
- ema1 = new EmailAddress
- {
- address = email
- };
- bccEmail.EmailAddress = ema1;
- bccRecip.Add(bccEmail);
- }
-
- var msgData = new gMessage()
- {
- message = new grfMessage()
- {
- subject = mSubject,
- body = new Body()
- {
- contentType = mContentType,
- content = mBody
- },
- toRecipients = listRecip,
- ccRecipients = ccRecip,
- bccRecipients = bccRecip
- },
- saveToSentItems = "True"
- };
Any help is much appreciated!
Thank you!