首先,需要在pom.xml中添加邮件相关的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

然后,创建一个MailUtil工具类:

@Component
public class MailUtil {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;

    public void sendMail(String to, String subject, String content) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        mailSender.send(message);
    }
}

其中,@Value注解用于获取配置文件中的发送邮件的用户名,@Autowired注解用于注入JavaMailSender实例,sendMail方法用于发送邮件。

接着,在application.properties中进行SMTP配置:

# SMTP配置
spring.mail.host=smtp.qq.com
spring.mail.port=587
spring.mail.username=your_username@qq.com
spring.mail.password=your_password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

其中,spring.mail.host和spring.mail.port分别为SMTP服务器的地址和端口号;spring.mail.username和spring.mail.password分别为发送邮件的用户名和密码;spring.mail.properties.mail.smtp.auth和spring.mail.properties.mail.smtp.starttls.enable为SMTP认证和STARTTLS协议的开关。

最后,在Controller中调用MailUtil工具类发送邮件:

@RestController
public class MailController {

    @Autowired
    private MailUtil mailUtil;

    @PostMapping("/sendMail")
    public String sendMail(@RequestParam String to, @RequestParam String subject, @RequestParam String content) {
        try {
            mailUtil.sendMail(to, subject, content);
            return "发送成功";
        } catch (MessagingException e) {
            e.printStackTrace();
            return "发送失败";
        }
    }
}

前端只需要一个button按钮,点击按钮即可发送邮件,可以使用jQuery的ajax方法实现:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>发送邮件</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
    <input type="text" id="to" placeholder="收件人邮箱"><br>
    <input type="text" id="subject" placeholder="邮件主题"><br>
    <textarea id="content" placeholder="邮件内容"></textarea><br>
    <button id="sendMail">发送</button>
    <script>
        $(function () {
            $('#sendMail').click(function () {
                var to = $('#to').val();
                var subject = $('#subject').val();
                var content = $('#content').val();
                $.ajax({
                    url: '/sendMail',
                    type: 'POST',
                    data: {
                        to: to,
                        subject: subject,
                        content: content
                    },
                    success: function (data) {
                        alert(data);
                    },
                    error: function () {
                        alert('发送失败');
                    }
                });
            });
        });
    </script>
</body>
</html>

这样就完成了一个简单的发送邮件功能

spring boot编写一个发送邮件工具类并进行SMTP配置controller调用工具类并将发送邮件信息写到action中前端只有一个button按钮点击发送即可

原文地址: https://www.cveoy.top/t/topic/goCs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录