附加多个图像并通过SMTP发送邮件

时间:2018-05-17 14:24:28

标签: android email

您好我试图从图库中附加一些多张照片并通过Android工作室中的 SMTP protocole发送电子邮件但是我收到此错误Caused by: java.io.FileNotFoundException: /external/images/media/39235: open failed: ENOENT (No such file or directory),如果有人可以提供帮助,请。

这是我的代码

发送电子邮件:

btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Properties props = new Properties();
                            props.put("mail.smtp.auth", "true");
                            props.put("mail.smtp.starttls.enable", "true");
                            props.put("mail.smtp.host", "smtp.gmail.com");
                            props.put("mail.smtp.port", "587");

                            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                                protected PasswordAuthentication getPasswordAuthentication() {
                                    return new PasswordAuthentication(username, password);
                                }
                            });
                            // TODO Auto-generated method stub
                            Message message = new MimeMessage(session);
                            message.setFrom(new InternetAddress("hs@gmail.com"));
                            message.setRecipients(Message.RecipientType.TO,
                                    InternetAddress.parse("sal5@live.fr"));
                            message.setSubject("email");
                            message.setText("HI,"
                                    + "\n\n great");
                            if (!"".equals(arrayUri)) {


                                Multipart multipart = new MimeMultipart();

                                for (int i = 0; i < arrayUri.size(); i++) {
                                    // 4) create new MimeBodyPart object and set DataHandler object
                                    // to this object
                                    MimeBodyPart messageBodyPart2 = new MimeBodyPart();
                                    String filename = arrayUri
                                            .get(i)
                                            .getPath()
                                            .substring(
                                                    arrayUri.get(i).getPath().lastIndexOf("/") + 1,
                                                    arrayUri.get(i).getPath().length());// change
                                    // accordingly
                                    System.out.println("filename " + filename);
                                    DataSource source = new FileDataSource(arrayUri.get(i).getPath());
                                    messageBodyPart2.setDataHandler(new DataHandler(source));
                                    messageBodyPart2.setFileName(filename);
                                    messageBodyPart2.setDisposition(MimeBodyPart.ATTACHMENT);

                                    // 5) create Multipart object and add MimeBodyPart objects to
                                    // this object
                                    multipart.addBodyPart(messageBodyPart2);

                                }

                                // 6) set the multiplart object to the message object
                                message.setContent(multipart);

                            }
                            Transport.send(message);
                            System.out.println("Done");
                        } catch (MessagingException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();
            }
        });

附上图片:

btnAddFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create the ACTION_GET_CONTENT Intent
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_LOADIMAGE);
}});

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);


        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case RQS_LOADIMAGE:
                    Uri imageUri = data.getData();
                    arrayUri.add(imageUri);
                    myFileListAdapter.notifyDataSetChanged();
                    break;
                case RQS_SENDEMAIL:
                    break;
            }
        }

0 个答案:

没有答案
相关问题