首先需要保证要有mail.jar,activation.jar,spring.jar.
然后就是对spring的集成,发代码吧,首先是spring的配置文件,因为我使用了properties文件,所以直接读取文件,配置文件这里要注意下,每条属性后面一定不要跟空格,不然会就会找不到smtp服务器,我就是在这里中招的!
#user is the sender name; #password is the sender password; #auth is allow grant for the mailServices. default value is true #smtp.com is the mailService's address mail.user=*** mail.password=**** mail.auth=true smtp.com=smtp.163.com mail.timeout=25000
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value> classpath*:mailSend.properties </value> </list> </property> </bean> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host"> <value>${smtp.com}</value> </property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">${mail.auth}</prop> <prop key="mail.smtp.timeout">${mail.timeout}</prop> <prop key="mail.smtp.from">${mail.user}</prop> </props> </property> <property name="username"> <value>${mail.user}</value> </property> <property name="password"> <value>${mail.password}</value> </property> </bean> <bean id="mailService" class="com.magus.mail.service.impl.MailServiceImpl"> <property name="mailSend"> <ref local="mailSender" /> </property> </bean> <bean id="mailAction" class="com.magus.mail.action.MailAction"> <property name="mailService"> <ref local="mailService" /> </property> </bean> </beans>下面的是我两个类是实现:MailService类
import javax.mail.internet.MimeMessage; import org.springframework.mail.SimpleMailMessage; public interface MailService { public void sendMail(SimpleMailMessage smm); public void sendMail(MimeMessage mm); public MimeMessage getMimeMessage(); }
MailServiceImpl类
import javax.mail.internet.MimeMessage; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import com.magus.mail.service.MailService; public class MailServiceImpl implements MailService { private JavaMailSender mailSend; public JavaMailSender getMailSend() { return mailSend; } public void setMailSend(JavaMailSender mailSend) { this.mailSend = mailSend; } @Override public void sendMail(SimpleMailMessage smm) { mailSend.send(smm); } @Override public void sendMail(MimeMessage mm) { mailSend.send(mm); } @Override public MimeMessage getMimeMessage() { return mailSend.createMimeMessage(); } }
发送邮件的action类:MailAction
import java.io.File; import java.io.UnsupportedEncodingException; import java.util.List; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.MimeMessageHelper; import com.magus.mail.service.MailService; public class MailAction { private MailService mailService; /** * 普通邮件发送,sendTo收件者地址,sendSubject邮件主题,sendContent邮件内容<br/> * 注:发送者需要在mailSend.properties文件里面配置<br/> * @param sendTo * @param sendSubject * @param sendContent * @return */ public void sendMail(String sendTo,String sendSubject,String sendContent){ SimpleMailMessage smm=new SimpleMailMessage(); smm.setTo(sendTo); smm.setSubject(sendSubject); smm.setText(sendContent); mailService.sendMail(smm); } /** * 带HTML格式的邮件发送,sendTo收件者地址,sendSubject邮件主题<br/> * sendContent邮件内容,flag为是否发送HTML格式邮件:true是,false不是<br/> * 注:发送者需要在mailSend.properties文件里面配置 <br/> * @param sendTo * @param sendSubject * @param sendContent * @param flag * @return */ public void sendMailHtml(String sendTo,String sendSubject,String sendContent,boolean flag){ MimeMessage mimiMail=mailService.getMimeMessage(); try { MimeMessageHelper mHelp=new MimeMessageHelper(mimiMail,true,"utf-8"); mHelp.setTo(sendTo); mHelp.setSubject(sendSubject); mHelp.setText(sendContent,flag); mailService.sendMail(mimiMail); } catch (MessagingException e) { e.printStackTrace(); } } /** * 带HTML格式的邮件发送,sendTo收件者地址,sendSubject邮件主题<br/> * sendContent邮件内容,flag为是否发送HTML格式邮件:true是,false不是<br/> * List<File>表示需要发送的附件<br/> * 注:发送者需要在mailSend.properties文件里面配置 <br/> * @param sendTo * @param sendSubject * @param sendContent * @param flag * @param l * @return */ public void sendMailHtmlByAttach(String sendTo,String sendSubject,String sendContent,boolean flag,List<File> f){ MimeMessage mimiMail=mailService.getMimeMessage(); try { MimeMessageHelper mHelp=new MimeMessageHelper(mimiMail,true,"utf-8"); mHelp.setTo(sendTo); mHelp.setSubject(sendSubject); mHelp.setText(sendContent,flag); for(int i=0;i<f.size();i++){ File file=f.get(i); String filename=file.getName(); mHelp.addAttachment(MimeUtility.encodeWord(filename), file); } mailService.sendMail(mimiMail); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public MailService getMailService() { return mailService; } public void setMailService(MailService mailService) { this.mailService = mailService; } }
这些做完之后基本上就已经成型了!
给出个测试方法
public static void main(String[] args) { args=new String[]{"/mail-service.xml"}; BeanFactory factory=new ClassPathXmlApplicationContext(args); MailAction wiq=(MailAction) factory.getBean("mailAction"); File f=new File("H:\\path.txt"); List<File> l=new ArrayList<File>(); l.add(f); wiq.sendMailHtmlByAttach("*****@qq.com", "test", "this is a test mail",false,l); }
这个模块胜在灵活度高,可以在这个基础上安排定时器(例如定时发送邮件),下面也有代码可以下载
有疑问或者错误的请指出.