Sending email attachments

stevel05

Expert
Licensed User
Longtime User
Hi all,

I am now back to looking at this as my app is very nearly complete. I have two versions of code that work for different email apps.

The first 'Generic' version uses the B4A supplied Email object but my HTC mail doesn't appear as an option in the chooser. This works and allows attachment of email to both K-9 mail and GMail, although the Gmail doesn't send (that's another issue though and nothing to do with B4A)

The second 'HTC' List's K-9, Gmail and the HTC mail app, The Htc mail app works and attaches the file and sends, the K-9 mail doesn't attach the file and Gmail stops responding!.

My question is, how does using the Email Object differ to creating an intent from scratch (as in version 2), as both are halfway there and I'd like to meet in the middle and get one routine to work with all email apps.

Thanks

Steve

'Generic
Dim Email1 As Email

Email1.Subject="file.zip"
Email1.Attachments.Add(File.Combine(File.Combine(File.DirRootExternal,"cpbackups"),"file.zip"))
Try
StartActivity(Email1.GetIntent)
Catch
Log("CreateMail: Start Activity Failed... ")
End Try


'HTC
Dim Intent1 As Intent
Uri="file://"&File.Combine(File.Combine(File.DirRootExternal,"cpbackups"),"file.zip")
Intent1.Initialize(Intent1.ACTION_SEND,"")
Intent1.PutExtra("android.intent.extra.SUBJECT","file.zip")
Intent1.PutExtra("android.intent.extra.STREAM",Uri)
Intent1.SetType("application/zip")
Intent1.PutExtra("android.intent.extra.TEXT","Message Text")

Try
StartActivity(Intent1)
Catch
Log("CreateMail: Start Activity Failed... "&Uri)
End Try
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The Email Intent is pretty complex:

B4X:
public Intent GetIntent() {
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
                    To.getObject().toArray(new String[0]));
            emailIntent.putExtra(android.content.Intent.EXTRA_CC, 
                    CC.getObject().toArray(new String[0]));
            emailIntent.putExtra(android.content.Intent.EXTRA_BCC, 
                    BCC.getObject().toArray(new String[0]));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, Subject);
            emailIntent.putExtra(Intent.EXTRA_TEXT, Body);
            ArrayList<Uri> uris = new ArrayList<Uri>();
            for (Object file : Attachments.getObject())
            {
                File fileIn = new File((String)file);
                Uri u = Uri.fromFile(fileIn);
                uris.add(u);
            }
            emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            return emailIntent;
        }
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Thanks Erel,

The only real difference I can see is that you use 'android.content.Intent.ACTION_SEND_MULTIPLE' which doesn't seem to be available to the Intent, the activity fails to start if I try it. Can this be made available?

Steve
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
OK, I tried that one.

The only other thing that seems to make a difference is the mime type. Anything but application/zip and the htc mail disappears from the list.

I've tried

Intent1=Email1.GetIntent
Intent1.SetType("application/zip")
ActivityStart(Intent1)

without success. Should this actually set the mime type?

Steve
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
OK I answered my own question by setting the mimetype to */* and a lot more apps respond, so it seems to. But the HTC mail still doesn't appear.:BangHead:

Can you think of anything else that could make this happen?

Thanks

Steve
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I think I understand the issues, maybe!

1 HTC mail doesn't respond to SEND_MULTIPLE and so doesn't work with the Email intent.

2 Neither K-9 mail or GMail like accepting the Uri as an extra.STREAM, even if its empty, although HTC mail is quite happy and works as expected. K-9 ignores it and gmail crashes. Could this be a string format issue? I can see that in your java code it uses a Parcel to pass the URI, would the string format in B4a be compatible once compiled?

Would it be possible to have and additional email intent that uses action.SEND? This would presumably only allow one attachment per email.

Or is there a simpler solution?

Thanks

Steve
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Ah, OK I haven't looked at that yet. I'll give it a go.

Thanks

Steve
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Hi Erel,

I've spent a bit of time on this and have arrived at the code below. The Action gets changed, and the HTC mail appears on the chooser, but the attachment is no longer attached, although the log entry shows it's still there, and Gmail still crashes.

Is this how you'd expect it to be done? or have I missed a step or two?

Thanks

Steve


Sub CreateEmail


Dim Obj1 As Reflector
Dim Intent1 As Intent
Dim types(1)
Dim args(1)

Dim Email1 As Email

types(0)="java.lang.String"
args(0)="android.intent.action.SEND"

Email1.Subject="file.zip"


Email1.Attachments.Add(File.Combine(File.Combine(File.DirRootExternal,"cpbackups"),"file.zip"))

Intent1=Email1.GetIntent
Intent1.SetType("application/zip")

Obj1.Target=Intent1

Obj1.RunMethod2("setAction",args(0),types(0))

Intent2=Obj1.Target
Log(Intent2.ExtrasToString)

Try
StartActivity(Obj1.Target)
Catch
Log("CreateMail: Start Activity Failed...

")
End Try
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Hmm standardization at it's best.:sign0148:

I'll have to try to identify the correct component and have an option to call,the HTC mail directly.

Thanks Erel.

Steve
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
It looks fine however it is possible that the HTC mail application expects a different Intent structure.
Hey :)

I use at the end of my code Startactivity(mail.getintent) but somehow in the intentchooser appear apps like dropbox or bluetooth. Is this a bug? :D

Thanks for reply :)
 
Upvote 0
Top