0

我正在使用以下代码用 Java 发送电子邮件。我没有收到任何错误,但电子邮件似乎没有到达(即使是垃圾邮件,我已经等了将近一天了)

List<SimpleMailMessage> messages = new ArrayList<>();
...
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("smtp.gmail.com");
sender.setPort(587);
sender.setUsername("mysecretemail@gmail.com");
sender.setPassword("mysecretpassw0rd");
sender.setProtocol("smtp");
Properties mailProperties = new Properties();
mailProperties.setProperty("mail.smtp.auth", "true");
mailProperties.setProperty("mail.smtp.starttls.enable", "true");
mailProperties.setProperty("mail.smtp.quitwait", "false");

mailProperties.setProperty("mail.debug", "true");
sender.setJavaMailProperties(mailProperties);
sender.send(messages.toArray(new SimpleMailMessage[messages.size()]));
4

1 回答 1

0

检查您是否启用了对您帐户的 POP/IMAP 访问,如果我没记错的话,必须启用它才能使 SMTP 正常工作。至于 java 邮件属性,这个代码在几年前曾经工作过:

    Session mailSession = Session.getDefaultInstance(new Properties(), null);
    Properties props = mailSession.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "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");
于 2013-03-01T23:20:26.007 回答