0

使用JavaMail附加图像文件时,打开FileNotFoundException失败:ENOENT(没有这样的文件或目录)这是我尝试但无法解决 iT 的异常

日志结果

E/SendMail: IOException while sending message
javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.FileNotFoundException: /drawable:/2130837597: 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 io.farooq.check.GMailSender.sendMail(GMailSender.java:81)
    at io.farooq.check.MainActivity$1$1.run(MainActivity.java:74)
    at java.lang.Thread.run(Thread.java:841)
 Caused by: java.io.FileNotFoundException: /drawable:/2130837597: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:409)
    at java.io.FileInputStream.<init>(FileInputStream.java:78)
    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 javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:444)
    at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:102)
    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:897)
    at javax.activation.DataHandler.writeTo(DataHandler.java:330)
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)
    at javax.mail.Transport.send0(Transport.java:189) 
    at javax.mail.Transport.send(Transport.java:118) 
    at io.farooq.check.GMailSender.sendMail(GMailSender.java:81) 
    at io.farooq.check.MainActivity$1$1.run(MainActivity.java:74) 
    at java.lang.Thread.run(Thread.java:841) 
 Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
    at libcore.io.IoBridge.open(IoBridge.java:393)
    at java.io.FileInputStream.<init>(FileInputStream.java:78) 
    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 javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:444) 
    at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:102) 
    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:897) 
    at javax.activation.DataHandler.writeTo(DataHandler.java:330) 
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403) 
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636) 
    at javax.mail.Transport.send0(Transport.java:189) 
    at javax.mail.Transport.send(Transport.java:118) 
    at io.farooq.check.GMailSender.sendMail(GMailSender.java:81) 
    at io.farooq.check.MainActivity$1$1.run(MainActivity.java:74) 
    at java.lang.Thread.run(Thread.java:841) 

Gmailsender.java

public class GMailSender extends javax.mail.Authenticator {
    private String mailhost = "smtp.gmail.com";
    private String user;
    private String password;
    private Session session;
    private Multipart _multipart = new MimeMultipart();

    static {
        Security.addProvider(new JSSEProvider());
    }

    public GMailSender(String user, String password) {
        this.user = user;
        this.password = password;

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }

    public synchronized void sendMail(String subject, String body,
                                      String sender, String recipients) throws Exception {

        MimeMessage message = new MimeMessage(session);

        // DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));

        message.setSender(new InternetAddress(sender));
        message.setSubject(subject);

        // message.setDataHandler(handler);


        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText(body);

        _multipart.addBodyPart(messageBodyPart);


        if (recipients.indexOf(',') > 0)
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
        else
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

        message.setContent(_multipart);

        Transport.send(message);
    }


    public void AddAttachment(String Path) throws MessagingException {


        BodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(Path);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(Path);
        _multipart.addBodyPart(messageBodyPart);

    }


}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    String FilePath;
    final int PICK_IMAGE=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

        work();


    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(MainActivity.this,"File Path =" +FilePath,Toast.LENGTH_LONG).show();
        if ((requestCode == 0) && (resultCode == -1)) {
            FilePath=data.getData().getpath();

        }
    }

    public void work()
    {


        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Thread(new Runnable() {

                    @Override
                    public void run() {
                      //  final String imageUri = "drawable://" + R.drawable.write;

                        try {
                            GMailSender sender = new GMailSender("xyz@gmail.com","Password");
                            try{
                            sender.AddAttachment(FilePath);
                            }
                            catch (Exception e){
                                Log.e(" Attachment !!!", e.getMessage(), e);
                            }
                            sender.sendMail(one.getText().toString(),two.getText().toString(),
                                    "xyz@gmail.com", "xyz@gmail.com");

                        } catch (Exception e) {
                            Log.e("SendMail", e.getMessage(), e);
                        }
                    }

                }).start();
            }
        });


    }


}

我尝试了很多代码,但都导致了同样的错误,比如从 drawble 获取图像或从 sd 卡中选择

我试过了

第一

 FilePath=data.getData().getPath();

第二

Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
                        "://" + getApplicationContext().getResources().getResourcePackageName(R.drawable.write));

第三

FilePath=Environment.getExternalStorageDirectory().getPath()+ imageUri.toString()+".jpg";

第 4 名

 File myFile = new File(FilePath);
        FilePath=Environment.getExternalStorageDirectory().getPath()+ myFile.getAbsolutePath()+".jpg";

第 5 名

String imageUri = "/drawable://" + R.drawable.write;
       FilePath=Environment.getExternalStorageDirectory().getPath()+imageUri+".jpg";

对我来说没有任何用得到同样的错误:(帮助!!!

4

1 回答 1

0

查看这个问题:如何从 URI 获取文件路径? 这里的重点是您需要将文件路径传递给 FileDataSource。在 Windows 上,它应该类似于“C:/mypath/myfile.ext”。在 Linux 上:“/home/user/path/myfile.ext”。因此,您需要将 URI 转换为真实的文件路径。

我建议尝试我引用的问题中提到的方法:

File myFile = new File(uri.getPath());
myFile.getAbsolutePath()
于 2017-02-16T19:14:31.720 回答