Java 邮件发送中 API 邮件没有发送..挂断程序

Java Email sending API email did not send.. hang up the program

我编写了一个 java 电子邮件发送程序。但是当我点击发送按钮时,按钮出现挂起模式&程序仍然运行,但是邮件没有发送。 我无法检测到问题。有谁能够帮我... 代码如下。

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.starttls.enable", "false");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("myid@gmail.com", "password");
                }
            }
    );

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("myid@gmail.com"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("senderid@gmail.com"));
        message.setSubject("Demo mail");
        message.setText("Hello, world!");
        Transport.send(message);

        JOptionPane.showMessageDialog(this, "Message sent!");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, e);
    }

我的电子邮件帐户没有激活两步验证服务。 并且在outlook邮件发送软件中也可以使用。。我测试过。

但我的 java 程序不起作用。

我认为应该扩展身份验证器 class。这是一个对我有用的例子:

public class SendEmail {

    public SendEmail () {}

    public void send (String text){
        String host = "smtp.gmail.com";
        String username = "user@email.com";
        String password = "password";
        Properties props = new Properties();
        // set any needed mail.smtps.* properties here
        Session session = Session.getInstance(props, new GMailAuthenticator("user", "password"));
        Message msg = new MimeMessage(session);
        Transport t;
        try {
            msg.setText(text);
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress("stackkinggame@gmail.com", "Stack King"));
            t = session.getTransport("smtps");
            t.connect(host, username, password);
            t.sendMessage(msg, msg.getAllRecipients());
            t.close();
            Gdx.app.log("Email", "Message sent successfully.");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    class GMailAuthenticator extends Authenticator {
        String user;
        String pw;
        public GMailAuthenticator (String username, String password)
        {
            super();
            this.user = username;
            this.pw = password;
        }
        public PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(user, pw);
        }
    }
}

首先,修复程序中的所有 common JavaMail mistakes

其次,由于您使用的是 Gmail,请确保您已 enabled support for less secure apps

最后,您需要提供比 "it doesn't work" 更多的详细信息。 JavaMail debug output 会有所帮助。