Android Question send mail with attachment

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
hi

after lots of digging i finally got this sub to work perfectly
this is the only way i found that opens the email apps only and not all sharing apps (in some cases it adds paypal as an email app but generally it is perfect)

the only issue i have now is is it possible to add an attachment this sub?
how?
i tried with provider and made sure the file was copied and exist in shared dir but no attachment was added to the mail
does this method allows to add attachment?

thanks

B4X:
Sub SendMail(SendTO As String, SendCC As String, SendBCC As String, Subject As String, Body As String)
    Dim in As Intent
    
    Try
    
        in.Initialize("android.intent.action.SENDTO", "mailto:")
        
        in.PutExtra("android.intent.extra.EMAIL", Array As String(SendTO))
        in.PutExtra("android.intent.extra.CC", Array As String(SendCC))
        in.PutExtra("android.intent.extra.BCC", Array As String(SendBCC))
    
        in.PutExtra("android.intent.extra.SUBJECT", Subject)
    
        in.PutExtra("android.intent.extra.TEXT", Body)
    
        StartActivity(in)
    
    Catch
        
        Log (LastException.Message)
        
    End Try
    
End Sub
 

zed

Active Member
Licensed User
Using an Email object you can create an intent that holds a complete email message.
You can then launch the email application by calling StartActivity. Note that the email will not be sent automatically. The user will need to press on the send button.
Example:
B4A:
Dim Message As Email
Message.To.Add("SomeEmail@example.com")
Message.Attachments.Add(File.Combine(File.DirRootExternal, "SomeFile.txt"))
StartActivity(Message.GetIntent)

look at this
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Thanks
Tried this way, it opens all sharing apps on the start not only email apps and that's the bigest problem as when the user presses always all sharing will go through email app he chose - very bad
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
thanks
i tried to figure out what's going on there and got even more lost
my code (in the first post) works file - opens only email apps as i want
but the only issue i now have is how to add attachment to it
is there anything you can help with?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The FileProvider Example shows how to send an Email with Intent with Attachments.

It does work for me on an Android 13 Device without any problem.

The code from Example:
B4X:
Private Sub btnSendEmail_Click
    Dim FileName As String = "b4a.png"
    'copy the shared file to the shared folder
    File.Copy(File.DirAssets, FileName, Provider.SharedFolder, FileName)
    Dim email As Email
    email.To.Add("aaa@bbb.com")
    email.Subject = "subject"
    email.Attachments.Add(Provider.GetFileUri(FileName))
    email.Attachments.Add(Provider.GetFileUri(FileName)) 'second attachment
    Dim in As Intent = email.GetIntent
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    StartActivity(in)
End Sub
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
The FileProvider Example shows how to send an Email with Intent with Attachments.

It does work for me on an Android 13 Device without any problem.

The code from Example:
B4X:
Private Sub btnSendEmail_Click
    Dim FileName As String = "b4a.png"
    'copy the shared file to the shared folder
    File.Copy(File.DirAssets, FileName, Provider.SharedFolder, FileName)
    Dim email As Email
    email.To.Add("aaa@bbb.com")
    email.Subject = "subject"
    email.Attachments.Add(Provider.GetFileUri(FileName))
    email.Attachments.Add(Provider.GetFileUri(FileName)) 'second attachment
    Dim in As Intent = email.GetIntent
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    StartActivity(in)
End Sub
Yes it does
But then the intent opens all sharing apps not just email apps
Then the user clicks on [always] ans all his sharing goes to that email app and i got an angry client...

So is there a way to force that example to open the picker with only email apps?

The sample i use does that...
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Then the user clicks on [always] ans all his sharing goes to that email app and i got an angry client...
B4X:
Private Sub btnSendEmail_Click
    Dim FileName As String = "b4a.png"
    'copy the shared file to the shared folder
    File.Copy(File.DirAssets, FileName, Provider.SharedFolder, FileName)
    Dim email As Email
    email.To.Add("aaa@bbb.com")
    email.Subject = "subject"
    email.Attachments.Add(Provider.GetFileUri(FileName))
    email.Attachments.Add(Provider.GetFileUri(FileName)) 'second attachment
    Dim in As Intent = email.GetIntent
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    in.WrapAsIntentChooser("'Send email")
    StartActivity(in)
End Sub

will show the chooser every time.

PD: You should tell your users to select an emailapp
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
You can reduce the number of apps the user has to chose from by specifying a more limited MIME type with SetType
B4X:
Private Sub btnSendEmail_Click
    Dim Provider As FileProvider
    Provider.Initialize
    Dim FileName As String = "b4a.png"
    Dim FileName2 As String = "b4a2.png"
    'copy the shared file to the shared folder
    File.Copy(File.DirAssets, FileName, Provider.SharedFolder, FileName)
    File.Copy(File.DirAssets, FileName2, Provider.SharedFolder, FileName2)
    Dim email As Email
    email.To.Add("aaa@bbb.com")
    email.Subject = "subject"
    email.Attachments.Add(Provider.GetFileUri(FileName))
    email.Attachments.Add(Provider.GetFileUri(FileName2)) 'second attachment
    Dim in As Intent = email.GetIntent
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    in.SetType("message/rfc822") ' ADD THIS LINE TO SET A MORE LIMITED MIME TYPE
    StartActivity(in)
End Sub
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
You can reduce the number of apps the user has to chose from by specifying a more limited MIME type with SetType
B4X:
Private Sub btnSendEmail_Click
    Dim Provider As FileProvider
    Provider.Initialize
    Dim FileName As String = "b4a.png"
    Dim FileName2 As String = "b4a2.png"
    'copy the shared file to the shared folder
    File.Copy(File.DirAssets, FileName, Provider.SharedFolder, FileName)
    File.Copy(File.DirAssets, FileName2, Provider.SharedFolder, FileName2)
    Dim email As Email
    email.To.Add("aaa@bbb.com")
    email.Subject = "subject"
    email.Attachments.Add(Provider.GetFileUri(FileName))
    email.Attachments.Add(Provider.GetFileUri(FileName2)) 'second attachment
    Dim in As Intent = email.GetIntent
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    in.SetType("message/rfc822") ' ADD THIS LINE TO SET A MORE LIMITED MIME TYPE
    StartActivity(in)
End Sub
this opens ALL sharing apps so unfortunately it is not good
all the rest is perfect but i can't have the client press [always] on his mail app and then everything he wants to share will go through this mail app
B4X:
Private Sub btnSendEmail_Click
    Dim FileName As String = "b4a.png"
    'copy the shared file to the shared folder
    File.Copy(File.DirAssets, FileName, Provider.SharedFolder, FileName)
    Dim email As Email
    email.To.Add("aaa@bbb.com")
    email.Subject = "subject"
    email.Attachments.Add(Provider.GetFileUri(FileName))
    email.Attachments.Add(Provider.GetFileUri(FileName)) 'second attachment
    Dim in As Intent = email.GetIntent
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    in.WrapAsIntentChooser("'Send email")
    StartActivity(in)
End Sub

will show the chooser every time.

PD: You should tell your users to select an emailapp
ok

this one does open the picker every time but it shows other app as well as mail apps so the user can choose whatsapp instead - and it sends the file
so it's nice
i will play with it - perhaps the solution is just tell him to choose a mail app
but it works
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
You can't use android.intent.action.SENDTO to add attachment. ACTION_SENDTO is for no attachment,See doc
tried your advice
here's what i have
it doesn't work - no attachment is added to the mail - what did i do wrong?

B4X:
Private Sub Button6_Click
    Dim in As Intent
    
    Try
    
        'in.Initialize("android.intent.action.SENDTO", "mailto:")
        in.Initialize("android.intent.action.SEND", "mailto:")
        
        in.PutExtra("android.intent.extra.EMAIL", Array As String("aaa@bbb.com"))
    
        in.PutExtra("android.intent.extra.SUBJECT", "Subject")
    
        in.PutExtra("android.intent.extra.TEXT", "Body")
    
        Dim FileName As String = "b4a.png"

        If File.Exists(File.DirAssets,FileName) Then

            File.Copy(File.DirAssets, FileName, Provider.SharedFolder, FileName)
    
            in.PutExtra("Android.intent.EXTRA_STREAM", Provider.GetFileUri(FileName))
        
        End If
    
        StartActivity(in)
        
    Catch
        
        Log (LastException.Message)
        
    End Try
    
End Sub
 
Upvote 0
Top