簡體   English   中英

發送帶有附件的背景電子郵件

[英]Send Background Email with 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;
    }
}

進行少量更改。 工作代碼。

碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM