Android Question [Solved] Strange behavior Sending mail with Attachment

incendio

Well-Known Member
Licensed User
Longtime User
Hello guys,

I am using this class provided in this :
https://b4x.com/android/forum/threads/class-fileprovider-share-files.97865/#post-616995

to send email with attachment.

I am only add a code from that example to add email body.
Here is my changed code
B4X:
Sub btnSendEmail_Click
   Dim FileName As String = "b4a.png"
   'copy the shared file to the shared folder
   File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
   Dim email As Email
   email.To.Add("aaa@bbb.com")
   email.Subject = "subject"
   email.Body = "This is a test" 'This is the added code
   email.Attachments.Add(Starter.Provider.GetFileUri(FileName))
   email.Attachments.Add(Starter.Provider.GetFileUri(FileName)) 'second attachment
   Dim in As Intent = email.GetIntent
   in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
   StartActivity(in)
End Sub

This was tested on Android Oreo 8.0 with B4A 8.30 on the real device.

And this is the strange thing :
1. After install on device , first clicked on Send Mail -> failed to load attachment
2. Second clicked, without quit application -> worked fine
3. Quit application then clicked again Send mail -> still worked fine
4. After it worked fine, testing again and again, always worked fine.
5. Uninstall app, then install again, first clicked -> failed to load attachment, second clicked and so on -> worked fine

I can reproduce the behavior by install and uninstall app.

Another thing, on Lolipop, there is a shared folder created under DirInternal (i think), but on oreo, there wasn't such folder, where is shared folder created under Oreo?

Any idea, why this is happen ?
Thanks in advance.
 

Peter Simpson

Expert
Licensed User
Longtime User
Hello @incendio,
Have you tried using SMTP in the let library, I fond that works all the time without any issues whatsoever.

Check 'Net' in your library tab, Dim email As SMTP in 'Process_Globals', SMTPClient.Initialize in 'If FirstTime' and away you go, just put the rest of your code in your button sub.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try to change the package name to a completely new name. Run it and see whether it works on the first attempt.

Another thing, on Lolipop, there is a shared folder created under DirInternal (i think), but on oreo, there wasn't such folder, where is shared folder created under Oreo?
You can see which folder is used in FileProvider.Initialize code.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Hello @incendio,
Have you tried using SMTP in the let library, I fond that works all the time without any issues whatsoever.

Check 'Net' in your library tab, Dim email As SMTP in 'Process_Globals', SMTPClient.Initialize in 'If FirstTime' and away you go, just put the rest of your code in your button sub.
I can't use this Net library caused I want to sent data from my user email to my email, so I don't know his/her username/password. Thanks anyway.

Try to change the package name to a completely new name. Run it and see whether it works on the first attempt.


You can see which folder is used in FileProvider.Initialize code.
After spent hours, I think, I found the caused of this strange behavior.

It was caused by security setting in my phone.

I use Honor 9 lite and set application security to open Gmail, so each time Gmail is about to open, it ask for credential.

When my app tried to launch Gmail, credential was asked first and if it was the first attempt for application to send email, it will failed to load attachment. On the second attempt and so on, send email was not a problem.

After I removed the security to open Gmail, first attempt worked fine.

Thanks you all for your help.
 
Upvote 0
Top