Get an image of the body of an email, write to the database or in memory and show it in a new Viewer

Asked

Viewed 243 times

5

I’m trying to build an E-mail viewer.

To do so, I access my IMAP server and get email using the DLL ComponentSoft.Net.Mail.

After getting the message, I intend to present it in my viewer.

However, I am having problems with Linked Sources, this because I do not intend to store them physically but only store them in MemoryStream or BD byte array.

The problem is getting the image of any of these sites, because I want to put it back as CID in the Body email, so that my viewer presents it. However I am not getting that CID.

What to do?

For better understanding I put the code I’m using:

 Dim img As Drawing.Image = Drawing.Image.FromStream(email.LinkedResources(i).GetContentStream)
 Dim converter As New Drawing.ImageConverter
 Dim byteImage As Byte() = converter.ConvertTo(img, GetType(Byte()))
 Dim base64 As String = System.Convert.ToBase64String(byteImage)
 Dim LR As String = "cid:" + email.LinkedResources(i).ContentIdentifier
 If MailBody.Contains(LR) Then
     MailBody = Replace(MailBody, LR, "<img src=data:image/gif;base64," + base64)
 End If
  • 1

    Ever thought of converting the image to String Base64?

  • Actually, I was just doing a test using an ashx handler, and I figured I could use the Base64 image. Any tips on how to convert the image to Base64?

1 answer

1

As I proposed in the comments, it seems to be the ideal solution to convert the image to String Base64 and then just unfurl it. Since I’m basing that you’re using C#, my answer will be in C#, if it’s not I’ll fix it later.

My sample code was taken from this website SOEN question.

Converting to Base64

public string ConvertImageToBase64(byte[] imageFile){
  MemoryStream ms = new MemoryStream(imageFile);
  Image imgContainer = ScaleImage(Image.FromStream(ms));//Salva a imagem com novas proporções, caso necessário
  ms = new MemoryStream();
  imgContainer.Save(ms,System.Drawing.Imaging.ImageFormat.Png); //Modificar o Png para o formato da imagem
  return Convert.ToBase64String(ms.ToArray());
}

Returning from Base64 to image

public BitmapImage Base64Image(string imgBase64){
  byte[] fileBytes = Convert.FromBase64String(base64string);    
  MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length);
  ms.Write(fileBytes, 0, fileBytes.Length);
  BitmapImage bitmapImage = new BitmapImage();
  bitmapImage.SetSource(ms);
  return bitmapImage;
}

Basically this makes the conversion and disconnection, if you want to use the image internally to the program, as a BitmapImage.

If you want to create a file with the image, as seen here:

public void SaveImage(string imageBase64, string filePath){
  byte[] bytes = Convert.FromBase64String(imageBase64.Content);
  using (var imageFile = new FileStream(filePath, FileMode.Create)){
    imageFile.Write(bytes ,0, bytes.Length);
    imageFile.Flush();
  }
}

This conversion may also facilitate storage if necessary.

  • Hello @Elipe.avelar. First of all thank you. I already did the conversion (by the way I had been able to test it yesterday before leaving, the conversion). I’m actually using VB.Net, but you can adapt the code well. However, I have a question: to present the image in my "Mailbody", should I add the Base64 string as Cid? Or create an image tag (<img src....> ) ?

  • @N0c0dde, will depend on your email reader. If it is a Webmail or the client is compatible with the Data URI Scheme, You can embed the image in the img tag itself as follows: <img src="data:string_base_64"/> (which is equivalent to CID), and most email clients today support this. If you really have the desire to use CID is possible too, in this link from SOEN you see how to apply the two forms.

  • unfortunately I had already tried to follow these steps and it is not working. I replace Cid in Mailbody as follows Dim LR As String = "cid:" + email.LinkedResources(i).ContentIdentifier&#xA; If MailBody.Contains(LR) Then&#xA; MailBody = Replace(MailBody, LR, "<img src=data:image/gif;base64," + base64)&#xA; End If

  • But have you tried using Cid itself? The link I sent you has that answer that talks about using Cid with Base64.

  • From what I understood of that answer, that would be to embed directly into an email reader. The point is that I’m creating my own visualization page, in code. I don’t know if that was very explicit

  • @N0c0dde, unfortunately I only know these two ways to embed an image in an email. : // You are creating your own reader, in this case?

  • Okay. Thanks for your help. Yes, I am creating a viewer that, as I explained in the first post, goes to my IMAP server to get the emails and shows them to users.

  • Another issue I came across was that, for example on a normal email server, Linked Resources are part of an Handler Attachment.ashx with an id that corresponds to an owaID. I have used these owaIDs in situations in the past, but only for mail Uniqueids. There will be a way to convert Linked Resource Ids to Owaids to be able to use in my Handler (which I have created since)?

  • @N0c0dde If you have other questions, ask the question or if you think it does not fit in the same question open another question that we will do our best to help you. Here is different from forum. We need questions and answers well organized. We do not want to solve only your problem, we want this knowledge to be easy for other people to find.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.