Spring3整合JavaMail只需要往Spring工程里面加入mail.jar然后配置JavaMailSenderImpl这个Bean,然后通过这个Bean的实例去调用他的send方法就可以发送邮件了,我这里使用的是1.4.5版本。
下面的demo可以额发送普通文本邮件,Html脚本邮件和Mime附件邮件:
<bean id="mail" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- SMTP发送邮件的服务器的IP和端口 -->
<property name="host" value="smtp.163.com"/>
<property name="port" value="25"/>
<!-- 登陆SMTP邮件发送服务器的用户名和密码 -->
<property name="username" value="[email protected]"/>
<property name="password" value="password"/>
<!-- 获得邮件会话属性,验证登录邮件服务器是否成功-->
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
public class MailSenderFactory {
// 获取JavaMailSender bean
public static JavaMailSender getJavaMailSender() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
return (JavaMailSender) context.getBean("mail");
}
}
public interface Send {
public void send();
}
/**
* @author Chou
* @since 2012-09-16
* 文本邮件
* SimpleMailMessage只能用来发送text格式的邮件
*/
public class SendTextMail implements Send{
public void send() {
System.out.println("准备发送文本形式的邮件...");
SimpleMailMessage mail1 = new SimpleMailMessage();
SimpleMailMessage mail2 = new SimpleMailMessage();
mail1.setFrom("[email protected]");// 发送人名片
mail1.setTo("[email protected]");// 收件人邮箱
mail1.setSubject("Spring Mail 测试邮件");// 邮件主题
mail1.setSentDate(new Date());// 邮件发送时间
mail1.setText("Hello,Boy,This is my Spring Mail,哈哈!!");
// 复制邮件
mail1.copyTo(mail2);
mail2.setSubject("这是一封复制的邮件");
// 群发
SimpleMailMessage[] mailMessages = { mail1, mail2 };
MailSenderFactory.getJavaMailSender().send(mailMessages);
System.out.println("文本形式的邮件发送成功!!!");
}
}
/**
* @author Chou
* @since 2012-09-16
* HTML脚本形式邮件MimeMessage需要设置编码
*/
public class SendHtmlMail implements Send{
public void send() {
JavaMailSender mailSender = MailSenderFactory.getJavaMailSender();
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
System.out.println("HTML脚本形式邮件正在发送...");
//设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
//设置发送人名片
helper.setFrom("[email protected]");
//设置收件人名片和地址
helper.setTo(new InternetAddress("\"" + MimeUtility.encodeText("sina邮箱") + "\" <[email protected]>"));// 发送者
// 邮件发送时间
helper.setSentDate(new Date());
//设置回复地址
helper.setReplyTo(new InternetAddress("[email protected]"));
//设置抄送的名片和地址
helper.setCc(InternetAddress.parse(MimeUtility.encodeText("抄送人001") + " <[email protected]>," + MimeUtility.encodeText("抄送人002") + " <[email protected]>"));
//主题
helper.setSubject("챔피언쉽");
// 邮件内容,注意加参数true,表示启用html格式
helper.setText("<html><head></head><body><h1>hello!!我是乔布斯</h1></body></html>",true);
//发送
mailSender.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("HTML脚本形式邮件发送成功!!!");
}
}
/**
* @author Chou
* @since 2012-09-16
* 发送带附件和图片的邮件
* MimeMessage需要设置编码
*/
public class SendFileMail implements Send{
public void send() {
JavaMailSender mailSender = MailSenderFactory.getJavaMailSender();
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
System.out.println("带附件和图片的邮件正在发送...");
//设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
//设置发送人名片
helper.setFrom("[email protected]");
//设置收件人邮箱
helper.setTo(new InternetAddress("\"" + MimeUtility.encodeText("sina邮箱") + "\" <[email protected]>"));
//设置回复地址
helper.setReplyTo(new InternetAddress("[email protected]"));
//设置收件人抄送的名片和地址(相当于群发了)
helper.setCc(InternetAddress.parse(MimeUtility.encodeText("邮箱001") + " <[email protected]>," + MimeUtility.encodeText("邮箱002") + " <[email protected]>"));
// 主题
helper.setSubject("챔피언쉽");
// 邮件内容,注意加参数true,表示启用html格式
helper.setText("<html><head></head><body><h1>hello!!我是乔布斯 챔피언쉽</h1></body></html>",true);
// 加入附件
helper.addAttachment(MimeUtility.encodeText("附件"), new File("E:\\WALL\\Style\\卡通.jpg"));
// 加入插图
helper.addInline(MimeUtility.encodeText("pic01"), new File("E:/WALL/Style/卡通.jpg"));
//发送
mailSender.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("带附件和图片的邮件发送成功!!!");
}
}
其实在实际项目中,用到的功能基本都是群发邮件,helper.setCc()可以设置抄送人地址,相当于你客户端只发送一封邮件,然后邮件服务器会帮你抄送给多人实现群发,当然,如果需求是不用抄送,要每人发一封,那么就要考虑多线程了。