带有JavaMailSenderImpl的Gmail smtp

时间:2013-03-01 19:07:11

标签: java spring smtp gmail

我正在使用以下代码以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()]));

1 个答案:

答案 0 :(得分: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");