Spring Mail Sender使用Gmail SMTP错误。我怎么修好?

时间:2018-06-01 04:48:27

标签: spring-boot

我之前从未使用过它。我跑不了。我有错误列表

这是我的应用程序properties.spring.mail.username我可以使用什么?我的Gmail帐号和密码对不对?

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=xxxx@gmail.com
spring.mail.password=xxxxx

#mail properties
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

这是我的主要课程

@SpringBootApplication
public class MarterTravelApplication extends SpringBootServletInitializer implements CommandLineRunner {

    @Autowired
    @Qualifier("javasampleapproachMailSender")
    public MailSender mailSender;
    public static void main(String[] args) {
        SpringApplication.run(MarterTravelApplication.class, args);

    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MarterTravelApplication.class);
    }

    public void run(String... arg0) throws Exception {

        String from = "xxxxxx@gmail.com";
        String to = "xxxxx@gmail.com";
        String subject = "JavaMailSender";
        String body = "Just-Testing!";

        mailSender.sendMail(from, to, subject, body);
    }

然后我的邮件发件人类

package com.marter.travel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component("javasampleapproachMailSender")
public class MailSender {

    @Autowired
    JavaMailSender javaMailSender;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    public void sendMail(String from, String to, String subject, String body) {

        SimpleMailMessage mail = new SimpleMailMessage();

        mail.setFrom(from);
        mail.setTo(to);
        mail.setSubject(subject);
        mail.setText(body);

        logger.info("Sending...");

        javaMailSender.send(mail);

        logger.info("Done!");
    }
}

我的错误代码就像javax.mail.AuthenticationFailedException如何修复该错误

org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.8.RELEASE.jar:1.5.8.RELEASE]
Caused by: org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtJ
534-5.7.14 jo4gUzPM-ZGj5a92U0ymdgCJ6Zu00ISucB5c1CQLTB1zwZM3QY6KKIIXcxDHVOW4cwQXXc
534-5.7.14 tTNoxOzv8WMBebiW06gwl_J9nhMY1krJtWud9R-3ZrZvym73YvCXLRy5yjhcFg3jP25rme
534-5.7.14 Lq02oyvHyD79OexP-qqsH3eudTCcIeKr8siLZA8a4aH-fekwGDyJXpeCihR8Rq2OsDiyXO
534-5.7.14 -1GfeQloALfGx7kWKoF6LLl-o3p7s> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 z129-v6sm61960461pfb.108 - gsmtp

    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:424) ~[spring-context-support-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307) ~[spring-context-support-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296) ~[spring-context-support-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at com.marter.travel.MailSender.sendMail(MailSender.java:29) ~[classes/:na]
    at com.marter.travel.MarterTravelApplication.run(MarterTravelApplication.java:34) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    ... 11 common frames omitted
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtJ
534-5.7.14 jo4gUzPM-ZGj5a92U0ymdgCJ6Zu00ISucB5c1CQLTB1zwZM3QY6KKIIXcxDHVOW4cwQXXc
534-5.7.14 tTNoxOzv8WMBebiW06gwl_J9nhMY1krJtWud9R-3ZrZvym73YvCXLRy5yjhcFg3jP25rme
534-5.7.14 Lq02oyvHyD79OexP-qqsH3eudTCcIeKr8siLZA8a4aH-fekwGDyJXpeCihR8Rq2OsDiyXO
534-5.7.14 -1GfeQloALfGx7kWKoF6LLl-o3p7s> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 z129-v6sm61960461pfb.108 - gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:932) ~[javax.mail-1.5.6.jar:1.5.6]
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:843) ~[javax.mail-1.5.6.jar:1.5.6]
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:748) ~[javax.mail-1.5.6.jar:1.5.6]
    at javax.mail.Service.connect(Service.java:366) ~[javax.mail-1.5.6.jar:1.5.6]
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:501) ~[spring-context-support-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:421) ~[spring-context-support-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    ... 16 common frames omitted

3 个答案:

答案 0 :(得分:1)

转到https://myaccount.google.com/lesssecureapps 并打开“允许不太安全的应用程序”: 为我工作

答案 1 :(得分:0)

使用以下属性:

mail.host=smtp.gmail.com
mail.port=587
mail.username=<gmail-username@gmail.com>
mail.password=<gmail-password>
mail.transport.protocol=smtp
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.from.email=<gmail-username@gmail.com>

默认情况下,gmail不允许安全性较低的应用进行身份验证。您需要打开Gmail帐户中的选项,以便让安全性较低的应用程序获得身份验证。

请按照以下步骤操作:

1.Login to Gmail. 
2.Access the URL as https://www.google.com/settings/security/lesssecureapps 
3.Select "Turn on"

再次运行你的春季启动应用程序。

答案 2 :(得分:0)

只需转到链接Less secure app access

然后允许运行安全性较低的应用程序。 #happy编码:)