0

I am trying to send an email with an image as attachment from my android app. I have followed this post (among many others): Sending email with attachment through GMailSender?

So I've done the same thing, and I can send emails, but only without attachments. Unfortunately, Transport.send seems to fail. After a while it shows:

D/SntpClient( 61): request time failed: java.net.SocketException: Address family not supported by protocol

I tried creating the File object in different ways (streamUri I believe is correct):

Uri streamUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); 
File f = new File(streamUri.toString()); //I get an error if I pass only streamUri as parameter

and also

File f = new File(streamUri.getEncodedPath());

But I get:

(  418): IOException while sending message
(  418): javax.mail.MessagingException: IOException while sending message;
(  418):   nested exception is:
(  418):    java.io.FileNotFoundException: /media/external/images/media/2 (No such file or directory)

So I suspect I might be creating the File object incorrectly.

Community
  • 1
  • 1
fandroid
  • 11
  • 2

1 Answers1

1

The path was incorrect, this fixed the problem:

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
fandroid
  • 11
  • 2