Spring: 发送 HTML 邮件
Spring: Send HTML mail
我想创建一个服务,使用 Spring 电子邮件和 MimeMessage
发送 HTML 邮件。这是我的服务:
public void sendHtmlEmail(String receiver,String Subject, String htmlBody) throws MessagingException {
MimeMessage msg = javaMailSender.createMimeMessage();
// true = multipart message
MimeMessageHelper helper = new MimeMessageHelper(msg, false);
helper.setTo(receiver);
helper.setSubject(Subject);
// true = text/html
helper.setText(htmlBody, true);
javaMailSender.send(msg);
}
问题是我在 html 中没有收到电子邮件,但在 html 中收到了标签,我知道我在方法 setText()
中设置了 true !我发送的电子邮件以纯文本 html 显示,如下所示
<html><h1>some text !</h1></html>
一些可以帮助您的链接:
https://mkyong.com/spring-boot/spring-boot-how-to-send-email-via-smtp/
https://www.baeldung.com/spring-email
这是 application.properties :
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password
# Other properties
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
# TLS , port 587
spring.mail.properties.mail.smtp.starttls.enable=true
和控制器
@PostMapping("/htmlMail")
public String sendHtmlMail(@RequestBody MailDTO mail) {
mailService.sendHtmlEmail(mail.getReceiver(),mail.getSubject(),mail.getHtmlbody());
return "html E-Mail Sent ! ";
}
有时需要的参数不存在,所以会报错。在你的问题中,没有给出完整的代码或错误截图,所以我在路上描述。
先检查以下两点,
- 已提供所有必需的配置数据。
- 您的电子邮件 ID 必须有权使用您的应用程序发送邮件(允许 "less secure app" 在您的 gmail 帐户中 - 如果您正在使用)。
Yml 属性文件
mail:
host: smtp.gmail.com // Take based on your mail provider
port: 587
username: *@gmail.com
password: ****
transport:
protocol: smtp
properties:
test-connection: true
debug: true
smtp:
auth: true
starttls:
enable: true
required: true
ssl:
enable: true
代码片段
@Autowired
JavaMailSender mailSender;
public void sendMail(String to, String subject, String content, boolean
isMultipart, boolean isHtml){
MimeMessage mimeMessage = mailSender.createMimeMessage();
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(EMAIL_HOST);
mailSender.setPort(EMAIL_PORT);
mailSender.setUsername(EMAIL_USERNAME);
mailSender.setPassword(EMAIL_PASSWORD);
Properties properties = mailSender.getJavaMailProperties();
properties.put("mail.smtp.starttls.enable", Boolean.TRUE);
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.auth", Boolean.TRUE);
properties.put("mail.smtp.starttls.required", Boolean.TRUE);
properties.put("mail.smtp.ssl.enable", Boolean.FALSE);
properties.put("mail.test-connection", Boolean.TRUE);
properties.put("mail.debug", Boolean.TRUE);
mailSender.setJavaMailProperties(properties);
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, isMultipart, "UTF-8");
messageHelper.setFrom(USER_EMAIL);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(content, isHtml);
mailSender.send(mimeMessage);
} catch (Exception ex) {
log.warn("Email could not be sent to user '{}': {}", to, ex.getMessage());
}
}
来电
@Async
public void sendTestingMail(String mail) {
String subject = "Test mail from Project Management System";
String content = "<h1>Be happy, Enjoy Life...!!!</h1>";
sendMail(mail, subject, content, false, true);
}
您设置一次并使用多个地方的电子邮件配置,因此请尝试进行单一的完整设置。
祝你有愉快的一天...!!!
我想创建一个服务,使用 Spring 电子邮件和 MimeMessage
发送 HTML 邮件。这是我的服务:
public void sendHtmlEmail(String receiver,String Subject, String htmlBody) throws MessagingException {
MimeMessage msg = javaMailSender.createMimeMessage();
// true = multipart message
MimeMessageHelper helper = new MimeMessageHelper(msg, false);
helper.setTo(receiver);
helper.setSubject(Subject);
// true = text/html
helper.setText(htmlBody, true);
javaMailSender.send(msg);
}
问题是我在 html 中没有收到电子邮件,但在 html 中收到了标签,我知道我在方法 setText()
中设置了 true !我发送的电子邮件以纯文本 html 显示,如下所示
<html><h1>some text !</h1></html>
一些可以帮助您的链接:
https://mkyong.com/spring-boot/spring-boot-how-to-send-email-via-smtp/
https://www.baeldung.com/spring-email
这是 application.properties :
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password
# Other properties
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
# TLS , port 587
spring.mail.properties.mail.smtp.starttls.enable=true
和控制器
@PostMapping("/htmlMail")
public String sendHtmlMail(@RequestBody MailDTO mail) {
mailService.sendHtmlEmail(mail.getReceiver(),mail.getSubject(),mail.getHtmlbody());
return "html E-Mail Sent ! ";
}
有时需要的参数不存在,所以会报错。在你的问题中,没有给出完整的代码或错误截图,所以我在路上描述。
先检查以下两点,
- 已提供所有必需的配置数据。
- 您的电子邮件 ID 必须有权使用您的应用程序发送邮件(允许 "less secure app" 在您的 gmail 帐户中 - 如果您正在使用)。
Yml 属性文件
mail:
host: smtp.gmail.com // Take based on your mail provider
port: 587
username: *@gmail.com
password: ****
transport:
protocol: smtp
properties:
test-connection: true
debug: true
smtp:
auth: true
starttls:
enable: true
required: true
ssl:
enable: true
代码片段
@Autowired
JavaMailSender mailSender;
public void sendMail(String to, String subject, String content, boolean
isMultipart, boolean isHtml){
MimeMessage mimeMessage = mailSender.createMimeMessage();
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(EMAIL_HOST);
mailSender.setPort(EMAIL_PORT);
mailSender.setUsername(EMAIL_USERNAME);
mailSender.setPassword(EMAIL_PASSWORD);
Properties properties = mailSender.getJavaMailProperties();
properties.put("mail.smtp.starttls.enable", Boolean.TRUE);
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.auth", Boolean.TRUE);
properties.put("mail.smtp.starttls.required", Boolean.TRUE);
properties.put("mail.smtp.ssl.enable", Boolean.FALSE);
properties.put("mail.test-connection", Boolean.TRUE);
properties.put("mail.debug", Boolean.TRUE);
mailSender.setJavaMailProperties(properties);
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, isMultipart, "UTF-8");
messageHelper.setFrom(USER_EMAIL);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(content, isHtml);
mailSender.send(mimeMessage);
} catch (Exception ex) {
log.warn("Email could not be sent to user '{}': {}", to, ex.getMessage());
}
}
来电
@Async
public void sendTestingMail(String mail) {
String subject = "Test mail from Project Management System";
String content = "<h1>Be happy, Enjoy Life...!!!</h1>";
sendMail(mail, subject, content, false, true);
}
您设置一次并使用多个地方的电子邮件配置,因此请尝试进行单一的完整设置。
祝你有愉快的一天...!!!