【问题标题】:Send Background Email with attachments发送带有附件的后台电子邮件
【发布时间】:2015-04-27 16:47:33
【问题描述】:

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

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

错误日志:

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

【问题讨论】:

  • 你得到什么错误/输出?如果您尝试发送带附件的电子邮件与不带附件的电子邮件,该输出会有所不同吗?
  • 我没有收到任何错误。有/没有附件,输出没有区别。
  • 我怀疑这是您唯一的问题,但是您为什么使用 imagePaths.get(0) 而不是 imagePaths.get(i) ?此外,在尝试期间记录您正在添加的路径并检查 logcat(针对整个设备,而不仅仅是您的应用程序)。
  • 只有 imagePaths.get(i)。我做了这个改变。对这个有什么建议吗?仍然坚持这一点。谢谢。
  • 有什么建议吗?

标签: java android android-studio email-attachments


【解决方案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;
    }
}

【讨论】:

    猜你喜欢
    • 2015-09-09
    • 2015-08-28
    • 2013-11-26
    • 2014-11-09
    • 2017-02-04
    相关资源
    最近更新 更多