1、创建邮件发送器
首先定义JavaMailSenderImpl对象,并对其进行smtp相关信息设置,相当于我们自己的邮箱,如下:
-
JavaMailSenderImpl mailSender =newJavaMailSenderImpl();
-
mailSender.setHost("smtp.qq.com");
-
mailSender.setUsername("mosaic@qq.com");
-
mailSender.setPassword("asterisks");
当然更好的方法是使用配置文件进行配置,这里只是进行介绍,忽略硬编码先。
以上主机为邮箱服务商的smtp地址,用户名、密码为用户自己的邮箱。除以上外还可以设置
setPort(int port) 、setProtocol(String protocol) 等,可暂时不考虑。
这样我们便类似创建好了一个邮件发送器
2、 开始写邮件,编写邮件内容
JavaMailSenderImpl支持MimeMessages和
SimpleMailMessages。
MimeMessages为复杂邮件模板,支持文本、附件、html、图片等。
SimpleMailMessages实现了MimeMessageHelper,为普通邮件模板,支持文本。
下面先以SimpleMailMessages为例进行介绍
-
SimpleMailMessage smm =newSimpleMailMessage();
-
// 设定邮件参数
-
smm.setFrom(mailSender.getUsername());
-
smm.setTo("mosaic@126.com");
-
smm.setSubject("Hello world");
-
smm.setText("Hello world via spring mail sender");
-
// 发送邮件
-
mailSender.send(smm);
如此,我们便完成了一个简单邮件的编写,对于复杂邮件,编写及发送如下
-
//使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容
-
MimeMessage msg = mailSender.createMimeMessage();
-
//创建MimeMessageHelper对象,处理MimeMessage的辅助类
-
MimeMessageHelper helper =newMimeMessageHelper(msg,true);
-
//使用辅助类MimeMessage设定参数
-
helper.setFrom(mailSender.getUsername());
-
helper.setTo("mosaic@126.com");
-
helper.setSubject("Hello Attachment");
-
helper.setText("This is a mail with attachment");
-
//加载文件资源,作为附件
-
ClassPathResource file =newClassPathResource("Chrysanthemum.jpg");
-
//加入附件
-
helper.addAttachment("attachment.jpg", file);
-
// 发送邮件
-
mailSender.send(smm);
其中MimeMessageHelper为的辅助类MimeMessages。以上包含了以资源文件为附件进行发送。对于普通文件发送方式如下:
Java代码
-
FileSystemResource file =newFileSystemResource("C:\\Users\\image1.jpg");
-
helper.addInline("file", file);
3、发送邮件
2中已经包含了发送的代码,只需使用JavaMailSenderImpl的send接口即可。支持类型为
-
voidsend(MimeMessage mimeMessage)
-
Send the given JavaMail MIME message.
-
voidsend(MimeMessage[] mimeMessages)
-
Send the given array of JavaMail MIME messages in batch.
-
voidsend(MimeMessagePreparator mimeMessagePreparator)
-
Send the JavaMail MIME message prepared by the given MimeMessagePreparator.
-
voidsend(MimeMessagePreparator[]
mimeMessagePreparators)
-
Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.
-
voidsend(SimpleMailMessage simpleMessage)
-
Send the given simple mail message.
-
voidsend(SimpleMailMessage[] simpleMessages)
-
Send the given array of simple mail messages in batch.
下面介绍下怎么发送富文本文件以及以velocity为模板发送邮件。
4、发送html文件
只需要在MimeMessageHelpersetText时将是否是html设为true即可。setText介绍如下:
-
setText(String text, boolean html)
-
Set the given text directly as content in non-multipart mode or as default body part in multipart mode.
示例代码(包括附件)如下:
-
//第二个参数true,表示text的内容为html
-
//注意<img/>标签,src='cid:file','cid'是contentId的缩写,'file'是一个标记,需要在后面的代码中调用MimeMessageHelper的addInline方法替代成文件
-
helper.setText("<body><p>Hello Html Email</p><img src='cid:file'/></body>",true);
-
FileSystemResource file =newFileSystemResource("C:\\Users\\image1.jpg");
-
helper.addInline("file", file);
分享到:
相关推荐
通过spring类,发送邮件信息!包含相应的jar包,有实例
java 发送邮件 spring发送邮件Mail
利用spring下的一个类进行邮件发送,内附具体用法
spring boot发送普通文本邮件/HTML邮件/附件邮件/图片邮件完整代码.zip
邮件发送支持以 HTML 的形式去构建我们喜欢的文本格式,Spring 对 HTML 格式的邮件也做出了支持,非常方便使用。 我们在 MailService 中添加支持 HTML 发送的方法. Spring Boot整合邮件发送并保存历史发送邮箱 和...
Spring邮件发送服务(java邮件发送),支持多附件、多人发送/抄送/暗送
使用Spring Boot 开发支持多附件邮件发送微服务平台代码
java发送邮件示例源码,支持html格式邮件,接收人,邮件主题,邮件内容,附件路径 #邮件 spring.mail.default-encoding=utf-8 # 配置SMTP服务器地址 spring.mail.host=smtp.qq.com #发送方邮件名 spring.mail....
3 JavaMailSenderImpl+SimpleMailMessage+BodyPart+Multipart:获得模版邮件body并发送邮件。 4 包含简单项目+所需要的jar。 5 说明:项目只是一个模版,不能直接运行测试,如果要测试,自己建个project去测试。
主要介绍了Spring Boot中利用JavaMailSender发送邮件的方法示例, 相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置...
Spring实现各种邮件的发送 简单配置就可发送邮件
使用springmvc 调用.JavaMailSenderImpl 发送邮件
数据库:sqlserver CreateHtmlTable中的生成路径和SendService中的上传路径改成自己的
java发送邮件(两种方式-springcloud方式+静态调用方式)
用法查看 https://blog.csdn.net/qq_32371887/article/details/72821291 注意配置的邮箱不要被标记为垃圾邮件
编程语言+JAVAspring+邮件...它介绍了JAVAspring的邮件服务的概念、原理和作用,以及如何使用JAVAspring的邮件服务来发送邮件,包括邮件的配置、构建、发送、接收、附件、模板等内容,以及一些配置文件和注解的用法。
用spring实现了定时发送邮件,无需复杂的代码
Spring mail发送邮件实例 Spring mail发送邮件实例
使用Spring mail发送邮件的完整实例,包含代码与Spring mail的jar,可直接运行与使用。相关学习文档参考博客《Spring Mail邮件发送 http://blog.csdn.net/chenxiang0207/article/details/8256528》。