java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板...

本文详细介绍了如何使用JavaMailSenderImpl发送邮件,包括发送一般邮件、富文本(html)邮件、以velocity为模板发送邮件的过程。主要内容分为创建邮件发送器、编写邮件和发送邮件三步,并详细说明了邮件发送器的配置、邮件内容的编写以及发送方法。此外,文章还特别介绍了如何发送富文本文件以及以velocity为模板发送邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要介绍利用JavaMailSenderImpl发送邮件。首先介绍了发送一般邮件,然后介绍了发送富文本(html)邮件以velocity为模板发送邮件。

邮件发送分为为三步:创建邮件发送器编写邮件发送邮件

Spring的JavaMailSenderImpl提供了强大的邮件发送功能,可发送普通文本邮件、带附件邮件、html格式邮件、带图片邮件、设置发送内容编码格式、设置发送人的显示名称。

下面就进行介绍,示例代码中很多都是字符串硬编码,实际使用时推荐使用spring的配置文件进行配置。

1、创建邮件发送器

首先定义JavaMailSenderImpl对象,并对其进行smtp相关信息设置,相当于我们自己的邮箱,如下:

Java代码 收藏代码
  1. JavaMailSenderImplmailSender=newJavaMailSenderImpl();
  2. mailSender.setHost("smtp.qq.com");
  3. mailSender.setUsername("xxxxxxxxx@qq.com");
  4. mailSender.setPassword("xxxxxx");

当然更好的方法是使用配置文件进行配置,这里只是进行介绍,忽略硬编码先。

以上主机为邮箱服务商的smtp地址,用户名、密码为用户自己的邮箱。除以上外还可以设置

setPort(int port) 、setProtocol(String protocol) 等,可暂时不考虑。

这样我们便类似创建好了一个邮件发送器

2、 开始写邮件,编写邮件内容

JavaMailSenderImpl支持MimeMessagesSimpleMailMessages

MimeMessages为复杂邮件模板,支持文本、附件、html、图片等。

SimpleMailMessages实现了MimeMessageHelper,为普通邮件模板,支持文本。

下面先以SimpleMailMessages为例进行介绍

Java代码 收藏代码
  1. SimpleMailMessagesmm=newSimpleMailMessage();
  2. //设定邮件参数
  3. smm.setFrom(mailSender.getUsername());
  4. smm.setTo("xxxxxxx@126.com");
  5. smm.setSubject("Helloworld");
  6. smm.setText("Helloworldviaspringmailsender");
  7. //发送邮件
  8. mailSender.send(smm);

如此,我们便完成了一个简单邮件的编写,对于复杂邮件,编写及发送如下

Java代码 收藏代码
  1. //使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容
  2. MimeMessagemsg=mailSender.createMimeMessage();
  3. //创建MimeMessageHelper对象,处理MimeMessage的辅助类
  4. MimeMessageHelperhelper=newMimeMessageHelper(msg,true);
  5. //使用辅助类MimeMessage设定参数
  6. helper.setFrom(mailSender.getUsername());
  7. helper.setTo("xxxxxx@126.com");
  8. helper.setSubject("HelloAttachment");
  9. helper.setText("Thisisamailwithattachment");
  10. //加载文件资源,作为附件
  11. ClassPathResourcefile=newClassPathResource("Chrysanthemum.jpg");
  12. //加入附件
  13. helper.addAttachment("attachment.jpg",file);
  14. //发送邮件
  15. mailSender.send(smm);

其中MimeMessageHelper为的辅助类MimeMessages。以上包含了以资源文件为附件进行发送。对于普通文件发送方式如下:

Java代码 收藏代码
  1. FileSystemResourcefile=newFileSystemResource("C:\\Users\\image1.jpg");
  2. helper.addInline("file",file);

3、发送邮件

2中已经包含了发送的代码,只需使用JavaMailSenderImpl的send接口即可。支持类型为

Java代码 收藏代码
  1. voidsend(MimeMessagemimeMessage)
  2. SendthegivenJavaMailMIMEmessage.
  3. voidsend(MimeMessage[]mimeMessages)
  4. SendthegivenarrayofJavaMailMIMEmessagesinbatch.
  5. voidsend(MimeMessagePreparatormimeMessagePreparator)
  6. SendtheJavaMailMIMEmessagepreparedbythegivenMimeMessagePreparator.
  7. voidsend(MimeMessagePreparator[]mimeMessagePreparators)
  8. SendtheJavaMailMIMEmessagespreparedbythegivenMimeMessagePreparators.
  9. voidsend(SimpleMailMessagesimpleMessage)
  10. Sendthegivensimplemailmessage.
  11. voidsend(SimpleMailMessage[]simpleMessages)
  12. Sendthegivenarrayofsimplemailmessagesinbatch.

下面介绍下怎么发送富文本文件以及以velocity为模板发送邮件。

4、发送html文件

只需要在MimeMessageHelpersetText时将是否是html设为true即可。setText介绍如下:

Xml代码 收藏代码
  1. setText(Stringtext,booleanhtml)
  2. Setthegiventextdirectlyascontentinnon-multipartmodeorasdefaultbodypartinmultipartmode.

示例代码(包括附件)如下:

Java代码 收藏代码
  1. //第二个参数true,表示text的内容为html
  2. //注意<img/>标签,src='cid:file','cid'是contentId的缩写,'file'是一个标记,需要在后面的代码中调用MimeMessageHelper的addInline方法替代成文件
  3. helper.setText("<body><p>HelloHtmlEmail</p><imgsrc='cid:file'/></body>",true);
  4. FileSystemResourcefile=newFileSystemResource("C:\\Users\\image1.jpg");
  5. helper.addInline("file",file);

5、以velocity为模板发送邮件

使用Velocity模板,需要Velocity的jar包,可以在官方网站下载,并加入ClassPath。

以velocity为模板发送邮件的原理如下:

a 类似web编程,将velocity作为前端,在java中设置vm中需要显示的变量值

b 使用VelocityEngineUtilsmergeTemplateIntoString函数将vm内容转换为文本

c 同4的发送html邮件一样发送邮件

所以最重要的过程将是将vm的内容转换为string,即设置邮件内容,其他同上面并无差异

5.1 新建vm文件,命名为index.vm

Java代码 收藏代码
  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <div>${user}</div>
  6. <div>${content}</div>
  7. </body>
  8. </html><spanstyle="white-space:normal;background-color:#ffffff;"></span>

为了方便省略了html头定义。
其中${user} 为Velocity的语法,相当于一个变量,在java程序中可以设置这个变量的值在前端显示。

5.2 创建VelocityEngineFactoryBean对象,并设置属性

Java代码 收藏代码
  1. //Velocity的参数,通过VelocityEngineFactoryBean创建VelocityEngine,也是推荐在配置文件中配置的
  2. Propertiesproperties=System.getProperties();
  3. properties.put("resource.loader","class");
  4. properties.put("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  5. VelocityEngineFactoryBeanv=newVelocityEngineFactoryBean();
  6. v.setVelocityProperties(properties);

5.3 转换vm内容为普通String

Java代码 收藏代码
  1. //声明Map对象,并填入用来填充模板文件的键值对
  2. Map<String,String>model=newHashMap<String,String>();
  3. model.put("user","ooo");
  4. model.put("content","nihao");
  5. //Spring提供的VelocityEngineUtils将模板进行数据填充,并转换成普通的String对象
  6. StringemailText=VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,"index.vm",model);

这样我们便将vm中的变量值填充好,并且将内容转换为了string

5.4 设置邮件内容,同4

Java代码 收藏代码
  1. helper.setText(emailText,true);

其他内容同上面的1、2、3过程。

注意:vm文件格式需要与邮件编码一致否则会出现乱码

参考:

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mail/javamail/JavaMailSenderImpl.html

http://www.cnblogs.com/codeplus/archive/2011/11/03/2232893.html

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mail/javamail/

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/ui/velocity/VelocityEngineUtils.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值