spring整合freemarker发送邮件例子
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
在IT行业中,Spring框架是Java应用开发的基石,它提供了丰富的功能来简化企业级应用的构建。Freemarker则是一个强大的模板引擎,常用于生成HTML或其他文本格式的动态内容。结合这两者,我们可以构建出高效的邮件发送系统。本文将详细探讨如何在Spring框架中整合Freemarker,实现邮件的发送,包括在HTML邮件中内嵌图片、添加附件以及解决字符编码问题。 我们需要在Spring项目中引入相关的依赖。对于Spring,可以选择Spring Boot项目,因为它集成了许多必要的依赖,如Spring MVC和JavaMailSender。对于Freemarker,需要在`pom.xml`文件中添加Freemarker和JavaMail的相关依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency> </dependencies> ``` 接下来,配置Spring的JavaMailSender。在`application.properties`或`application.yml`中添加以下配置: ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=your-email@example.com spring.mail.password=your-password spring.mail.protocol=smtp spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 然后创建一个邮件服务类,用于处理邮件发送。在这个类中,我们将使用Freemarker模板生成HTML内容,并通过JavaMailSender发送邮件: ```java @Service public class EmailService { @Autowired private JavaMailSender mailSender; @Autowired private FreeMarkerTemplateEngine templateEngine; public void sendEmail(String to, String subject, Map<String, Object> model) { // 创建邮件信息 SimpleMailMessage message = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setTo(to); helper.setSubject(subject); // 使用Freemarker生成HTML邮件内容 String htmlContent = templateEngine.process("email-template.ftl", model); helper.setText(htmlContent, true); // 添加内嵌图片 addInlineImage(helper, "image.jpg", "image/jpeg"); // 添加附件 addAttachment(helper, "attachment.txt", "text/plain"); } }; // 发送邮件 mailSender.send(message); } private void addInlineImage(MimeMessageHelper helper, String imageFileName, String contentType) throws MessagingException { FileSystemResource file = new FileSystemResource(new File("path/to/" + imageFileName)); helper.addInline(imageFileName, file, contentType); } private void addAttachment(MimeMessageHelper helper, String attachmentFileName, String contentType) throws MessagingException { FileSystemResource file = new FileSystemResource(new File("path/to/" + attachmentFileName)); helper.addAttachment(attachmentFileName, file); } } ``` 在上述代码中,`email-template.ftl`是Freemarker模板文件,用于定义邮件的HTML结构。你可以根据需求设计模板,例如: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>${subject}</title> </head> <body> <h1>${greeting}</h1> <img src="cid:image.jpg" alt="Embedded Image"> <p>${message}</p> </body> </html> ``` 在这个模板中,`${greeting}`、`${message}`等变量将在运行时被模型对象中的数据替换。`cid:image.jpg`是内嵌图片的引用,与`addInlineImage`方法中使用的ID相匹配。 至于乱码问题,确保在创建MimeMessageHelper时设置`true`作为第二个参数,这样可以确保邮件正文以HTML格式发送。此外,确保邮件内容和附件的文件名、内容都使用正确的字符编码(通常是UTF-8)。 通过Spring和Freemarker的整合,我们可以实现一个功能丰富的邮件发送系统,包括发送HTML邮件、内嵌图片、添加附件,并有效处理字符编码问题。这为开发者提供了一种灵活且强大的方式来创建和发送自定义的电子邮件。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![war](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/RAR.png)
- 1
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 11
- 资源: 141
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 打包和分发Rust工具.pdf
- SQL中的CREATE LOGFILE GROUP 语句.pdf
- C语言-leetcode题解之第172题阶乘后的零.zip
- C语言-leetcode题解之第171题Excel列表序号.zip
- C语言-leetcode题解之第169题多数元素.zip
- ocr-图像识别资源ocr-图像识别资源
- 图像识别:基于Resnet50 + VGG16模型融合的人体细胞癌症分类模型实现-图像识别资源
- C语言-leetcode题解之第168题Excel列表名称.zip
- C语言-leetcode题解之第167题两数之和II-输入有序数组.zip
- C语言-leetcode题解之第166题分数到小数.zip
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)