Android Question Send an email in background

Mostez

Well-Known Member
Licensed User
Longtime User
I want to send an email as a proof of request to company email account once order were placed, and I want to do this in background without pop up activities. how to do that?
 

jahswant

Well-Known Member
Licensed User
Longtime User
If you Use GMAIL remember to enable unsecure apps.

B4X:
Dim emailing As clsEmail

Dim SenderEmail As String = "xxxxxxxxxxxx@gmail.com"
            Dim gMailPass As String = "xxxxxxxxxxxxxxxxxxxx"
            Dim ReciptEmails() As String
            ReciptEmails = Array As String("xxxxxxxxx@gmail.com","xxxxxxxxxx@gmail.com", "xxxxxxxxxxxxx@gmail.com")
            emailing.Initialize(Me,"smtp.gmail.com", "pop.gmail.com", SenderEmail, gMailPass, SenderEmail, ReciptEmails)
            Dim mp As Map
            mp.Initialize
            mp.Put(FileName12,File.DirInternal)
            Dim res As ResumableSub = emailing.SendEmail("OBJECT", "MESSAGE", mp)
            Wait For(res) Complete (Success As Boolean)
            If Success = True Then

  End If



B4X:
' Class for working with SMTP emailing, but with POP3 check before any sending
'(c) Pomelov Vlad aka Peacemaker, v.0.1
Sub Class_Globals
    Private InternetConnected As Boolean, ph As PhoneEvents
    Private Callback As Object
    Private SMTP1 As SMTP
    Private pop As POP3
    Private SMTPserver, SMTPlogin, SMTPpass, SenderAdr, POPserver As String
    Private SMTPto() As String
    Private const SMTPssl As Boolean = True
    Private SMTPport As Int = 465    '25, 587
    Private POPport As Int = 995
    Dim SMTPsent As Boolean
    Dim LatestError As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Parent As Object, mSMTPserver As String, mPOPserver As String, mSMTPlogin As String, mSMTPpass As String, mSenderAdr As String, mSMTPto() As String)
    Callback = Parent
    SMTPserver = mSMTPserver
    SMTPlogin = mSMTPlogin
    SMTPpass = mSMTPpass
    SenderAdr = mSenderAdr
    POPserver = mPOPserver
    SMTPto = mSMTPto
End Sub


Sub SendEmail (Subj As String, Body As String, Att As Map) As ResumableSub
        ph.Initialize("ph")
        Wait For ph_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
        If State = "CONNECTED" Then
            Log("ph_ConnectivityChanged: CONNECTED")
            InternetConnected = True
        Else    'NO INTERNET
            Log("ph_ConnectivityChanged: DISCONNECTED")
            InternetConnected = False
            LatestError = LastException.Message
            Check_Internet
            Return False
        End If

        'POP before SMTP
        pop.Initialize(POPserver, POPport, SMTPlogin, SMTPpass, "pop")
        pop.UseSSL = SMTPssl
        pop.Status

        Wait For pop_StatusCompleted (Success As Boolean, NumberOfMessages As Int, TotalSize As Int)
        If Success = False Then
            LatestError = LastException.Message
            Return False
        End If
        ' setup the SMTP object
        SMTP1.Initialize(SMTPserver,SMTPport,SMTPlogin,SMTPpass,"SMTP1")
        SMTP1.UseSSL = SMTPssl
    #if release
    For i= 0 To SMTPto.Length - 1
        SMTP1.To.Add(SMTPto(i))
    Next
    'SMTP1.To.Add(SMTPto(0))
    #end if
    #if debug
    SMTP1.To.Add(SMTPto(0))
    #End If
        SMTP1.Sender = SenderAdr
        Log("SenderAdr = "&SenderAdr)
        SMTP1.Subject = Subj
        Log("SenderAdr = "&Subj)
        SMTP1.Body = Body
        Log("Body = "&Body)
        If Att <> Null Then
            If Att.IsInitialized Then
                For i = 0 To Att.Size - 1
                    Log(Att.GetValueAt(i) & "   " & Att.GetKeyAt(i))
                    SMTP1.AddAttachment(Att.GetValueAt(i), Att.GetKeyAt(i))
                Next
            End If
        End If
        SMTP1.Send
        Return True
  
End Sub

Private Sub ph_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
    If State = "CONNECTED" Then
        Log("ph_ConnectivityChanged: CONNECTED")
        InternetConnected = True
    Else    'NO INTERNET
        Log("ph_ConnectivityChanged: DISCONNECTED")
        InternetConnected = False
      
    End If
End Sub

Sub SMTP1_MessageSent(Success As Boolean)
    Log ("Message Sent: " & Success)
    SMTPsent = Success
    If SMTPsent Then
        SMTPsent = True
        LatestError = ""
    Else If SMTPsent = False Then 
        LatestError = LastException.Message
        Log("Send Error: " & LatestError)
    End If
    If SubExists(Callback, "SMTP_MessageSent") Then
        CallSub2(Callback, "SMTP_MessageSent", SMTPsent)
    End If
End Sub

Sub Check_Internet As Boolean
    If InternetConnected = False Then
        Dim a As String = "Check Internet-connection..."
        ToastMessageShow(a, True)
        Return False
    Else
        Return True
    End If
End Sub
 
Upvote 0

stari

Active Member
Licensed User
Longtime User
ok, where to find "enable unsecure apps.". Thks.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…