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"
}
};
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!
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!