Android Question SMTP Email does not work in receiver/service

DaOel

Member
Licensed User
Longtime User
My app wants to remind people for appointments via Email automatically. This can only be done in the background via receiver which starts itself in a certain time interval.

While being able to send Emails via a button_click.
It is not possible to send Emails in a service or receiver. (I am using google Email account as sender)
The Error is the following:

B4X:
error: (RuntimeException) java.lang.RuntimeException: Empty writer returned: 503-5.5.1 RCPT first. A mail transaction protocol command was issued out of
503-5.5.1 sequence. For more information, go to
503-5.5.1  https://support.google.com/a/answer/3221692 and review RFC 5321
503 5.5.1 specifications. fc19-20020a1709073a5300b00a26ea2179e8sm7448020ejc.41 - gsmtp

If example code is required, please ask for it. I will create one then.
There is no other way to schedule tasks than StartServiceAt or StartReceiverAt, correct?
 
Last edited:

DaOel

Member
Licensed User
Longtime User
thanks Erel,
I will try to follow your instructions as close as possible.

1. Relevant code:
[Note: It is still an activity app, I tried to port it to an B4XPages app. The Code excerpts are from my new port to B4XPages / as same error occurs in an B4XPages app]

Starter Service:
B4X:
Sub Service_Start (StartingIntent As Intent)
 
    StartService(EmailSender)     'I tried receiver first, now I tried Service. Same Error happens
 
End Sub

Email Sender Service::
B4X:
Sub Service_Start (StartingIntent As Intent)

    SmtpClient.Initialize("smtp.gmail.com", 587, "[email protected]", "application_password_here", "smtpClient")
    'SmtpClient.Initialize("mail.gmx.net", 587, "[email protected]", "application_password_here", "smtpClient")
    SmtpClient.StartTLSMode = True

End Sub

B4X:
Sub Send (email As String, subject As String, message As String)
 
    'SmtpClient.AuthMethod = SmtpClient.AUTH_LOGIN          'This doesn't help / same error
    SmtpClient.To.Clear
    SmtpClient.To.Add(email)
    SmtpClient.HtmlBody = False
    SmtpClient.Subject = "test"
    SmtpClient.Body = "test"
   
    Dim sf As Object = SmtpClient.Send
    Wait For (sf)smtpClient_MessageSent (Success As Boolean)
    If Success Then
        Log("Email sent")
    Else
        Log("error: " & LastException)
        ToastMessageShow(LastException, True)
    End If
 
End Sub



_____________________________
2.
a.
I used this code to test: http call (HttpJob)

B4X:
    Private j1 As HttpJob
    j1.Initialize("j", Me)
    j1.Download("https://google.com")
 
    Wait For (j1) JobDone(j As HttpJob)
    If j.Success Then
        Dim res As String = j.GetString
        Log(res)
        'Log($"Clean:"$)
    Else
        Log(j.ErrorMessage)
    End If
    j.Release


This code basically works in a service.
Interestingly it doesnt work, if i have a breakpoint before the Wait for, but without breakpoint the httpJob works fine.


2.
b.
I have registered also at GMX and also Yahoo. Yahoo regrets they are not able to create an account and GMX delivered another error 503 (bad sequence of commands)
Google basically works, the only problem is when SMTP is started from a receiver / service.
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
works for me (main starts receiver at some time in future, receiver starts service, service successfully sends email. i received it):

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
*** Receiver (gmailreceiver) Receive (first time) ***
started by:
starting service
*** Service (gmailservice) Create ***
** Service (gmailservice) Start **
says the message was sent...

simple example attached.
 

Attachments

  • gmailtest.zip
    10.3 KB · Views: 38
Upvote 0

DaOel

Member
Licensed User
Longtime User
hello drgottjr,
this code also sends an email on my phone!

I do not reach this code:
B4X:
Sub smtp_MessageSent(Success As Boolean)
    If Success Then
        Log("says the message was sent...")
    Else
        Log("no deal...")
    End If
End Sub

Thats why there is no log about message being sent.

But Wait for works for me:
B4X:
    Dim s As Object = smtp.Send
    Wait For (s)smtpClient_MessageSent (Success As Boolean)
    If Success Then
        Log("Email sent")
    Else
        Log("error: " & LastException)
        ToastMessageShow(LastException, True)
    End If

I wonder what the problem is on my project and I will structure it the same way you did. Thanks!
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
my interest was to show that it is possible to send an email using a receiver and a service. i thought that was the most important thing for you to know. i put something together for you quickly without "wait for". wait for is what we use nowadays (when i change my example to "wait for", mail is still sent). if the only code in your receiver is to start the service, then the problems are in the service. if you're doing stuff in the receiver, you shouldn't.
 
Last edited:
Upvote 0

DaOel

Member
Licensed User
Longtime User
I tried to take your little example and put it into my bigger project. 503 error remained in the big project even with your code and same structure (1 Receiver and 1 Service).

So what I did was to take your example and extend it. I basically made an exact copy of the big project. Suprisingly the Email function kept working. Emails were sent all the time.
So I exchanged my old big project with the new one, that became basically a copy.

Now I uploaded the project which works with B4A Bridge on my smartphone into the testing phase of google console. I downloaded the app from google play store (as internal tester) and when the app tries to send an email:

B4X:
Empty writer returned: 503-5.5.1 RCPT first.

wtf ... there is some magic that is refusing to work sometimes, if the function is called from the background. ('StartServiceAtExact')
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
the topic of this thread involved sending an email using gmail smtp in a service started by a receiver. that has been addressed
and shown to be quite doable. that you are still encountering problems would indicate that the troubles are found elsewhere.

it's unclear what occurs in main. and aside from sending an email, it's equally unclear what happens in the service. there is simply too much that could happen; we cannot guess. we have something (email) which can be sent successfully, yet it is being prevented from being sent. by showing that you can send email from a service, i was hoping you would see the basic problem had nothing to do with the ability to send email from a service.

i think you should start a new thread concerning services, receivers and background actions.

i would make these observations:
startserviceatexact from the background is probably the problem. lots of related threads here.
you may need a foreground service.
you may not need a receiver (my example merely showed how it was possible to use a receiver to start a service).
google doesn't like anything that wants to be started at an "exact time". it interferes with its enery-saving schemes.
starting anything from the background is probably not going to work.
you should look into the alarm manager and how to get your app to wake up (erel wrote a nice routine).
you also need to think about what to do if the user turns the device off or if android happens to decide to kill your app (there is no guarantee the app will run forever) or if the user travels to a different time zone or if there is no internet connection. there are broadcast actions to cover those eventualities; you would need to incorporate them all into your project.
 
Upvote 0
Top