Java Spring Boot 发送邮件工具类 - 使用 Thymeleaf 和 MailUtil
以下是一个简单的发送邮件的工具类,使用JavaMail API实现。在Spring Boot中使用时,需要在pom.xml文件中添加相关依赖,并在application.properties文件中配置邮件服务器信息。
package com.example.mail;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailUtil {
private static final String SMTP_HOST = 'smtp.gmail.com'; // 邮件服务器
private static final String SMTP_PORT = '465'; // 端口号
private static final String USERNAME = 'your_email@gmail.com'; // 发送邮件的账号
private static final String PASSWORD = 'your_password'; // 发送邮件的密码
/**
* 发送邮件
* @param to 收件人邮箱
* @param subject 邮件主题
* @param content 邮件内容
* @throws AddressException
* @throws MessagingException
*/
public static void sendMail(String to, String subject, String content) throws AddressException, MessagingException {
Properties props = new Properties();
props.setProperty('mail.transport.protocol', 'smtp');
props.setProperty('mail.smtp.host', SMTP_HOST);
props.setProperty('mail.smtp.port', SMTP_PORT);
props.setProperty('mail.smtp.auth', 'true');
props.setProperty('mail.smtp.ssl.enable', 'true');
props.setProperty('mail.smtp.ssl.trust', SMTP_HOST);
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(USERNAME));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
message.setText(content);
Transport.send(message);
}
}
在Thymeleaf视图中,可以使用以下代码调用上述工具类发送邮件:
<!DOCTYPE html>
<html xmlns:th='http://www.thymeleaf.org'>
<head>
<meta charset='UTF-8'>
<title>Send Mail</title>
</head>
<body>
<form method='post' th:action='@{/send}'>
<label for='to'>To:</label>
<input type='email' id='to' name='to' required>
<br>
<label for='subject'>Subject:</label>
<input type='text' id='subject' name='subject' required>
<br>
<label for='content'>Content:</label>
<textarea id='content' name='content' rows='10' required></textarea>
<br>
<button type='submit'>Send</button>
</form>
</body>
</html>
在Spring Boot中,可以在Controller中添加以下代码处理发送邮件的请求:
package com.example.controller;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.mail.MailUtil;
@Controller
public class MailController {
@PostMapping('/send')
public String sendMail(@RequestParam('to') String to,
@RequestParam('subject') String subject,
@RequestParam('content') String content) {
try {
MailUtil.sendMail(to, subject, content);
return 'success';
} catch (AddressException e) {
e.printStackTrace();
return 'error';
} catch (MessagingException e) {
e.printStackTrace();
return 'error';
}
}
}
以上是一个简单的发送邮件的工具类和使用Thymeleaf视图和Spring Boot处理请求的示例。需要注意的是,在实际应用中可能需要对邮件内容进行验证和格式化处理。
原文地址: https://www.cveoy.top/t/topic/omYz 著作权归作者所有。请勿转载和采集!