developer
developer

Reputation: 11

How to send base64 image in attachment of email using base64 link?

I have base64 image url and i want to send this image as email attachment but its not working.Throwing error PathTooLongException

My code :

   System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(Base64urlpath);
   attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
   myMail.Attachments.Add(attachment);

Please answer me in system.web.Mail.

Thanks,

Upvotes: 1

Views: 5229

Answers (1)

germi
germi

Reputation: 4668

The constructor you are using is not accepting base64 input, but needs a path to a file:

public Attachment (string fileName);

Parameters

fileName String

A String that contains a file path to use to create this attachment.

(quoted from the documentation). And because your encoded image is longer than 260 characters, you get the exception that the path is too long.

It seems like one of the constructors accepting a Stream might be what you're looking for.

One possibility to convert your base64 encoded image to a stream is to create a MemoryStream out of it:

var imageBytes = Convert.FromBase64String(Base64urlpath);
using var stream = new MemoryStream(imageBytes);
var attachment = new System.Net.Mail.Attachment(stream, null); // you may want to provide a name here

Upvotes: 1

Related Questions