Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
4.5k views
in Programming by 66 69 85
edited by

I have a C# application that sends emails to users and I need to add an image as an attachment in an email using C#.

I have formatted the image to be in Base64 format. but the attachment in the email that was sent is incorrect, and I got an error System.IO.PathTooLongException: 'The specified path, file name, or both are too long The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.'

My code: 

public static void SendEmail(string ToMail, string Base64)
{

    MailMessage message = new MailMessage();
    SmtpClient smtp = new SmtpClient();

    message.From = new MailAddress("info@outlook.com");
    message.To.Add(new MailAddress(ToMail));
    message.Subject = "Test from C#";
    message.Attachments.Add(new Attachment(Base64));

    message.IsBodyHtml = true;
    message.Body = "<b>My body</b>";
    smtp.Port = 25;
    smtp.Host = "mail@outlook.com";
    smtp.EnableSsl = false;
    smtp.UseDefaultCredentials = false;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Send(message);
}

How can I send an image as an attachment in an email using C#?


1 Answer

1 like 0 dislike
by 66 69 85
 
Best answer

How to send base64 in email attachment?

First you need to clear your base64 if it start with data:image/png;base64,. Also you need to remove spaces. The below code fuction for cleaning Base64

public static string Cleaning(string img)
{
    var Base64 = img.Split(',')[1];
    StringBuilder sb = new StringBuilder(Base64, Base64.Length);
    sb.Replace("\r\n", string.Empty);
    sb.Replace(" ", string.Empty);
    return sb.ToString();
}

Second in send email function you must add your Base64 to MemoryStream.

string fileName = "MyImg.png";
Byte[] bytes = Convert.FromBase64String(Cleaning(Base64)); // clean Base64 then convert it
MemoryStream ms = new MemoryStream(bytes); // create MemoryStrem
Attachment data = new Attachment(ms, fileName); // create Attachment object + add Stream with filename
data.ContentId = fileName;
data.ContentDisposition.Inline = true;
message.Attachments.Add(data); // add data to the attachment

The final code will be like this:

public static string Cleaning(string img)
{
var Base64 = img.Split(',')[1];
StringBuilder sb = new StringBuilder(Base64, Base64.Length);
sb.Replace("\r\n", string.Empty);
sb.Replace(" ", string.Empty);
return sb.ToString();
}

public static void SendEmail(string ToMail, string Base64)
{

MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();

message.From = new MailAddress("info@outlook.com");
message.To.Add(new MailAddress(ToMail));
message.Subject = "Test from C#";

// Here is the sloution!
string fileName = "MyImg.png";
Byte[] bytes = Convert.FromBase64String(Cleaning(Base64));
MemoryStream ms = new MemoryStream(bytes);
Attachment data = new Attachment(ms, fileName);
data.ContentId = fileName;
data.ContentDisposition.Inline = true;
message.Attachments.Add(data);

message.IsBodyHtml = true;
message.Body = "<b>My body</b>";
smtp.Port = 25;
smtp.Host = "mail@outlook.com";
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
If you don’t ask, the answer is always NO!
...