使用spring的邮件发送功能

使用spring提供的MailSender和JavaMailSender类。

1、邮件对象类

package cn.luxh.app.mail;

import java.util.List;

import org.springframework.core.io.AbstractResource;

public class Email {
    //发件人
    private String from;
    //收件人
    private String[] to;
    //主题
    private String subject;
    //邮件内容
    private String text;
    //附件
    private List<AbstractResource> resources;
    
    //geter seter
        //...
    
}
    

2、邮件发送服务类

package cn.luxh.app.mail;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.AbstractResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailService {
    
    //简单的文本邮件发送类
    private MailSender mailSender;
    //复杂邮件发送类
    private JavaMailSender javaMailSender;
    
    /**
     * 发送简单的文本邮件
     * @param email
     */
    public void send(Email email){
        SimpleMailMessage smm = new SimpleMailMessage();
        smm.setFrom(email.getFrom());
        smm.setSubject(email.getSubject());
        smm.setTo(email.getTo());
        smm.setText(email.getText());
        mailSender.send(smm);
        
    }
    
    /**
     * 发送复杂邮件
     * @param email
     * @throws MessagingException
     */
    public void sendMime(Email email) throws MessagingException{
        MimeMessage mm = javaMailSender.createMimeMessage();
        //加上编码,解决中文乱码
        MimeMessageHelper helper = new MimeMessageHelper(mm,true,"GB2312");
        
        helper.setFrom(email.getFrom());
        helper.setTo(email.getTo());
        helper.setSubject(email.getSubject());
        helper.setText(email.getText(),true);
        
        //添加附件
        if(email.getResources()!=null && email.getResources().size()>0) {
            for(AbstractResource resource:email.getResources()) {
                helper.addAttachment(resource.getFilename(), resource);
            }
        }
        
        javaMailSender.send(mm);
        
    }
    
    public MailSender getMailSender() {
        return mailSender;
    }

    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }
    
    public JavaMailSender getJavaMailSender() {
        return javaMailSender;
    }

    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }
}

3、邮件属性配置文件mail.properties

#smtp服务器
mail.host=smtp.163.com

#用户名
mail.username=heymenfolk@163.com

#密码
mail.password=your password

4、spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/context      
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!-- 启用注解支持 -->
    <context:annotation-config />
    
    <!-- 加载属性文件 -->
    <context:property-placeholder location="classpath:mail.properties" />
    
    <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.host}"/>
        <property name="username" value="${mail.username}"/>
        <property name="password" value="${mail.password}"/>
    </bean>
    
    <bean id="mailService" class="cn.luxh.app.mail.MailService">
        <property name="mailSender" ref="javaMailSender"/>
        <property name="javaMailSender" ref="javaMailSender"/>
    </bean>    
</beans>

5、测试

package cn.luxh.app.test;

import java.util.ArrayList;
import java.util.List;

import javax.mail.MessagingException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.luxh.app.mail.Email;
import cn.luxh.app.mail.MailService;

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations={"classpath:app-mail.xml"})
public class MailTester {
    
    @Autowired
    private MailService mailService;
    @Test
    public void testSendMail() {
        Email email = new Email();
        email.setFrom("[email protected]");
        email.setTo(new String[]{"[email protected]"});
        email.setSubject("简单文本邮件");
        email.setText("how are you.i am from china!\r你好,程序猿!!");
        mailService.send(email);
    }
    
    @Test
    public void testSendMimeMail() throws MessagingException {
        Email email = new Email();
        email.setFrom("[email protected]");
        email.setTo(new String[]{"[email protected]","[email protected]"});
        email.setSubject("复杂邮件");
        String text = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='http://luxh.cnblogs.com'>我的博客</a></h1></body></html>";
        email.setText(text);
        
        
        List<AbstractResource> resources = new ArrayList<AbstractResource>();
        //添加附件
        ClassPathResource file1 = new ClassPathResource("top1.jpg");
        FileSystemResource file2 = new FileSystemResource("d:/中文.txt");
        resources.add(file1);
        resources.add(file2);
        email.setResources(resources);
        
        mailService.sendMime(email);
    }
}

 

你可能感兴趣的:(spring)