Android Question [SOLVED] Automatically sending an email from B4A

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I have been calling the phone's email like this.

B4X:
Dim msg As Email

msg.Subject = "Song Requests"
msg.Body = EditTextRequestedSongs.Text
msg.To.Add("ultraviproduction@gmail.com")

StartActivity(msg.GetIntent)

Is there a way in B4A to send the email from the app without actually displaying the email client on the screen in code?

Thanks.
 

JohnC

Expert
Licensed User
Longtime User
Using an intent will just open the default email client on the device (or display a list of clients that can handle it) - which the user then needs to manually complete the sending of the email.

Doing a quick search on this forum you will find this tutorial of how to programmically send emails using the user's own gmail account without user intervention (after the user does a one-time authorization to authorize your app):

 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks for such a fast reply. I will go through the tutorial tomorrow.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
check this SMTP
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks everyone for responding.

My hopes went high when I saw John's post, but the tutorial sent me to another tutorial for GoogleOAuth2. That tutorial had too many assumptions that the reader knew how to set up a Google API account and create an account and create a client id and basically navigate Google API which we didn't have time to learn. Anyway I went to Alex's posting and I thought that was the solutions for us but when I downloaded the attached zip file, it only contained the library files and there was no sample project showing how to use the library so I did a search on that library. If I find a solution I will post the results and coding in case someone else needs to do what we are doing.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
You misunderstand, YOU do all that those steps to get the Client ID for your app.

To the user, when they first try to send an email, it will ask for authorization, which is a simple prompt, then that's all the user needs to do!
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
B4X:
Private Sub SendEmail
    
    Try

Dim mail As SMTP
mail.Initialize(Main.MailServer, 587, Main.MailUserName, Main.MailPassword, "SMTP")


        Dim Body As String
        Dim Subj As String
        Dim EmailTo As String
        
        Subj="Please contact the office to manually enter timesheet."

        Body= "It's been more than 24 hours since your clock in"
        
        EmailTo="someemail.@email.com"                
        
        mail.StartTLSMode = True
        mail.To.Add(EmailTo)
        mail.HtmlBody=True       
        mail.Subject = Subj
        mail.Body = Body
        mail.Sender = "info@email.com"
        mail.Send
        
        
    Catch
        Log("SendEmail " & LastException)
        
    End Try
    
End Sub

Sub SMTP_MessageSent(Success As Boolean)
    
    Try
        Log(Success)
        If Success Then
            
            ToastMessageShow("Message sent successfully", True)
        Else
            ToastMessageShow("Error sending message", True)
            Log(LastException.Message)
        End If
    Catch
        Log("SMTP_MessageSent " & LastException)
        
    End Try
    
    

End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
You misunderstand, YOU do all that those steps to get the Client ID for your app.

To the user, when they first try to send an email, it will ask for authorization, which is a simple prompt, then that's all the user needs to do!
You misunderstand, YOU do all that those steps to get the Client ID for your app.

To the user, when they first try to send an email, it will ask for authorization, which is a simple prompt, then that's all the user needs to do!
Hi John,

We will be going with Alex's code because we don't know how to navigate the Google API website, but thanks for suggesting that as a possible solution.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks so much for the code sample. Looks like we need another library along with the Network library.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Hi John,

We will be going with Alex's code because we don't know how to navigate the Google API website, but thanks for suggesting that as a possible solution.
That's ok, but keep in mind if you use the SMTP method, then you will still need the user to figure out some rather technical info like what their smtp server address is, the port number, the username, the password and SSL/TSL setting in order to send an email using the SMTP method.

However, if your plan is to instead use YOUR smtp server (and you would embed your smtp account username, password, port, etc within your app, so you wouldn't have to ask the user for their server's details), then please keep in mind that it wouldn't be too difficult for a hacker to uncover that info from within your APK and then use your smtp account for bad purposes. It is not likely this will happen, but I just wanted you to know about this possibility.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User

Thanks for the advice.
We were planning on having the user enter their credentials like they would do when they set up their email client on their computer.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
You really should try the GMAIL API, it's not very difficult to figure out, and it will offer a much better UX for your app.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Using Alex's code I was able to get the app to compile and recognise the library after exit and load the project back up. The message though would not be send so I'm going to start a new thread for that issue and mark this one as solved.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Thanks.

Could you look at my new thread and let me know why the message is not being sent? Thanks so much.

https://www.b4x.com/android/forum/threads/net-library-using-smtp-to-send-an-email-message.123969/

Check This first https://support.google.com/accounts...en&visit_id=637395031216316543-150071489&rd=1

Step 1 - Solution https://www.google.com/settings/security/lesssecureapps Enable the check
Step 2 -
B4X:
Private Sub SendEmail
    
    Try
        
  
        email.Initialize("smtp.gmail.com", 587, "yourmailbox@gmail.com", "gmailpassword", "EmailClient")
        
  
        Dim Body As String
        Dim Subj As String
        Dim EmailTo As String
        Dim SenderEmail As String
      
        Subj="Test Email."
        Body= "Testing 123."
        EmailTo="info@email.com"
        SenderEmail="yourmailbox@gmail.com" ' MUST be the same as "yourmailbox@gmail.com"
                
        email.AuthMethod=email.AUTH_LOGIN
        email.UseSSL = True
        email.StartTLSMode = True
        email.To.Add(EmailTo)
        email.HtmlBody=True
        email.Subject = Subj
        email.Body =  Body
        email.Sender = SenderEmail
        email.HtmlBody=True
        email.Send
        Log("Tried to send")
        
    Catch
        Log("SendEmail " & LastException)
    End Try
End Sub

Sub EmailClient_MessageSent(Success As Boolean)
    
    Try
    
        Log(Success)
        
        
    Catch
        Log(LastException)
    End Try
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks for the coding changes but it still won't get sent as reported in the logs. The "Tried to send" message and the "False" was in the logs. I also tried the "Step 1" link but Safari reports it as not a valid address.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Alex,

We evaluated the pros and cons of automatic email and because it's not secure, we choose to go for the more secure route by not making the user enter their email credentials into the app and having the app call their email client so they can tap their send button from their email.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Check this one

B4X:
    Dim in As Intent
    in.Initialize("android.intent.action.SENDTO", "mailto:")
    in.PutExtra("android.intent.extra.EMAIL", Array As String(EmailToStr))
    in.PutExtra("android.intent.extra.SUBJECT", "Some Subj")
    in.PutExtra("android.intent.extra.TEXT",  BodyStr)
    StartActivity(in)
 
Upvote 0

Similar Threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…