为了使用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配置
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 |
xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:p=
"http://www.springframework.org/schema/p"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
>
|
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框架来发送邮件的小程序"
);
sender
.
send
(
mailMessage
);
}
}
|
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
.
setSubject
(
"测试邮件中嵌套图片!!"
);
// true 表示启动HTML格式的邮件
messageHelper
.
setText
(
"
hello!!spring image html mail"
FileSystemResource
img
=
new
FileSystemResource
(
new
File
(
"单.png"
));
messageHelper
.
addInline
(
"image"
,
img
);
//跟cid一致
sender
.
send
(
mailMessage
);
System
.
out
.
println
(
"邮件发送成功..."
);
}
}
|
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
.
setSubject
(
"测试邮件中嵌套图片!!"
);
// true 表示启动HTML格式的邮件
messageHelper
.
setText
(
"
hello!!spring image html mail"
FileSystemResource
img
=
new
FileSystemResource
(
new
File
(
"单.png"
));
messageHelper
.
addAttachment
(
"单.png"
,
file
);
//添加到附件
sender
.
send
(
mailMessage
);
System
.
out
.
println
(
"邮件发送成功..."
);
}
}
|
6、如果使用的是QQ邮箱的话建议把这几项都选上,不然可能调试不能通过。
附:所需的jar:http://download.csdn.net/detail/eternity0_0/7179617