我看到了类似的问题,但没有一个能帮助我。我将使用javaMailSender
发送电子邮件。虽然,我确信,我已经使用了有效的凭证,它抱怨我的用户名和密码不正确。所以这是属性:
spring.mail.host=smtp.gmail.com
spring.mail.username=myemail@gmail.com
spring.mail.password= mypassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class= javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.fallback= false
spring.mail.properties.mail.smtp.ssl.enable=true
然后在sender类中创建一个Bean进行注入:
@Configuration
public class MailConfig {
@Autowired
JavaMailSender javaMailSender;
@Bean
@ConditionalOnProperty("spring.mail.host")
public SmtpkMailSender getMockMailSender(){
return new SmtpkMailSender(javaMailSender);
}
}
发件人类只有一个方法来自以下界面:
public interface MailSender {
void sender(String to, String subject,String body);
}
最后,这是发件人:
public class SmtpkMailSender implements MailSender {
static Log log=LogFactory.getLog(MailSender.class);
JavaMailSender javaMailSender;
public SmtpkMailSender(JavaMailSender javaMailSender){
this.javaMailSender=javaMailSender;
}
public void sender(String to, String subject,String body){
MimeMessage message=javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper=new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(body);
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
因为我确定
spring.mail.username=myemail@gmail.com spring.mail.password = mypassword
我转到https://myaccount.google.com/u/1/lesssecureapps?pageId=none链接 并将其设置为 on ,但它仍会抱怨以下错误:
这是错误:
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials n29sm260426lfi.27 - gsmtp
答案 0 :(得分:0)
尝试重置您的电子邮件密码,然后尝试使用新密码登录。另外,请确保您的用户名(电子邮件地址)最后包含@gmail.com
。另外,请确保您的用户名和密码是字符串(用引号表示)。
示例:
spring.mail.username = "myemail@gmail.com"
spring.mail.password= "mypassword"