简体   繁体   中英

java.io.FileNotFoundException: /drawable:/2130837597: open failed: ENOENT (No such file or directory) On Attaching File With Email

While Attaching Image File With JavaMail Getting FileNotFoundException open failed: ENOENT (No such file or directory) Here is exception shown there I tried but could not solve iT

LogResult

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


    }


}

I tried many codes but all resulted in same error Like getting image from drawble Or selecting from sd card

I Tried

1st

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

2nd

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

3rd

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

4th

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

5th

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

Nothing worked for me getting same error :( Help!!!

Check out this question: How to get the file path from URI? The point here is that you need to pass the file path to the FileDataSource. On windows it should be like "C:/mypath/myfile.ext". On linux: "/home/user/path/myfile.ext". So, you need to convert your URI to a real file path.

I suggest trying the approach mentioned in the question I cited:

File myFile = new File(uri.getPath());
myFile.getAbsolutePath()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM