Android Question Insert Image into an Email

Jakes72

Active Member
Licensed User
Longtime User
Hi Experts,

I would like to put an image into my email, not as an attachment, but embedded within the email - so that when the email is opened the image will show.
Is this possible?

I am assuming I must put my message into an HTML format to achieve this and set some sort of 'image tag' = the source of my image.
Assuming my message is 'This is a test image:' and just below that I want to put 'MyImage.jpg', how would I achieve this?

Code would be much appreciated!

Regards
Jacques.
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Jakes72

Active Member
Licensed User
Longtime User
Hi Don,

I have done this before with .Net code (as shown below).
I just do not know how to do this with B4A?

Is someone able to help me with this please?

B4X:
           '----------------------------------------------
           'Prepare the message text.
           '----------------------------------------------
            Dim m_MessageBody As String = ""
            m_MessageBody = "This is a test image"
         
            'Put the message text into an AlternateView.
            Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(m_MessageBody, Nothing, "text/plain")

            'Get a unique ID for the image.
            Dim strImageID As String = Now.Ticks
            System.Threading.Thread.Sleep(20)
            

            '---------------------------------------------
            'Prepare the HTML.
           '----------------------------------------------
            Dim htmlBody As String = ""
            
            'Put the message into the html body.
            htmlBody += m_MessageBody

            'Leave a blank line.
            htmlBody += "<br/>"

           'Put the image tag in.
            htmlBody += "<img alt="""" hspace=0 src=""cid:" & strImageID & """ align=baseline border = 0 >"

           'Put the html body into an Alternate View.
            Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, "text/html")
            


           '-----------------------------------------------
           'Prepare the Image
           '-----------------------------------------------
            Dim MyImage As AlternateView

            'Put the image into an Alternate view.
            MyImage= New AlternateView(Server.MapPath("~\Graphics\MyImage.jpg"), MediaTypeNames.Image.Gif)
            MyImage.ContentId = strImageID 
            MyImage.TransferEncoding = TransferEncoding.Base64

            'Put the image into a Linked resource.
            Dim MyImageLinkedResource As LinkedResource = New LinkedResource(Server.MapPath("~\Graphics\MyImage.jpg"), MediaTypeNames.Image.Gif)
            MyImageLinkedResource .ContentId = strImageID

            'Add this resource to the HTML view.
            htmlView.LinkedResources.Add(MyImageLinkedResource )

            'Add the all the Alternate Views to the Message.
            msg.AlternateViews.Add(MyImage)
            msg.AlternateViews.Add(plainView)
            msg.AlternateViews.Add(htmlView)

            'Set the message Body to HTML.
            msg.IsBodyHtml = True

[/code}
 
Upvote 0

Jakes72

Active Member
Licensed User
Longtime User
OK I have solved the problem myself by finding bits & pieces on the internet and coming up with a solution.

Here is my working code to embed an image into an email (not as an attachment), hope it can help others:

B4X:
    Dim S As StringUtils
    Dim Bytes() As Byte =  File.ReadBytes(File.DirAssets,"MyImage.gif")  'Read the image into an array of bytes.
   
    Dim strImageBase64 As String
    strImageBase64 = S.EncodeBase64(Bytes)   'Now Convert our Image to Base64 string so that it can be directly embedded into the HTML.
   
    'Prepare the image HTML 'src' property which will contain the Image data as Base64.
    Dim strImageSource As String = """" & "data:image/png;base64, " & strImageBase64 & """"    '    "data:image/png;base64, abc123//444553......"
    
    'Finally Concatenate it together to form the final HTML string.
    Dim strHTML As String
    strHTML = "This is a Test Image:<br/>"
    strHTML = strHTML &  "<img alt=""Picture"" hspace=0 src=" & strImageSource & " " & "/>" 
   

    Mail.SetIsHTMLBody(True)                                 'Make sure we send the email as HTML.
    Mail.Send("[email protected]","Test image",strHTML)   'Send the HTML.
 
Upvote 0

Jakes72

Active Member
Licensed User
Longtime User
I have now wrapped this up in a sub which will take as parameters your Image Path & Name, it will then create the Image HTML for you which you can then simply send or add it to to other html:

B4X:
Public Sub CreateHTMLForImage(strImagePath As String,strImageName As String) As String
    
    'The path of the image.
    Dim strPath As String
    If strImagePath.Trim = "" Then
        strPath = File.DirAssets     'ie.   ..../Files
    Else
        strPath = strImagePath
    End If
   
   
    Dim S As StringUtils
    Dim Bytes() As Byte =  File.ReadBytes(strPath,strImageName)  'Read the image into an array of bytes.
   
    Dim strImageBase64 As String
    strImageBase64 = S.EncodeBase64(Bytes)   'Now Convert our Image to Base64 string so that it can be directly embedded into the HTML.

   
    'Prepare the image HTML 'src' property which will contain the Image data as Base64.
    Dim strImageSource As String = """" & "data:image/png;base64, " & strImageBase64 & """"    '    "data:image/png;base64, abc123//444553"
   
    'Finally Concatenate it together to form the final HTML string.
    Dim strHTML As String = ""
    strHTML =  "<img alt=""Picture"" hspace=0 src=" & strImageSource & " " & "/>"
   
   
    Return strHTML

End Sub


Use it like this:
B4X:
Dim strMyImageHTML as String
strMyImageHTML = CreateHTMLForImage(File.DirAssets,"MyImage.gif")

Dim strEmailHTML as string
strEmailHTML = "Hello world!!! I am on top of the Image!<br/>"
strEmailHTML = strEmailHTML & strMyImageHTML & "<br/>"
strEmailHTML = strEmailHTML & "I am below the Image!"
Mail.SetIsHTMLBody(True)                               
Mail.Send("[email protected]","Test image",strEmailHTML)
 
Upvote 0
Top