Javax.mail包在发布模式apk下无效

时间:2017-08-01 10:33:46

标签: java android javamail

我正在使用javax.mail包进行邮件发送, 在调试模式下应用程序,它工作正常,并将每封邮件发送到我的帐户,但当我创建发布应用程序邮件时,停止发送表单应用程序。 代码如下:

   Properties props = new Properties();
    props.put("mail.smtp.user", "abc@xyz.com");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    //Creating a new session

    session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        //Authenticating the password
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("abc@xyz.com", "password@123");
        }
    });
    try {
        //Creating MimeMessage object
        MimeMessage mm = new MimeMessage(session);

        //Setting sender address
        mm.setFrom(new InternetAddress("abc@xyz.com"));
        //Adding receiver
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress("abc@xyz.com"));
        //Adding subject
        mm.setSubject(subject);
        //Adding message
        mm.setText(message);

        //Sending email
        Transport.send(mm);

    } catch (MessagingException e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

通常这个问题是因为proguard而发生的,如果你将proguard添加到你的项目中它会停止这样的一些功能。

我是通过在proguard-rules.pro文件中添加包javax来实现的。

  

-keep class javax。** {*;}

这将在proguard中添加所有javax方法的权限。