jeffkenn
jeffkenn

Reputation: 201

Attaching to sendgrid api not working when using sendgrid mail helper

I have looked through the questions i can find on here about attaching a file to a sendgrid email but none seem to have the issue I am.

My question is is this. How do you send an email with an attachment in sendgrid using the api?

 dynamic sg = new SendGridAPIClient(apiKey);
        var from = new SendGrid.Helpers.Mail.Email("[email protected]");
        var subject = "Hello World from the SendGrid C# Library!";
        var to = new SendGrid.Helpers.Mail.Email(toAddress);
        var content = new Content("multipart/form-data", "Textual content");
        var attachment = new Attachment {Filename = attachmentPath };
        var mail = new Mail(from, subject, to, content);

        var ret = mail.Get();

        mail.AddAttachment(attachment);


        dynamic response = await sg.client.mail.send.post(requestBody: ret);

If i put the mail.attachment after the get the mail sends but there is no attachment. If i put the addattachment line before the get i get a "bad request" message.

I have yet to find an example of exactly how to do this.

Also, the path to the file is c:\tblaccudatacounts.csv

Upvotes: 2

Views: 5538

Answers (2)

DStage31
DStage31

Reputation: 56

After struggling with this for a couple hours, I found an answer using sendgrid's V3 API. Here's what I learned.

In your example, you call var ret = mail.Get(); before adding the attachment. Since mail.Get() is essentially serializing the mail object into the Json format SendGrid is expecting, adding the attachment after the mail.Get() call will not actually add it to the mail object.

The other thing you should know is that the API doesn't have a way of simply taking the file path as an input (At least that I can find, I hope someone can correct me). You need to manually set at least the content (as a base 64 string) and filename. You can find more information here.

Here is my working solution:

string apiKey = "your API Key";
dynamic sg = new SendGridAPIClient(apiKey);

Email from = new Email("[email protected]");
string subject = "Hello World from the SendGrid CSharp Library!";
Email to = new Email("[email protected]");
Content body = new Content("text/plain", "Hello, Email!");
Mail mail = new Mail(from, subject, to, body);

byte[] bytes = File.ReadAllBytes("C:/dev/datafiles/testData.txt");
string fileContentsAsBase64 = Convert.ToBase64String(bytes);

var attachment = new Attachment
{
    Filename = "YourFile.txt",
    Type = "txt/plain",
    Content = fileContentsAsBase64
};

mail.AddAttachment(attachment);

dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());

Upvotes: 3

jeffkenn
jeffkenn

Reputation: 201

I figure it out. I was using a helper written by a third party. I went with what with SendGrid actually suggested. See code below that is now working.

var myMessage = new SendGridMessage {From = new MailAddress("[email protected]")};
        myMessage.AddTo("Jeff Kennedy <[email protected]>");
        myMessage.Subject = "test email";
        myMessage.Html = "<p>See Attachedment for Lead</p>";
        myMessage.Text = "Hello World plain text!";
        myMessage.AddAttachment("C:\\tblaccudatacounts.csv");
        var apiKey = "apikey given by sendgrid";
        var transportWeb = new Web(apiKey);
        await transportWeb.DeliverAsync(myMessage);

Upvotes: 0

Related Questions