Replace more then one item in Outlook Mail Message

Hi
I am replacing an image converted to Base64String into Mailbody.txt using:
Mailbody.Replace(“PictureOfGraphExp”,ImageTag)

It works, but if I want to replace >1 image, what would the Mailbody.Replace code then look like?

I have defined:
ByteArray = System.IO.File.ReadAllBytes(“PictureOfGraphExp.jpg”)
Base64String = convert.ToBase64String(ByteArray)
ImageTag = ImageTag.Replace(“tobeReplaced”,Base64String) with default value: “<img src=”“data:image/jpg;base64,tobeReplaced”" alt="“Red dot”">"

Br Tanja

1 Like

Hey @TBFA

Just concat all the final IMG Tags

So let’s say you have three images & all the three image tags will be concatenated to a final string.

Then perform the replace operation which replaces the static string PictureOfGraphExp with the concatenated img tags (may be with3 image tags or even more than that.

Hope this helps

Thanks
#nK

Hi

I am not sure I understand, can you show me an example?

Br Tanja

1 Like

Hey @TBFA

Here you go…

finalImgTag = String.Empty;

ByteArray1 = System.IO.File.ReadAllBytes(“PictureOfGraphExp.jpg”)
Base64String1 = convert.ToBase64String(ByteArray1)
ImageTag1 = ImageTag.Replace(“tobeReplaced”,Base64String1) with default value: “<img src=”“data:image/jpg;base64,tobeReplaced”" alt="“Red dot”">"

ByteArray2 = System.IO.File.ReadAllBytes(“PictureOfGraphExp.jpg”)
Base64String2 = convert.ToBase64String(ByteArray2)
ImageTag2 = ImageTag.Replace(“tobeReplaced”,Base64String2) with default value: “<img src=”“data:image/jpg;base64,tobeReplaced”" alt="“Red dot”">"

finalImgTag = ImageTag1 + "<br />" + ImageTag2

Mailbody.Replace(“PictureOfGraphExp”,finalImgTag)

Hope this helps

Thanks
#nK

I think need to extend the Mailbody.Replace code, because it it not the same picture (PictureOfGraphExp). I have 3 different pictures that I want to insert 3 different places in the txt file.

1 Like

Hey @TBFA

I just shown a sample, you can please use three different pictures which is fine.

finalImgTag = String.Empty;

ByteArray = System.IO.File.ReadAllBytes(“Picture1.jpg”)
Base64String = convert.ToBase64String(ByteArray)
ImageTag1 = ImageTag.Replace(“tobeReplaced”,Base64String) with default value: “<img src=”“data:image/jpg;base64,tobeReplaced”" alt="“Red dot”">"

ByteArray = System.IO.File.ReadAllBytes(“Picture2.jpg”)
Base64String = convert.ToBase64String(ByteArray)
ImageTag2 = ImageTag.Replace(“tobeReplaced”,Base64String) with default value: “<img src=”“data:image/jpg;base64,tobeReplaced”" alt="“Red dot”">"

ByteArray = System.IO.File.ReadAllBytes(“Picture3.jpg”)
Base64String = convert.ToBase64String(ByteArray)
ImageTag3 = ImageTag.Replace(“tobeReplaced”,Base64String) with default value: “<img src=”“data:image/jpg;base64,tobeReplaced”" alt="“Red dot”">"

finalImgTag = ImageTag1 + "<br />" + ImageTag2 + "<br />" + ImageTag3

Mailbody.Replace(“PictureOfGraphExp”,finalImgTag)

Hope this helps

Thanks
#nK

The 3 different ImageTags (1, 2 and 3) should be inserted in the txt file instead of the text “picture1”, “picture2” and “picture3”. Then I need both the “picture1”, “picture2” and “picture3” to be part of the code: MailBody.Replace.

Is it something like: Mailbody.Replace(“Picture1”,ImageTag1" + “Picture2”,ImageTag2" + “Picture3”,ImageTag3" )?

Or how do I combine the “Picture1”,ImageTag1", “Picture2”,ImageTag2" and “Picture3”,ImageTag3" in the MailBody.Replace code?

1 Like

Hi @TBFA ,

I believe it should be in the following way :

Mailbody.Replace("Picture1",ImageTag1).Replace("Picture2",ImageTag2).Replace("Picture3",ImageTag3)

Hey @TBFA

Mailbody replace is still the same as you can see we have added all the image tags under the finalimgtag variable.

And we are using that variable in replace.

Which will obviously include all the three images into the mail body.

Hope this helps

Thanks
#nK

Hi

It works as intended, thank you :smiley:

Br Tanja

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.