javax.mail无法在Marshmallow或更高版本的系统上运行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javax.mail无法在Marshmallow或更高版本的系统上运行相关的知识,希望对你有一定的参考价值。

我正在尝试使用javax.mail软件包从android应用程序发送电子邮件,我发现this,它在premarshmallow操作系统上工作得很好但是当我试图从具有Marshmallow或以上操作系统的手机发送电子邮件时,Transport.send(message)挂起并且永远不会回来这是我的SendEmailTask课程

 public static class SendEmailTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {

                int a=5;
            Log.e("ErrorAsync","before out");
            try {
                Log.e("ErrorAsync","before in");
                Transport.send(message);
                Log.e("ErrorAsync","after in");
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("ErrorAsync",e.getMessage());
            }
            Log.e("ErrorAsync","after out");
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {

        }

        @Override
        protected void onPreExecute() {
            if(android.os.Debug.isDebuggerConnected())
                android.os.Debug.waitForDebugger();
        }

        @Override
        protected void onProgressUpdate(Void... values) {}
    }

logcat输出如下

09-12 08:29:21.872 6498-6542 /? E / ErrorAsync:在出局之前

09-12 08:29:21.873 6498-6542 /? E / ErrorAsync:在之前

我的问题是:有没有办法在Marshmallow或以上操作系统上以编程方式发送电子邮件?

答案

我已经使用这个库创建了我的实现,并且它很长时间以来工作正常。所以尝试如下:

J SSE provider.Java

        import java.security.AccessController;
        import java.security.Provider;

        public final class JSSEProvider extends Provider {

            public JSSEProvider() {
                super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
                AccessController
                        .doPrivileged(new java.security.PrivilegedAction<Void>() {
                            public Void run() {
                                put("SSLContext.TLS",
                                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                                put("Alg.Alias.SSLContext.TLSv1", "TLS");
                                put("KeyManagerFactory.X509",
                                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                                put("TrustManagerFactory.X509",
                                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                                return null;
                            }
                        });
            }
        }

mail sender.Java

    import android.util.Log;

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.security.Security;
    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;


    public class MailSender extends javax.mail.Authenticator {

        private String mailhost = "smtp.zoho.com";
        private String user;
        private String password;
        private Session session;

        private Multipart _multipart = new MimeMultipart();
        static {
            Security.addProvider(new JSSEProvider());
        }

        public MailSender(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 {
            try {
                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);

                // Put parts in message
                message.setContent(_multipart);
                if (recipients.indexOf(',') > 0)
                    message.setRecipients(Message.RecipientType.TO,
                            InternetAddress.parse(recipients));
                else
                    message.setRecipient(Message.RecipientType.TO,
                            new InternetAddress(recipients));
                Transport.send(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void addAttachment(String filename) throws Exception {
            BodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName("download image");
            _multipart.addBodyPart(messageBodyPart);
        }

        public class ByteArrayDataSource implements DataSource {
            private byte[] data;
            private String type;

            public ByteArrayDataSource(byte[] data, String type) {
                super();
                this.data = data;
                this.type = type;
            }

            public ByteArrayDataSource(byte[] data) {
                super();
                this.data = data;
            }

            public void setType(String type) {
                this.type = type;
            }

            public String getContentType() {
                if (type == null)
                    return "application/octet-stream";
                else
                    return type;
            }

            public InputStream getInputStream() throws IOException {
                return new ByteArrayInputStream(data);
            }

            public String getName() {
                return "ByteArrayDataSource";
            }

            public OutputStream getOutputStream() throws IOException {
                throw new IOException("Not Supported");
            }
        }
    }

mail.Java

    import android.util.Log;
    public class Mail {
        String senderId,password,receiverId,subject,body;

        public Mail() {
        }

        public void setSenderId(String senderId) {
            this.senderId = senderId;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public void setReceiverId(String receiverId) {
            this.receiverId = receiverId;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }

        public void setBody(String body) {
            this.body = body;
        }

        public void sendMail(){
            new Thread(new Runnable() {
                public void run() {
                    try {
                        MailSender sender = new MailSender(
                                senderId,
                                password);
                        sender.sendMail(subject,
                                body,
                                senderId,
                                receiverId);
                    } catch (Exception e) {
                        Log.e("mailError",e.getMessage());
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

将所有这些类保存在一个包中。您可以发送以下电子邮件:

Mail email = new Mail();
email.setReceiverId("");
email.setSenderId("");
email.setPassword("");
email.setSubject("");
email.setBody("");
email.sendMail();

注意根据您的提供商更改MailSender类中的smtp设置。我用过zoho邮件。

以上是关于javax.mail无法在Marshmallow或更高版本的系统上运行的主要内容,如果未能解决你的问题,请参考以下文章

javax.mail.MessagingException:PKIX 路径构建失败:SunCertPathBuilderException:无法找到请求目标的有效证书路径;

无法解析导入javax.mail

无法使用 SMTP 发送电子邮件(获取 javax.mail.MessagingException:无法将套接字转换为 TLS;)

java.lang.ClassFormatError:类文件 javax/mail/MessagingException 中非本机或抽象方法中的缺失代码属性

javax.mail.MessagingException:AppEngine 应用程序中的连接错误

包 javax.mail 和 javax.mail.internet 不存在