While file uploading how to convert PDF & WORD to base64encoded and send to email as attachments using sendgrid?
I would like to upload any text file and send it to email as an attachment by using "Sendgrid". I am getting email and attachments too but couldn't open pdf and word files, and when I trying to open it there shows an error : "Whoops! There was a problem while previewing this document. " whereas .text file types can open and view as is it.
below I am creating sendgrid mail, and converting a file into base64 and adding it into AddAttachment of sendgrid.
string body = EmailBody(taskRecord.Subject, customer.CustName, User.Identity.Name);
var client = new SendGridClient("APIKey");
var from = new EmailAddress("sender", "The SaharaCRM");
var to = new EmailAddress("reciepient", customer.CustName);
var htmlContent = body;
var msg = MailHelper.CreateSingleEmail(from, to, taskRecord.Subject, "", htmlContent);
//converting file to base64
files.FileName = Request.Files.FileName;
index = files.FileName.IndexOf('.');
files.FileType = files.FileName.Substring(index);
System.IO.Stream fs = Request.Files.InputStream;
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte bytes = br.ReadBytes((Int32)fs.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
//adding converted base64 file input stream to AddAttachment and sending.
msg.AddAttachment(files.FileName, base64String, files.FileType, "attachment", "banner");
var some = client.SendEmailAsync(msg);