spring 封装javamail实现邮件的发送
spring 封装了javamail发送邮件功能
实现步骤
1,导入activation.jar mail.jar 及spring 相应包
2,写入一下代码
package com.cpd.crm.common;
importjava.io.File;
importjava.util.Properties;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeUtility;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.springframework.mail.SimpleMailMessage;
importorg.springframework.mail.javamail.JavaMailSender;
importorg.springframework.mail.javamail.JavaMailSenderImpl;
importorg.springframework.mail.javamail.MimeMessageHelper;
/**
*spring邮件发送
*1、发送文字邮件
*2、发送HTML内容邮件
*3、发送邮件可添加附件
*@author张祺麟
*
*/
publicclassSendMail{
publicApplicationContextctx=null;
publicSendMail(){
//获取上下文
ctx=newClassPathXmlApplicationContext("application-mail.xml");
}
publicvoidsend(){
//获取JavaMailSenderbean
JavaMailSendersender=(JavaMailSender)ctx.getBean("mailSender");
SimpleMailMessagemail=newSimpleMailMessage();//<span
try{
mail.setTo("[email protected]");//接受者
//mail.setTo("[email protected]");//接受者
//mail.setTo("[email protected]");//接受者
//mail.setFrom("[email protected]");
mail.setFrom("[email protected]");//发送者,这里还可以另起Email别名,不用和xml里的username一致
mail.setSubject("springmailtest!");//主题
mail.setText("springMail的简单发送测试");//邮件内容
sender.send(mail);
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
SendMailemail=newSendMail();
email.send();
}
publicvoidsendHTML(){
//获取JavaMailSenderbean
JavaMailSendersender=(JavaMailSender)ctx.getBean("mailSender");
JavaMailSenderImplsenderImpl=newJavaMailSenderImpl();
MimeMessagemailMessage=senderImpl.createMimeMessage();
//设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelpermessageHelper;
try{
messageHelper=newMimeMessageHelper(mailMessage,true,"utf-8");
messageHelper.setTo("");//接受者email.getEmail()
messageHelper.setFrom("[email protected]");//发送者
messageHelper.setSubject("测试邮件");//主题
//邮件内容,注意加参数true,表示启用html格式
messageHelper
.setText(
"<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",
true);
sender.send(mailMessage);
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidsendHtmlAndAnnex(){
//获取JavaMailSenderbean
JavaMailSendersender=(JavaMailSender)ctx.getBean("mailSender");
JavaMailSenderImplsenderImpl=newJavaMailSenderImpl();
MimeMessagemailMessage=senderImpl.createMimeMessage();
//设置utf-8或GBK编码,否则邮件会有乱码
try{
MimeMessageHelpermessageHelper=newMimeMessageHelper(
mailMessage,true,"utf-8");
messageHelper.setTo("");//接受者email.getEmail()
messageHelper.setFrom("[email protected]");//发送者
messageHelper.setSubject("测试邮件");//主题
//邮件内容,注意加参数true
messageHelper
.setText(
"<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",
true);
//附件内容
messageHelper.addInline("a",newFile("E:/xiezi.jpg"));
messageHelper.addInline("b",newFile("E:/logo.png"));
Filefile=newFile("E:/测试中文文件.rar");
//这里的方法调用和插入图片是不同的,使用MimeUtility.encodeWord()来解决附件名称的中文问题
messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()),
file);
sender.send(mailMessage);
}catch(Exceptione){
e.printStackTrace();
}
}
//publicstaticvoidmain(Stringargs[]){
//JavaMailSenderImplsenderImpl=newJavaMailSenderImpl();
////设定mailserver
//senderImpl.setHost("smtp.163.com");
////建立邮件消息
//SimpleMailMessagemailMessage=newSimpleMailMessage();
////设置收件人,寄件人用数组发送多个邮件
////String[]array=newString[]{"[email protected]","[email protected]"};
////mailMessage.setTo(array);
//mailMessage.setTo("[email protected]");
//mailMessage.setFrom("[email protected]");
//mailMessage.setSubject("测试简单文本邮件发送!");
//mailMessage.setText("测试我的简单邮件发送机制!!");
//senderImpl.setUsername("userName");//根据自己的情况,设置username
//senderImpl.setPassword("password");//根据自己的情况,设置password
//
// Properties prop= new Properties() ;
//prop.put("mail.smtp.auth","true");//将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
//prop.put("mail.smtp.timeout","25000");
//senderImpl.setJavaMailProperties(prop);
////发送邮件
//senderImpl.send(mailMessage);
//System.out.println("邮件发送成功..");
//}
}<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--注意:这里的参数(如用户名、密码)都是针对邮件发送者的-->
<!--配置邮件发送-->
<beanid="mailSender"class="org.springframework.mail.javamail.JavaMailSenderImpl">
<propertyname="host">
<!--<value>smtp.qq.com</value>-->
<value>smtp.sina.com</value>
</property>
<propertyname="javaMailProperties">
<props>
<propkey="mail.smtp.auth">true</prop>
<propkey="mail.smtp.timeout">25000</prop>
</props>
</property>
<propertyname="username">
<value>[email protected]</value>
<!--<value>[email protected]</value>-->
</property>
<propertyname="password">
<value>1314521zq~</value>
<!--<value>zhangqi1314521~</value>-->
</property>
</bean>
</beans>这中间可能会报错,我简单的记录要点,先注意邮箱地址是否正确,然后如果是使用新浪邮箱发送的,可能需要在自己的邮箱设置里,将一些STMP HOST的服务打开。