Android Question [Solved] [Class] Send E-Mail with FileproviderURI

fchirico

Member
Hello, According to the post https://www.b4x.com/android/forum/threads/class-send-e-mail-with-fileprovideruri.89217/

I would like to send the email in HTML format.
I modified where it says EmailIntent.SetType ("text / plain") by EmailIntent.SetType ("text / html").
But it is not interpreting the HTML code, it sends the email literally, that is, the html code appears in the body of the email.

What am I doing wrong?

This is the HTML code:

B4X:
Sub EnviarCorreoDeConfirmacion
    Dim cEncab As String
    Dim cBody  = " " As String
    Dim cBody2  = " " As String
    Dim i As Int

    cEncab = $"
    <!DOCTYPE html Public "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Arimex Importadora S.A.</title>
    </head>
    
    <body>

    <h1>Su pedido: ${cNumPed} Sucursal: ${Main.cSucursal} </h1>
    <h1>Fecha: ${Main.cFecha} </h1>
    <h1>Cliente: ${lbl_NomCli.text}  -  ${lbl_CodCli.text}/${lbl_LojaCli.text}  </h1>
    <h1>Vendedor:  ${lbl_CodVen.text} - Usuario GA.MA Tools:  ${Main.cUSR_usuario} </h1>
    <h1>Canal: ${cCanal}  Condición de Pago:  ${edt_CondPa.text} Dto.Global: ${lbl_DtoG01.text}</h1>
    <h1>Dirección de Entrega: ${edt_DirEntre.text} </h1>
    <h1>Mensaje para Factura: ${edt_MensajeFac.text} </h1>
    <table>
      <tr>
        <th scope="col">Producto</th>
        <th scope="col">Nombre producto</th>
        <th scope="col">Precio unitario</th>
        <th scope="col">Unidades</th>
        <th scope="col">Dto.de Linea</th>
      </tr>"$

    For i = 0 To nIndiceLineaPedido - 1
        If User4(i).LinPed_Cantidad > 0 Then
            cBody = cBody & $"<tr>
            <td>${User4(i).LinPed_CodProd}</td>
            <td>${User4(i).LinPed_DescriPro}</td>
            <td>${User4(i).LinPed_Precio}</td>
            <td>${User4(i).LinPed_Cantidad}</td>
            <td>${User4(i).LinPed_DtoLin} </td>
            </tr>"$
        End If
    Next

    cEncab = cEncab & cBody & $"</table>     </body>     </html>"$
    Main.sendmail.Send(Main.cUSR_email,File.DirInternal, cEncab, "GA.MA Tools - Pedido generado: " & cNumPed & " (" & Main.cEntorno & ")", "")
    
End Sub



B4X:
Private Sub SendEmail (SendTo As String, SendBody As String, SendSub As String, SendAtt As String)
    Dim EmailIntent As Intent
   
    LogColor("Send Att: " & SendAtt, Colors.Red)

    EmailIntent.Initialize(EmailIntent.ACTION_SEND, SendTo)
    EmailIntent.putExtra("android.intent.extra.SUBJECT", SendSub)
    EmailIntent.putExtra("android.intent.extra.TEXT", SendBody)
    'EmailIntent.SetType("text/plain") 'it is not related to the file itself.
    EmailIntent.SetType("text/html") 'it is not related to the file itself.
    EmailIntent.Flags = 1
    If SendAtt <> "" Then
        EmailIntent.PutExtra("android.intent.extra.STREAM", CreateFileProviderUri(shared, GlobFileName))
    End If

    StartActivity (EmailIntent)
End Sub
 
Last edited:

fchirico

Member


I have added the following lines of code to generate the mail in HMTL format:

B4X:
Sub btnSendEmail_Click
    Dim FileName As String = "b4a.png"
    'copy the shared file to the shared folder
    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
    Dim email As Email
    email.To.Add("[email protected]")
    email.Subject = "subject"
    email.Attachments.Add(Starter.Provider.GetFileUri(FileName))
    email.Attachments.Add(Starter.Provider.GetFileUri(FileName)) 'second attachment
    Dim in As Intent = email.GetIntent
    in.SetType("text/html") ' ===============>   ADDED
    in.putExtra("android.intent.extra.TEXT", "<b>Prueba HTML</b>") ' ======> ADDED
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    StartActivity(in)
End Sub

But it does not work. What it does is this, it displays the body text of the mail LITERALLY (view image attached)
 

Attachments

  • Captura.JPG
    Captura.JPG
    32.4 KB · Views: 347
Upvote 0

fchirico

Member
Try to use email.GetHtmlIntent and don't remote the two following lines.

It worked! I did it like you said, I added the indicated lines

B4X:
Sub btnSendEmail_Click
    Dim FileName As String = "b4a.png"
    'copy the shared file to the shared folder
    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
    Dim email As Email
    email.To.Add("[email protected]")
    email.Subject = "subject"
    email.Body = "<b>Prueba HTML</b>" '=========>  ADDED
    email.Attachments.Add(Starter.Provider.GetFileUri(FileName))
    email.Attachments.Add(Starter.Provider.GetFileUri(FileName)) 'second attachment
    'Dim in As Intent = email.GetIntent
    Dim in As Intent = email.GetHtmlIntent '=========>  ADDED
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    StartActivity(in)
End Sub


Thank You!
 
Upvote 0
Top