MS Graph Send Mail

This first example shows how to send an email from a Office 365 Mailbox that is protected by Modern Authentication. The email has a HTML body with a logo, and has a PDF attachment.
A WCF client is created to connect to the Jupiter Server. The MS Graph Message object that is hosted on the Jupiter Server requires a complex request object to be constructed. The PDF and the logo file must be encoded with base64 and the correct MIME types included in the message request.  

            Console.WriteLine("Send Mail");

            //Set up WCF client code
            MSGraph.Iv1_0Client client = new MSGraph.Iv1_0Client();
            (client.ChannelFactory.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
            (client.ChannelFactory.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
            (client.ChannelFactory.Endpoint.Binding as BasicHttpBinding).ReceiveTimeout = new TimeSpan(0, 10, 0);
            UriBuilder uri = new UriBuilder(client.ChannelFactory.Endpoint.Address.Uri);
            uri.Host = "localhost";
            client.ChannelFactory.Endpoint.Address = new EndpointAddress(uri.Uri);
            // Setup Objects
            MSGraph.SendMailRequest req = new MSGraph.SendMailRequest();            
            MSGraph.O365Message message = new MSGraph.O365Message();
            MSGraph.attachmentList attachment = new MSGraph.attachmentList();
            List<MSGraph.attachmentList> attList = new List<MSGraph.attachmentList>();
            MSGraph.ArrayOfAttachmentList arrOfAtt = new MSGraph.ArrayOfAttachmentList();
            MSGraph.emailAddressObj toAddress = new MSGraph.emailAddressObj();
            MSGraph.emailAddressList emaillist = new MSGraph.emailAddressList();
            List<MSGraph.emailAddressList> eal = new List<MSGraph.emailAddressList>();
            MSGraph.ArrayOfEmailAddressList arrOfEm = new MSGraph.ArrayOfEmailAddressList();
            MSGraph.Body body = new MSGraph.Body();

            // Load in Attachment
            attachment.odatatype = "#Microsoft.OutlookServices.FileAttachment";
   = "Attachment File.pdf";
            byte[] imageArray = System.IO.File.ReadAllBytes(;
            attachment.size = imageArray.Length;
            string attBytes = Convert.ToBase64String(imageArray);
            attachment.contentBytes = attBytes;            
            message.attachments = arrOfAtt;
            // Define who is sending the email, 
            // which also is the user that is authorised in Azure for the WCF server to connect as
            req.user = @"";

            // Save the email in the SentItems folder
            req.saveToSentItems = true;

            // Load in a logo for the signature
            imageArray = System.IO.File.ReadAllBytes("logo.jpg");            
            string base64Img = Convert.ToBase64String(imageArray);
            string imageStr = $"<img src=\"data:image/jpeg;base64,{base64Img}\">";

            // Setup the body of the email
            body.content = "<strong>Hello</strong><p>Here is your report</p>" + imageStr;
            body.contentType = "html";
            message.body = body;

            // Setup the Subject field of the email
            message.subject = "Report";

            // Define the importance of the email
            message.importance = "Normal";

            // Declare that the email has attachments
            message.hasAttachments = true;

            // Define who the email is being sent to
            toAddress.address = "";
            emaillist.emailAddress = toAddress;
            message.toRecipients = arrOfEm;

            // Load the message into the Request object. 
            req.message = message;

            // Send the email message to the WCF server
            MSGraph.SendMailResponse response = client.SendMail(req);

            // Capture and display the status code to see if the email was Accepted or Rejected
            Console.WriteLine($"Response = {response.statusCode.ToString()}");