0

JavaMailを使用して画像ファイルを添付しているときにFileNotFoundExceptionのオープンに失敗しました: ENOENT (そのようなファイルまたはディレクトリはありません)

ログ結果

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


    }


}

多くのコードを試しましたが、ドローブルから画像を取得したり、SDカードから選択したりするなど、すべて同じエラーが発生しました

私は試した

1位

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

2位

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

3位

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

この質問を確認してください: How to get the file path from 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 に答える