Android Question SMTP – Error in sending message through Service

Discussion in 'Android Questions' started by masterleous, Mar 2, 2015.

  1. masterleous

    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
    Code:
    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

    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    It is a mistake to use the activity SMTP object from the service. Instead you should create a new SMTP object in the service.
     
  3. masterleous

    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
    Code:
    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?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Better to use Service_Create. Service_Start can be raised multiple times (if the service is started multiple times).
     
  5. masterleous

    masterleous Member Licensed User

    Thanks Erel for your help, problem is now solved, just gone through your advice
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice