Dermot Carroll

Dermot Carroll

  • NA
  • 21
  • 1.1k

issues with To Bcc and CC when building an email message using Graph

Jan 12 2021 11:04 AM

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;
  1. public class gMessage  
  2. {  
  3.     public grfMessage message { getset; }  
  4.     public string saveToSentItems { getset; }  
  5. }  
  6.   
  7. public class grfMessage  
  8. {  
  9.     public string subject { getset; }  
  10.     public Body body { getset; }  
  11.     public List <ToRecipient> toRecipients { getset; }  
  12.     public List<ToRecipient> ccRecipients { getset; }  
  13.     public List<ToRecipient> bccRecipients { getset; }  
  14. }  
  15.   
  16. public class Body  
  17. {  
  18.     public string contentType { getset; }  
  19.     public string content { getset; }  
  20. }  
  21.   
  22. public class ToRecipient  
  23. {  
  24.     public EmailAddress EmailAddress { getset; }  
  25.     public static implicit operator List<object>(ToRecipient v)  
  26.     {  
  27.         throw new NotImplementedException();  
  28.     }  
  29. }  
  30. public class EmailAddress  
  31. {  
  32.     public string address { getset; }  
  33. }  
The Code to build the message is;
  1. // Define our recipient list   
  2. List<ToRecipient> listRecip = new List<ToRecipient>(); // define new list. We will ultimately split the inbount array into this list  
  3. ToRecipient RecipEmail;  
  4. EmailAddress ema1;  
  5. // now split our list of recipients to an array  
  6. string[] ToAdds = EmailRecipients.Split(';');  
  7. foreach (var email in ToAdds)  
  8. {  
  9.     // Build the recipients list first  
  10.     RecipEmail = new ToRecipient();  
  11.     ema1 = new EmailAddress  
  12.     {  
  13.         address = email  
  14.     }; // Email Address class contains Address  
  15.     RecipEmail.EmailAddress = ema1;  
  16.     listRecip.Add(RecipEmail); // We have now added to the list in memory  
  17. }  
  18. //Define our CC List  
  19. List<ToRecipient> ccRecip = new List<ToRecipient>(); // define new list. We will ultimately split the inbount array into this list  
  20. ToRecipient ccEmail;  
  21. string[] ccAdds = EmailCCList.Split(';');  
  22. foreach (var email in ccAdds)  
  23. {  
  24.     // Build the recipients list first  
  25.     ccEmail = new ToRecipient();  
  26.     ema1 = new EmailAddress  
  27.     {  
  28.          address = email  
  29.     }; // Email Address class contains Address  
  30.     ccEmail.EmailAddress = ema1;  
  31.     ccRecip.Add(ccEmail); // We have now added to the list in memory  
  32. }  
  33. //Define our BCC List  
  34. List<ToRecipient> bccRecip = new List<ToRecipient>(); // define new list. We will ultimately split the inbount array into this list  
  35. ToRecipient bccEmail;  
  36. string[] bccAdds = EmailBCCList.Split(';');  
  37. foreach (var email in bccAdds)  
  38. {  
  39.     // Build the recipients list first  
  40.     bccEmail = new ToRecipient();  
  41.     ema1 = new EmailAddress  
  42.     {  
  43.         address = email  
  44.     }; // Email Address class contains Address  
  45.     bccEmail.EmailAddress = ema1;  
  46.     bccRecip.Add(bccEmail); // We have now added to the list in memory  
  47. }  
  48.   
  49. var msgData = new gMessage()  
  50. {  
  51.     message = new grfMessage()  
  52.     {  
  53.         subject = mSubject,  
  54.         body = new Body()  
  55.         {  
  56.            contentType = mContentType,  
  57.            content = mBody  
  58.         },  
  59.         toRecipients = listRecip,  
  60.         ccRecipients = ccRecip,  
  61.         bccRecipients = bccRecip  
  62.     },  
  63.     saveToSentItems = "True"  
  64. };  
Any help is much appreciated!
 
Thank you! 

Answers (1)