无法通过smtp.gmail.com发送电子邮件,而无法启用“访问安全性较低的应用程序”标记

时间:2017-02-15 09:05:12

标签: spring email spring-boot smtp gmail

如果“ Access for less secure apps ”未启用,则Gmail会阻止发送电子邮件,

我使用spring-boot并使用class发送电子邮件:

org.springframework.mail.javamail.JavaMailSenderImpl

它失败并显示消息:

org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 534-5.7.14

仅当我启用“ Access for less security apps ”标志时,它才有效,我想发送电子邮件而不启用该标志,是否有任何建议?

这是我的配置:

@Bean
public JavaMailSender emailSender() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost("smtp.gmail.com");
    javaMailSender.setPort(465);
    javaMailSender.setJavaMailProperties(getMailProperties());
    javaMailSender.setUsername(from);
    javaMailSender.setPassword(password);
    return javaMailSender;
}


    private Properties getMailProperties() {
    Properties properties = new Properties();
    properties.setProperty("mail.transport.protocol", "smtp");
    properties.setProperty("mail.smtp.auth", "true");
    properties.setProperty("mail.smtp.starttls.enable", "true");
    properties.setProperty("mail.debug", "false");
    properties.setProperty("mail.smtp.starttls.required", "true");
    properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    return properties;
}

0 个答案:

没有答案