Spring-boot-mail MailSendException,无法连接到主机

时间:2016-12-09 12:00:27

标签: java spring spring-boot gmail javamail

我想在我的应用程序中尝试javaMailSender。但我无法连接到smtp服务器,当我尝试发送消息时,我收到此错误:

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect; message exception details (1) are:
Failed message 1:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;

以下是application.properties

中的属性
spring.mail.host = smtp.gmail.com
spring.mail.username = XXXXXXXXXXXXXXXXX
spring.mail.password = XXXXXXXXXXXXXXXXX
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.socketFactory.fallback = false
spring.mail.properties.mail.smtp.ssl.enable = true

和MailSender:

@Service
public class MailHelper {

@Autowired
private JavaMailSender javaMailSender;

@Autowired 
private MailContentBuilder builder;

public void sendMail(String destination, String body){
    try {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setSubject("Price Tracer : Notify");
        helper.setTo(destination);
        String[] args = body.split(",");
        helper.setText(builder.build(args[0],args[1],args[2]),true);

        javaMailSender.send(message);

    } catch (MessagingException e) {
        System.out.println("Printer");
    }
}
}

0 个答案:

没有答案