} } }

    应用spring的邮件发送功能

    添加时间:2013-7-16 点击量:

    应用spring供给的MailSender和JavaMailSender类。


    1、邮件对象类



    package cn.luxh.app.mail;
    

    import java.util.List;

    import org.springframework.core.io.AbstractResource;

    public class Email {
    //发件人
    private String ;
    //收件人
    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(
    heymenfolk@163.com);
    email.setTo(
    new String[]{21760658@qq.com});
    email.setSubject(
    简单文本邮件);
    email.setText(
    how are you.i am china!\r你好,法度猿!!);
    mailService.send(email);
    }

    @Test
    public void testSendMimeMail() throws MessagingException {
    Email email
    = new Email();
    email.setFrom(
    heymenfolk@163.com);
    email.setTo(
    new String[]{21760658@qq.com,heymenfolk@outlook.com});
    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);
    }
    }


    文艺不是炫耀,不是花哨空洞的文字堆砌,不是一张又一张的逆光照片,不是将旅行的意义转化为名牌包和明信片的物质展示;很多时候它甚至完全不美——它嘶吼、扭曲,它会痛苦地抽搐,它常常无言地沉默。——艾小柯《文艺是一种信仰》
    分享到: