发送背景电子邮件附件

时间:2015-04-27 16:47:33

标签: java android android-studio email-attachments

我正在尝试在后台发送带附件的电子邮件 我无法使用/不使用附件。

不知道我哪里错了。 有人可以帮我解决这个问题吗? 感谢。

错误日志:

  

javax.mail.NoSuchProviderException:无效的协议:null       javax.mail.Session.getProvider(Session.java:441)       javax.mail.Session.getTransport(Session.java:660)       javax.mail.Session.getTransport(Session.java:641)       javax.mail.Session.getTransport(Session.java:627)       com.test.www.test.MailClass.doInBackground(MailClass.java:43)       com.test.www.test.DelAddress $ 1.run(DelAddress.java:82)       java.lang.Thread.run(Thread.java:818)

代码段:

    class MailClass extends AsyncTask<String, Void, Void> {
    MimeMessage email;
    String delAddress, pathsList, user, password;
    MimeMultipart multipart = new MimeMultipart();
    MimeBodyPart attachPart = new MimeBodyPart();
    DataSource source;
    Session session;

    protected Void doInBackground( ArrayList<String> imagePaths, String address) throws MessagingException, UnsupportedEncodingException, EmailException {
    setupEmailConfig();
    deliveryAddress = address;
    Log.i("doInBackground, Count:", String.valueOf(imagePaths.size()));
    createEmail(imagePaths);
    Log.i("doInBackground:", "Email Created Successfully.");
    try {
        Transport transport = session.getTransport();
        transport.connect();
        transport.sendMessage(email, email.getRecipients(Message.RecipientType.TO));
        transport.close();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    Log.i("doInBackground:", "Email Sent.");

return null;
}

    private void setupEmailConfig() {
        user = "abc@gmail.com";
        password = "abc";

        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.googlemail.com");
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.user", user);
        properties.put("mail.password", password);

        session = Session.getDefaultInstance(properties,
                  new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, password);
                     }
                });
    }

    private void createEmail(ArrayList<String> imagePaths) throws EmailException,
            MessagingException, IOException {
        String recip = "xyz@gmail.com";
        email = new MimeMessage(session);
        email.setFrom(new InternetAddress(user));
        email.addRecipient(Message.RecipientType.TO, new InternetAddress(recip));
        email.setSubject("Test Mail");
        email.setSentDate(new Date());

        pathsList = "";
        for(int i=0; i<imagePaths.size(); i++) {
            pathsList += "\r\n" + String.valueOf(i+1) + ") " + imagePaths.get(i);
//            attachPart.attachFile(imagePaths.get(i));
//            source = new FileDataSource(imagePaths.get(i));
//            attachPart.setDataHandler(new DataHandler(source));
//            attachPart.setFileName(new File(imagePaths.get(i)).getName());
//                multipart.addBodyPart(attachPart);
        }

        BodyPart messageBody = new MimeBodyPart();
        messageBody.setText("Text Body");
        multipart.addBodyPart(messageBody);
        email.setContent(multipart);
    }

    @Override
    protected Void doInBackground(String... params) {
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

做了很少的改变。工作守则。

代码:

class MailClass extends AsyncTask<String, Void, Void> {
    MimeMessage email;
    String delAddress, pathsList, user, password;
    MimeMultipart multipart = new MimeMultipart();
    Session session;

    protected Void doInBackground( ArrayList<String> imagePaths, String address) throws MessagingException, IOException, EmailException {
        setupEmailConfig();
        delAddress = address;
        Log.i("doInBackground, Count:", String.valueOf(imagePaths.size()));
        createEmail(imagePaths);
        Log.i("doInBackground:", "Email Created Successfully.");
        Transport.send(email);
        Log.i("doInBackground:", "Email Sent.");

        return null;
    }

    private void setupEmailConfig() {
        user = "abc@gmail.com";
        password = "abc";

        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.user", user);
        properties.put("mail.password", password);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.starttls.enable", "true");

        session = Session.getDefaultInstance(properties,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, password);
                    }
                });
    }

    private void createEmail(ArrayList<String> imagePaths) throws EmailException, MessagingException, IOException {
        String receiver = "abc@gmail.com";
        String receiverCC = "abc@gmail.com";
        email = new MimeMessage(session);
        email.setFrom(new InternetAddress(user, user));
        email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(receiver, receiver));
        email.addRecipient(Message.RecipientType.CC, new InternetAddress(receiverCC));
        email.setSubject("Customer Order");
        email.setSentDate(new Date());

        pathsList = "";
        for(int i=0; i<imagePaths.size(); i++) {
            pathsList += "\r\n" + String.valueOf(i+1) + ") " + imagePaths.get(i);
            MimeBodyPart attachPart = new MimeBodyPart();
            attachPart.setDataHandler(new DataHandler(new FileDataSource(imagePaths.get(i))));
            attachPart.setFileName(new File(imagePaths.get(i)).getName());
            multipart.addBodyPart(attachPart);
        }

        MimeBodyPart messageBody = new MimeBodyPart();
        messageBody.setText("Body Text." +
                delAddress + "\r\n List of Images: " + pathsList);

        multipart.addBodyPart(messageBody);
        email.setContent(multipart);
    }

    @Override
    protected Void doInBackground(String... params) {
        return null;
    }
}