【问题标题】:Gmail smtp with JavaMailSenderImpl带有 JavaMailSenderImpl 的 Gmail smtp
【发布时间】:2013-03-01 19:07:10
【问题描述】:

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

【问题讨论】:

    标签: java spring smtp gmail


    【解决方案1】:

    检查您是否启用了对您帐户的 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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      相关资源
      最近更新 更多