未找到SMTP电子邮件文件例外

时间:2018-04-26 16:17:40

标签: java android file smtp email-attachments

我正在尝试发送附带附件的SMTP电子邮件,我通过意图附加文件,如

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
   startActivityForResult(intent,RESULT_SUCCESS);

关于活动结果:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        fileUri = data.getData();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Properties properties = new Properties();
                properties.put("mail.smtp.auth","true");
                properties.put("mail.smtp.starttls.enable","true");
                properties.put("mail.smtp.host","smtp.gmail.com");
                properties.put("mail.smtp.port","587");
                properties.put("mail.smtp.ssl.enable","true");
                properties.put("mail.smtp.user","test@example.com");
                Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("test@example.com","test123");
                    }
                });
                try {

                    javax.mail.Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("someone@example.com"));

                    message.addRecipient(javax.mail.Message.RecipientType.TO,new InternetAddress("test@example.com"));
                    message.setSubject("title");
                    message.setText("safdafd");

                    MimeBodyPart bodyPart = new MimeBodyPart();
                    MimeMultipart multipart = new MimeMultipart();
                    javax.activation.DataSource source= new FileDataSource(Environment.getExternalStorageDirectory()+fileUri.getPath());
                    bodyPart.setDataHandler(new DataHandler(source));
                    bodyPart.setFileName("test");
                    multipart.addBodyPart(bodyPart);
                    message.setContent(multipart);

                    Transport.send(message);
                    Log.v("EMAIL","done");
                }
                catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        }).start();

但它无效并抛出IOException。

W/System.err: javax.mail.MessagingException: IOException while sending message;
            nested exception is:
            java.io.FileNotFoundException: /storage/sdcard/document/150B-061A:Download/Meeting Log (1).txt: open failed: ENOENT (No such file or directory)
              at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
              at javax.mail.Transport.send0(Transport.java:189)
              at javax.mail.Transport.send(Transport.java:118)
              at com.example.sdginternee5.test.Fragments.Ticket_b$3$override.run(Ticket_b.java:371)
              at com.example.sdginternee5.test.Fragments.Ticket_b$3$override.access$dispatch(Ticket_b.java)
              at com.example.sdginternee5.test.Fragments.Ticket_b$3.run(Ticket_b.java)
              at java.lang.Thread.run(Thread.java:818)
          Caused by: java.io.FileNotFoundException: /storage/sdcard/document/150B-061A:Download/Meeting Log (1).txt: open failed: ENOENT (No such file or directory)
              at libcore.io.IoBridge.open(IoBridge.java:456)
              at java.io.FileInputStream.<init>(FileInputStream.java:76)
              at javax.activation.FileDataSource.getInputStream(FileDataSource.java:110)
              at javax.activation.DataHandler.writeTo(DataHandler.java:318)
              at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
              at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:874)
              at 

我已经尝试过:

  • 从Uri创建文件。
  • 添加权限
  • 尝试使用不同的SDK。
  • 通过拖放传输文件到模拟器

0 个答案:

没有答案