How to convert PDF and WORD to base64encoded and send to email as attachments?

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[0].FileName; index = files.FileName.IndexOf('.'); files.FileType = files.FileName.Substring(index); System.IO.Stream fs = Request.Files[0].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);

Answer Wiki

Thanks. We'll let you know when a new response is added.

I did not see anything on their site that said attachments had to be converted tp base64encode. Can you provide the reason you are converting it  Either that or look into the add-on / plugin to go with it Digioh 

Discuss This Question: 3  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • TheRealRaven
    Text files don't need to be encoded simply because they consist only of "text". Non-text files must be encoded because SMTP only transfers "text" data; non-text is likely to contain byte sequences that are mistaken as SMTP control sequences.

    You shouldn't have to do any encoding. SendGrid should do it automatically, like practically every other recent e-mail client in the world.

    Is there an indication or instruction from them that you need to do it for them? (That would be a fairly ridiculous burden for customers.)
    36,340 pointsBadges:
  • ToddN2000
    I have to agree with Raven. If you are using a mail service that requires you to convert attachment in order to send them, Besides the fact, what does the email recipient have to do to view the document properly, like in the original PDF or DOC format? If you are forced to convert them,  I would look for another service.  There are numerous ones out there. Does this email program you are using offer any special feature that the other do not? It may help us better understand your dilemma.
    135,305 pointsBadges:
  • Subhendu Sen
    Is that email service you are using provides any addon or something like that?
    141,290 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: