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配置
|
来自CODE的代码片
Spring_mail_applicationContext.xml
3、发送简单的邮件
|
来自CODE的代码片
Spring_mail_SingleMail.java
4、发送带有图片的邮件,以嵌入HTML的方式
|
来自CODE的代码片
Spring_mail_AttachedFileMail.java
5、发送带有附件的邮件
|
来自CODE的代码片
Spring_mail_AttachedFileMail.java
6、如果使用的是QQ邮箱的话建议把这几项都选上,不然可能调试不能通过。
附:所需的jar:http://download.csdn.net/detail/eternity0_0/7179617