Java邮件无法正常工作

时间:2017-02-07 20:36:49

标签: java android email javamail

按下按钮时,我正在使用java邮件发送自动邮件。 我在android studio上工作,我收到错误javax.mail.AuthenticationFailedException,我找不到原因。

  • 我尝试将端口更改为25 - 587和465
  • 我是正确的
  • 尝试使用模拟器和真实设备
  • 我的Gmail帐户对低安全应用程序开放

这是代码:

Properties props = new Properties();
        props.put("mail.smtp.host" , "smtp.gmail.com");
        props.put("mail.stmp.user" , "horiond@gmail.com");

        //TLS
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.password", "xxxxx");

        //SSL
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session  = Session.getDefaultInstance( props , null);
        String to = "horiond@gmail.com";
        String from = "horiond@gmail.com";
        String subject = "Testing...";
        Message msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            msg.setSubject(subject);
            msg.setText("Working fine..!");
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.gmail.com" , 465  , "horiov@gmail.com", "xxxx");
            transport.send(msg);
            System.out.println("fine!!");
        }
        catch(Exception exc) {
            System.out.println(exc);
        }

1 个答案:

答案 0 :(得分:0)

您可以在另一个问题中查看此链接。这应该可以帮助你。

Sending Email in Android using JavaMail API without using the default/built-in app

相关问题