使用 GMailSender 从 Android 应用程序发送带有附件的电子邮件在 Transport.send 处失败

发布于 2024-12-21 15:28:50 字数 1072 浏览 0 评论 0原文

我正在尝试从我的 Android 应用程序发送一封带有图像作为附件的电子邮件。我关注了这篇文章(以及其他许多文章):通过 GMailSender 发送带有附件的电子邮件?

所以我做了同样的事情,我可以发送电子邮件,但只能没有附件。不幸的是,Transport.send 似乎失败了。一段时间后,它显示:

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

我尝试以不同的方式创建 File 对象(streamUri 我相信是正确的):

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

并且

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

但我得到:

(  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)

所以我怀疑我可能错误地创建了 File 对象。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2024-12-28 15:28:50

路径不正确,这解决了问题:

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文