Android Question SMTP – Error in sending message through Service

masterleous

Member
Licensed User
Hi all,

I need some help regarding SMTP issue having two different problems. I have one activity and one service, In activity I declared SMTP initialization properly and I am able to send emails using SMTP from activity with attachment. My service is programmed to run once in 24 hours. Service has to perform some processing work automatically and have to send email with attachment of file upon completion.

ISSUE 1: If application’s activity is open before start of Service (Android application is on oprn/sleep/standby mode and or waiting for any instance), than my Service is able to send email with attachment after processing routine work. But after sending email application got stuck or freeze with a message box having buttons “Force to Close” or “Wait”

ISSUE 2: At the time when application was closed/off and Service started, Now, Service is doing all required processing and generating file, but at the time of SMTP email it breaks without sending email.

My activity code is
B4X:
Sub Process_Globals
    Public SMTP As SMTP
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    If (FirstTime) Then
        SMTP.Initialize("smtp.gmail.com", 465, "email@gmail.com", "password", "SMTP")
        SMTP.UseSSL = True 'Gmail requires SSL.
    Else
End Sub

Sub SendEmailwithAttachment
SMTP.To.Add("sender_Email@gmail.com"”)
SMTP.Subject = "Subject of Email"
SMTP.Body = "Body of Email" & DateTime.Time(DateTime.Now) & " " & DateTime.Date(DateTime.Now)
SMTP.AddAttachment(File.DirRootExternal, "Attachment.xls")
SMTP.Send
End Sub

Sub SMTP_MessageSent(Success As Boolean)
    If Success Then
      Log("Email sent)
    Else
        Log("Error sending email")
    End If
End Sub
And my service code is

B4X:
Sub ServiceSendEmailwithAttachment
Main.SMTP.To.Add("sender_Email@gmail.com"”)
Main.SMTP.Subject = "Subject of Email"
Main.SMTP.Body = "Body of Email" & DateTime.Time(DateTime.Now) & " " & DateTime.Date(DateTime.Now)
Main.SMTP.AddAttachment(File.DirRootExternal, "Attachment.xls"))
Main.SMTP.Send
End Sub

Sub SMTP_MessageSent(Success As Boolean)
    If Success Then
      Log("Email sent)
    Else
        Log("Error sending email")
    End If
End Sub
 

masterleous

Member
Licensed User
Thanks Erel for your reply.

One more question about creating SMTP object in service. In activity we just declared SMTP initialization on Activity create function to be declared only for the first time like
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    If (FirstTime) Then
        SMTP.Initialize("smtp.gmail.com", 465, "email@gmail.com", "password", "SMTP")
        SMTP.UseSSL = True 'Gmail requires SSL.
    Else
End Sub
If we need to create SMTP object in Service, than what we need to declare SMTP initialization on Service create or Start function?
 
Top