Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29762

How to attach a file as an attachment in email using SendGrid?

$
0
0

I am wanting to attach a file as an attachment using SendGrid and C# - I have the code below which runs but the response.StatusCode that is returned is

BadResponse

How do I alter this code so that the file is attached and an email sent successfully?

var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var listAtta = new List<FileAttachment>();
emailProducts.Select(o => o.tp).ToList().ForEach(o =>
{
    string file = o.ProductPdf;
    var fileBytes = FileToByteArray(o.ProductPdf);
    if (fileBytes != null && fileBytes.Count() > 0)
    {
        listAtta.Add(new FileAttachment
        {
            FileData = fileBytes,
            FileName = o.ProductPdf
        }); ;
    }
    msg.AddAttachment(o.ProductPdf, fileBytes.ToString());
});

var response = await client.SendEmailAsync(msg);
var success = response.StatusCode;

Viewing all articles
Browse latest Browse all 29762

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>