spring mail的使用

[size=large]因为项目的要求,所以做了个邮件发送的功能,但是使用javaMail的话太麻烦了,还需要自己去适配服务器,进行验证,所以就选用了spring的mail功能.因为现在的框架是ssh的,所以就把他做成了模块.下面开始.
首先需要保证要有mail.jar,activation.jar,spring.jar.
然后就是对spring的集成,发代码吧,首先是spring的配置文件,因为我使用了properties文件,所以直接读取文件,配置文件这里要注意下,每条属性后面一定不要跟空格,不然会就会找不到smtp服务器,我就是在这里中招的!
[/size]
#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>
[size=large]下面的是我两个类是实现:MailService类[/size]
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();
}

[size=large]MailServiceImpl类[/size]
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();
}

}

[size=large]发送邮件的action类:MailAction[/size]
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;
}
}

[size=large]这些做完之后基本上就已经成型了!
给出个测试方法[/size]
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);
}


[size=large]这个模块胜在灵活度高,可以在这个基础上安排定时器(例如定时发送邮件),下面也有代码可以下载

有疑问或者错误的请指出.[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值