博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Java 发送各种邮件
阅读量:4038 次
发布时间:2019-05-24

本文共 22277 字,大约阅读时间需要 74 分钟。

Spring邮件抽象层的主要包为
org.springframework.mail
。它包括了发送电子邮件的主要接口
MailSender
,和
值对象
SimpleMailMessage
,它封装了简单邮件的属性如
from
to
,
cc
subject
,
text
。 包里还包含一棵以
MailException
为根的checked Exception继承树,它们提供了对底层邮件系统异常的高级别抽象。 要获得关于邮件异常层次的更丰富的信息,请参考Javadocs。


为了使用JavaMail中的一些特色, 比如MIME类型的信件, Spring提供了MailSender的一个子接口, 即org.springframework.mail.javamail.JavaMailSender。Spring还提供了一个回调接口org.springframework.mail.javamail.MimeMessagePreparator, 用于准备JavaMail的MIME信件。

这里简单的介绍了如何使用spring发送各种形式的邮件以及配置。

1、在src目录下建立mail.properties文件里边包含一下内容

mail.host=smtp.exmail.qq.com(建议使用这个,如果使用smtp.qq.com可能会出现安全认证的问题

mail.username=你的邮箱名
mail.password=你的邮箱密码
mail.from=发送方的邮箱

2、使用spring配置

Spring_mail_applicationContext.xml
3、发送简单的邮件

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
package             
mail
;
import
javax.mail.MessagingException
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
import
org.springframework.mail.MailSender
;
import
org.springframework.mail.SimpleMailMessage
;
/**
* 本类测试邮件发送Html形式
*
* @author Eternity_._
*
*/
public
class
SingleMailSend
{
static
ApplicationContext
actx
=
new
ClassPathXmlApplicationContext
(
"applicationContext.xml"
);
static
MailSender
sender
=
(
MailSender
)
actx
.
getBean
(
"mailSender"
);
static
SimpleMailMessage
mailMessage
=
(
SimpleMailMessage
)
actx
.
getBean
(
"mailMessage"
);
public
static
void
main
(
String
args
[])
throws
MessagingException
{
mailMessage
.
setSubject
(
"你好"
);
mailMessage
.
setText
(
"这个是一个通过Spring框架来发送邮件的小程序"
);
mailMessage
.
setTo
(
"9197****1@qq.com"
);
sender
.
send
(
mailMessage
);
}
}
Spring_mail_SingleMail.java
4、发送带有图片的邮件,以嵌入HTML的方式

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
package             
springmail
;
import
java.io.File
;
import
javax.mail.MessagingException
;
import
javax.mail.internet.MimeMessage
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
import
org.springframework.core.io.FileSystemResource
;
import
org.springframework.mail.javamail.JavaMailSenderImpl
;
import
org.springframework.mail.javamail.MimeMessageHelper
;
public
class
SpringAttachedImageMail
{
public
static
void
main
(
String
[]
args
)
throws
MessagingException
{
ApplicationContext
ctx
=
new
ClassPathXmlApplicationContext
(
"applicationContext.xml"
);
JavaMailSenderImpl
sender
=
(
JavaMailSenderImpl
)
ctx
.
getBean
(
"mailSender"
);
MimeMessage
mailMessage
=
sender
.
createMimeMessage
();
MimeMessageHelper
messageHelper
=
new
MimeMessageHelper
(
mailMessage
,
true
);
messageHelper
.
setFrom
(
"9197****1@qq.com"
);
messageHelper
.
setTo
(
"9197****1@qq.com"
);
messageHelper
.
setSubject
(
"测试邮件中嵌套图片!!"
);
// true 表示启动HTML格式的邮件
messageHelper
.
setText
(
"

hello!!spring image html mail

"
+
"baidu"
+
""
,
true
);
FileSystemResource
img
=
new
FileSystemResource
(
new
File
(
"单.png"
));
messageHelper
.
addInline
(
"image"
,
img
);
//跟cid一致
sender
.
send
(
mailMessage
);
System
.
out
.
println
(
"邮件发送成功..."
);
}
}
Spring_mail_AttachedFileMail.java
5、发送带有附件的邮件

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
package             
springmail
;
import
java.io.File
;
import
javax.mail.MessagingException
;
import
javax.mail.internet.MimeMessage
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
import
org.springframework.core.io.FileSystemResource
;
import
org.springframework.mail.javamail.JavaMailSenderImpl
;
import
org.springframework.mail.javamail.MimeMessageHelper
;
public
class
SpringAttachedImageMail
{
public
static
void
main
(
String
[]
args
)
throws
MessagingException
{
ApplicationContext
ctx
=
new
ClassPathXmlApplicationContext
(
"applicationContext.xml"
);
JavaMailSenderImpl
sender
=
(
JavaMailSenderImpl
)
ctx
.
getBean
(
"mailSender"
);
MimeMessage
mailMessage
=
sender
.
createMimeMessage
();
MimeMessageHelper
messageHelper
=
new
MimeMessageHelper
(
mailMessage
,
true
);
messageHelper
.
setFrom
(
"9197****1@qq.com"
);
messageHelper
.
setTo
(
"9197****1@qq.com"
);
messageHelper
.
setSubject
(
"测试邮件中嵌套图片!!"
);
// true 表示启动HTML格式的邮件
messageHelper
.
setText
(
"

hello!!spring image html mail

"
+
"baidu"
+
""
,
true
);
FileSystemResource
img
=
new
FileSystemResource
(
new
File
(
"单.png"
));
messageHelper
.
addAttachment
(
"单.png"
,
file
);
//添加到附件
sender
.
send
(
mailMessage
);
System
.
out
.
println
(
"邮件发送成功..."
);
}
}
你可能感兴趣的文章
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
一个ahk小函数, 实现版本号的比较
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>