Fork me on GitHub

Spring发送邮件

没有什么好说的,如下几种方式

文本邮件
HTML邮件
带附件邮件
Freemaker或Velocity模板方式

贴代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
    <!-- 邮件发送者参数-->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.163.com" />
        <property name="username" value="xxx@163.com" />
        <property name="password" value="xxxx/>
        <property name="defaultEncoding" value="UTF-8" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.timeout">25000</prop>
                <prop key="mail.smtp.port">465</prop>
                <prop key="mail.smtp.socketFactory.port">465</prop>
                <prop key="mail.smtp.socketFactory.fallback">false</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            </props>
        </property>
    </bean>
</beans>
@Controller
public class EmailController {
    private static Logger logger = LogManager.getLogger(EmailController.class);
    @Resource
    private JavaMailSender sender;
    @Resource
    private JavaMailSenderImpl senderImpl;

    @RequestMapping("/sendtext.shtml")
    public void sendText() {
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setTo("xxx@qq.com");
        mail.setFrom("xxx@163.com");
        mail.setSubject("spring mail test!");
        mail.setText("springMail的简单发送测试");
        sender.send(mail);
        logger.info("邮件发送成功啦。。。。。。。。。。。");
    }

    @RequestMapping("/sendhtml.shtml")
    public void sendHtml() throws MessagingException {
        StringBuilder builder = new StringBuilder();
        builder.append("<html><head>");
        builder.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
        builder.append("</head><body>");
        builder.append("您好,张三:<br /> <a href='#nolink' style='color: red'>点击此处</a>");
        builder.append("\t系统已为您重置了RUI密码, 账户信息如下:<br />");
        builder.append("用户账户:zhangsan<br />用户密码:123456<br />您可以点击以下链接登录RUI:");
        builder.append("<a href=\"");
        builder.append("\">");
        builder.append("</a>");
        builder.append("</body></html>");
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");
        messageHelper.setTo("xxx@qq.com");
        messageHelper.setFrom("xxx@163.com");
        messageHelper.setSubject("spring mail Html Test");
        messageHelper.setText(builder.toString(), true);
        sender.send(mailMessage);
    }

    @RequestMapping("/sendfile.shtml")
    public void sendFile() throws UnsupportedEncodingException, MessagingException {
        MimeMessage mimeMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setTo("xxx@qq.com");
        messageHelper.setFrom("xxx@163.com");
        messageHelper.setSubject("spring mail File Test");
        messageHelper.setText("<html><head></head><body><h1>hello!!Irving </h1></body></html>", true);
        
        messageHelper.addInline("Iphone", new File("H:/苹果.jpg"));
        
        FileSystemResource filePhoto = new FileSystemResource(new File("H:/妹子.jpg")); 
        messageHelper.addAttachment(filePhoto.getFilename(), filePhoto);

        File docFile = new File("F:/杂合集书籍/129道经典.NET笔试题.doc");
        messageHelper.addAttachment(MimeUtility.encodeWord(docFile.getName()), docFile);
        
        File pdfFile = new File("F:/杂合集书籍/GridView+72般绝技.pdf");
        messageHelper.addAttachment(MimeUtility.encodeWord(pdfFile.getName()), pdfFile);
        
        File rarFile = new File("F:/Visual C#.NET网络编程.rar");
        messageHelper.addAttachment(MimeUtility.encodeWord(rarFile.getName()), rarFile);
        sender.send(mimeMessage);
    }
}

结果

image

模板的方式还没有用到,有时间再参考文档

Refer:

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mail.html

posted @ 2013-03-05 19:23  花儿笑弯了腰  阅读(395)  评论(0编辑  收藏  举报