Sendgrid-csharp: C#, How to use SendGrid to send an email and attach a html page as attachment

Created on 12 Nov 2018  路  4Comments  路  Source: sendgrid/sendgrid-csharp

Issue Summary

I just wonder how to use SendGrid C# SDK for sending an email and attach a HTML page as the attachment.

Do I need to create the HTML page and save it first before I send it or can I just save it in the memory? I prefer the second option just like my current example below, here I'm attaching a text file without save it to the source code.

                var msg = MailHelper.CreateSingleEmail(from, to, subject, null, content);
                // Add attachment as txt/plain
                byte[] byteData = Encoding.ASCII.GetBytes(transcript);
                msg.Attachments = new List<SendGrid.Helpers.Mail.Attachment>
                {
                    new SendGrid.Helpers.Mail.Attachment
                    {
                        Content = Convert.ToBase64String(byteData),
                        Filename = "Transcript.txt",
                        Type = "txt/plain",
                        Disposition = "attachment"
                    }
                };
  • sendgrid-csharp Version: master (latest commit: [commit number])
  • Platform (ASP.NET)
  • .NET Version: 4.5.2
unknown or a question

Most helpful comment

Hello @CalvinFengDatacom

Thanks so much for your sample codes. This one works great. I been Googling for any examples that may works until your sample. Thanks so much!

           var msg = MailHelper.CreateSingleEmail(from, to, subject, null, content);
            // Add attachment as txt/plain

            byte[] byteData = Encoding.ASCII.GetBytes(File.ReadAllText(filePath));
            msg.Attachments = new List<SendGrid.Helpers.Mail.Attachment>
            {
                new SendGrid.Helpers.Mail.Attachment
                {
                    Content = Convert.ToBase64String(byteData),
                    Filename = "Transcript.txt",
                    Type = "txt/plain",
                    Disposition = "attachment"
                }
            };

All 4 comments

Hello @CalvinFengDatacom,

Here is an example of both scenarios. I hope that helps!

With Best Regards,

Elmer

@thinkingserious Thanks Elmer, I found the solution as well. Thanks for sharing.

Hello @CalvinFengDatacom

Thanks so much for your sample codes. This one works great. I been Googling for any examples that may works until your sample. Thanks so much!

           var msg = MailHelper.CreateSingleEmail(from, to, subject, null, content);
            // Add attachment as txt/plain

            byte[] byteData = Encoding.ASCII.GetBytes(File.ReadAllText(filePath));
            msg.Attachments = new List<SendGrid.Helpers.Mail.Attachment>
            {
                new SendGrid.Helpers.Mail.Attachment
                {
                    Content = Convert.ToBase64String(byteData),
                    Filename = "Transcript.txt",
                    Type = "txt/plain",
                    Disposition = "attachment"
                }
            };

I utilized this also. Thank you!

Was this page helpful?
0 / 5 - 0 ratings