如何使用Spring Boot发送邮件?

时间:2019-09-18 06:43:09

标签: spring spring-boot javamail spring-boot-test

`

`我正在尝试使用springboot应用程序发送邮件...但是由于无法转换为TLS异常和sslhandshake异常而无法发送邮件。

我也尝试使用端口25、465进行更改,但是没有用。在application.properties中应该怎么做?

package com.Spring_boots;



import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.MimeMessageHelper;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


@RestController

public class SimpleMailController {




  @Autowired
    private JavaMailSender sender;

    @RequestMapping("/sendMail")
    public String sendMail() {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);

        try {
            helper.setTo("imsubhankar.sd2@gmail.com");
            helper.setText("Greetings :)");
            helper.setSubject("Mail From Spring Boot");
        } catch (MessagingException e) {
            e.printStackTrace();
            return "Error while sending mail ..";
        }
        sender.send(message);
        return "Mail Sent Success!";
    }
}

0 个答案:

没有答案