Skip to content

Don't Include Empty Objects in JSON Request Body #297

@LorenDorez

Description

@LorenDorez

I switched from v2 to V3 and the API call to send appears to work but im not receiving any emails. The HTTP response comes back as bad request but everything looks good

 public async Task SendEmailAsync(IEnumerable<string> toAddresses, IEnumerable<string> bccAddresses, string fromAddress, string subject, string message, string[] attachments)
        {
            // Plug in your email service here to send an email.
            Mail myMessage = new Mail();

            Personalization personalization = new Personalization();
            foreach (string toAddress in toAddresses)
            {
                personalization.AddTo(new Email(toAddress));
            }

            if (bccAddresses != null && bccAddresses.Count() > 0)
            {
                foreach (string bccAddress in bccAddresses)
                {
                    personalization.AddBcc(new Email(bccAddress));
                }
            }

            myMessage.From = new Email(fromAddress);
            myMessage.Subject = subject;
            //myMessage.AddContent(new Content("text/plain", message));
            myMessage.AddContent(new Content("text/html", message));

            foreach (string filePath in attachments)
            {
                string fileContents = Convert.ToBase64String(File.ReadAllBytes(filePath));

                string contentType;
                new FileExtensionContentTypeProvider().TryGetContentType(filePath, out contentType);
                contentType = contentType ?? "application/octet-stream";

                Attachment attachment = new Attachment();
                attachment.Content = fileContents;
                attachment.Type = contentType;
                attachment.Filename = Path.GetFileName(filePath);
                attachment.Disposition = "attachment";
                //myMessage.AddAttachment(attachment);
            }


            //DEMO OVERRIDE
            if (_hostingEnvironment.IsDevelopment())
            {
                personalization.Tos = new List<Email> { new Email("[email protected]") };
                //personalization.Ccs.Clear();
                personalization.Bccs.Clear();
            }
            myMessage.AddPersonalization(personalization);

            // Create a Web transport for sending email.
            dynamic sg = new SendGridAPIClient(Options.SendGridApiKey);

            //return transportWeb.client.mail.send.postDeliverAsync(myMessage);
            dynamic response = await sg.client.mail.send.post(requestBody: myMessage.Get());

            var t1 = myMessage.Get();
            var str = "test";
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions