Android Question strange behavour sending mails

sergio Rassino

Member
Licensed User
Hello. Thank you for your help.

I've been working on a simple app to send sms to student's parents. So I have to send them rather quickly.
When I send an example message written inside the code, 20 or 30 sms can be sent without trouble. But when I want to pick the telephone number and the message from a text file (or a sqlite table) only 3 or 5 sms arrive to destiny althow LOG shows the app is still runing.

Can this be possible? I tryed many things: working with services, working with sqlite, changing telephone company, waiting up to 60 seconds between sms's, ... and always the same result.

Any idea about what am I doing wrong?
This is my code, now sending an example message, written by hand inside the code. But this is the version where I pick data from text file.

Thanks.
Sergio



B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: POP3 Example
    #VersionCode: 2
    #VersionName: 1.01
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals

    Dim SMS As PhoneSms
    Dim PE As PhoneEvents
    Dim PhoneID As PhoneId
    Dim m As Message   
    Dim h As Int
    Dim wid As Int
    Dim wnumero As String
    Dim wmensaje As String
    Dim wsuccess As Boolean
   
    Dim archivo As String
    archivo = "mensajes.txt"
    Dim linea As String
    Dim numero, mensaje As String
End Sub

Sub Globals     
    Private Label1 As Label
    Private EditText2 As EditText
    Private Button1 As Button  
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    If FirstTime Then

    End If

    insertar
    Enviar
End Sub


Sub Activity_Pause(UserClosed As Boolean)
   
End Sub


Sub Activity_Resume

End Sub


Sub Button1_Click

End Sub


Sub Enviar

    PE.InitializeWithPhoneState("PE", PhoneID)

    Dim directorio As String
    If File.ExternalWritable = True Then
        ' Si hay SD Card lo leerá de ella
        directorio = File.DirRootExternal
    Else
        ' Si no hay SD Card lo leerá del directorio interno
        directorio = File.DirInternal
    End If
   
    If File.Exists(directorio, archivo) = True Then
        Dim leer As TextReader
        Dim finalnumero As Int
        Dim finallinea As Int

        ' Lee Línea
        leer.Initialize(File.OpenInput(directorio, archivo))
        linea = leer.ReadLine

        Dim a As Int
        Do While linea <> Null

                    Log(linea)
            a = a+1

        finalnumero = linea.IndexOf2(":",0)
        numero = linea.SubString2(0, finalnumero)
        finallinea = linea.Length
        mensaje = linea.SubString2(finalnumero+1, finallinea)

            'numero = "351599999"
            'mensaje = "message written by hand "&wid

           ' Dim PhoneNumber As String = numero
            'Dim Message As String = mensaje
            Dim WantSentStatus As Boolean = True
            Dim WantDeliveryNotification As Boolean = True
           
        Try

            SMS.Send2(numero,mensaje,WantSentStatus,WantDeliveryNotification)

        Catch
            Log("Error when sending "& LastException.Message)
            Log(LastException.Message & CRLF)
        End Try   
   
       
               
            linea = leer.ReadLine
            Pausa(20)
        Loop

        leer.Close
    End If
End Sub

Sub PE_SmsDelivered (PhoneNumber As String, Intent As Intent)
    'Have never got a receipt from SMS using this!
    Dim Result As String
    Result="Receipt received Message" & wid
    'Log(Result & CRLF)
    'EditText1.Text=Result & CRLF   
End Sub

Sub PE_SmsSentStatus(Success As Boolean, ErrorMessage As String, PhoneNumber As String, Intent As Intent)
    Dim Result As String
    Result="SMS Sent Status:" & Success & " : " & ErrorMessage & " " & PhoneNumber
    'Log("Success: "&Success)
    If Success = True Then
        wsuccess = Success
    End If
   
    'EditText4.Text = Result & CRLF
    'EditText1.Text=EditText1.Text & Result & CRLF
End Sub



Sub insertar
   Dim archivo As String
   archivo = "mensajes.txt"
   Dim linea As String
   Dim numero, mensaje As String   

    Dim directorio As String
    If File.ExternalWritable = True Then
        ' Si hay SD Card lo guarda en ella
        directorio = File.DirRootExternal
    Else
        ' Si no hay SD Card lo guarda en directorio interno
        directorio = File.DirInternal
    End If
  
   File.Delete(directorio,archivo)   
   Log("archivo borrado")

For i = 1 To 10
    Log("insertando linea "&i)
    numero = "3515644741"
    mensaje = "mensaje en texto tres "&i
   
    linea = numero & ":" & mensaje
   

    Dim escribir As TextWriter
    escribir.Initialize(File.OpenOutput(directorio, archivo, True))
    escribir.WriteLine(linea)
    escribir.Close

Next
End Sub




Sub Pausa(segundos As Long)
   Dim now As Long
   now = DateTime.Now
   Do Until (DateTime.Now > now + (segundos * 1000))
      DoEvents
   Loop
End Sub
 

imbault

Well-Known Member
Licensed User
Longtime User
The lines extracted from your text file are < 160 car, or encoding of your file and your SQLite DB is OK (not unicode...)
 
Upvote 0

sergio Rassino

Member
Licensed User
Thanks imbault for your reply. I don't know if I understand what you say.
Yes, message was < 160 char (three words in fact)
What do you mean with "encoding of your file?"
When I tried with sqlite, I used this sentence to create the table:
SQL1.ExecNonQuery("DROP TABLE IF EXISTS mensajes")
SQL1.ExecNonQuery("CREATE TABLE mensajes (id INTEGER PRIMARY KEY AUTOINCREMENT, numero varchar(15) , mensaje varchar(160))")
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
In VARCHAR(x), x is ignored by SQLite, SQLite does not impose any length restrictions in VARCHAR, it means you can store more that 15 cars length in your numero field, as well as mensaje.

But maybe your problem is something else, just trying to figure out.
 
Upvote 0

sergio Rassino

Member
Licensed User
Hi Erel. Thank you for your reply. Finally, the problem was elsewhere. After doing lots of tests, I found out that my problem was that I was doing my tests sending the same sms to the same telephone number. And it seems that Telephone companies have some protocol to avoid duplicating sms, so they were dismissed.

Now it is working fine with your suggestions. But I have a small problem yet: I can't send 160 char sms. Only short ones. For example:

"TARDIVO MARIA ALICIA - Probando pocas palabras. Ahora agrego una mas." (this sms is sent ok)
but
"TARDIVO MARIA ALICIA - Probando pocas palabras. Ahora agrego una más." (this sms is not sent, and the log says: "Error when sending java.lang.NullPointerException") (the only difference is the accent on "más")

And also it does not work if I add uno word more.

What am I doing wrong?

Thank you for your continuous help

Sergio
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Sergio,

the problem you experience is due to the standard 7-bit encoding on SMS messages. Alternative schemes reduce the number of chars in a single message.
Have a look here for details.
Searching the forum you may find a code snippet from Erel useful to send LongSMSes, essentially splitting a message in parts of a multi-part message.

udg
 
Last edited:
Upvote 0
Top