Reece Hewitson Reece Hewitson - 12 days ago 5
C# Question

Sending an image over E-mail C#

So basically i work for a college, and i'm writing a program that deals with disciplinary's, i want to e-mail the security team an image of a student when they are suspended, i was wondering what the best way to approach this would be.

I currently have the images stored on a database on a web server as blobs, i was thinking of possibly retrieving the image as a string of bytes and re-imaging it into an e-mail, what are your thoughts?

I don't have any pseudo code as i'm still thinking about how i would do it myself so any help would be appreciated

Answer
string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:filename\"></body></html>";
 AlternateView avHtml = AlternateView.CreateAlternateViewFromString
    (htmlBody, null, MediaTypeNames.Text.Html);

 LinkedResource inline = new LinkedResource("filename.jpg", MediaTypeNames.Image.Jpeg);
 inline.ContentId = Guid.NewGuid().ToString();
 avHtml.LinkedResources.Add(inline);

 MailMessage mail = new MailMessage();
 mail.AlternateViews.Add(avHtml);

 Attachment att = new Attachment(filePath);
 att.ContentDisposition.Inline = true;

 mail.From = from_email;
 mail.To.Add(data.email);
 mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
 mail.Body = String.Format(
            "<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
            @"<img src=""cid:{0}"" />", inline.ContentId);

 mail.IsBodyHtml = true;
 mail.Attachments.Add(att);
Comments