1.导入javamail.jar        自行百度下载

2.使用模板发送邮件架包 freemarker.jar

3.Spring配置文件  以及架包这里就不需要说了吧,如果不明白的发我Email :xkjava@sina.com

 

 

//邮件信息设置类 main.java

package cn.jbit.email;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Mail {
    private JavaMailSender mailSender;
    private Configuration configuration;

    public Configuration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }


    public JavaMailSender getMailSender() {
        return mailSender;
    }

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    //模板設置
    private String getMailText() {

        String htmtext = "";
        // 发信内容
        String mailcontent = "你好,欢迎使用尊云服务器,在这里,你的数据更加安全、性能更加稳定!";
        try {
            // 获取实例模板
            Template template = configuration.getTemplate("Template02.ftl");

            // 通过map传递动态数据
            Map map = new HashMap();
            map.put("name", "简单");
            map.put("content", mailcontent);
            htmtext = FreeMarkerTemplateUtils.processTemplateIntoString(
                    template, map);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return htmtext;

    }

    //发送邮件
    public void send() throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true,"UTF-8");
        try {
            helper.setFrom("xkjava@163.com");
            helper.setTo("xkjava@sina.com");
            helper.setSubject("歡迎來到員工社區");
            helper.setText(getMailText(),true);
            
        //多个附件发送,先放进集合,注意这里的文件路径 List
<String> strFilePath = new ArrayList<String>();
                //文件路径 strFilePath.add(
"cn/jbit/template/1.doc"); strFilePath.add("cn/jbit/template/he_header.png"); for (String strfile : strFilePath) { //带附件发送邮件 ClassPathResource file = new ClassPathResource(strfile); helper.addAttachment(file.getFilename(), file.getFile()); } //发送 mailSender.send(message); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); /* simpleMailMessage.setFrom("kzhan8082@163.com"); simpleMailMessage.setTo("xkjava@sina.com"); simpleMailMessage.setText("HelloJavaMail!!!!"); simpleMailMessage.setSubject("Hello");*/ //mailSender.send(simpleMailMessage); // 发送邮件 } }

 

 

 

//Spring  applicationContext.xml 配置文件

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <!--通过模板发送邮件-->
    <bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
        <!-- 模板路徑 -->    <!--模板路径,这里根据自己的模板路径来,模板的后缀名为 .ftl-->
        <property name="templateLoaderPath" value="cn/jbit/template"></property>
        
        <!-- 设置FreeMarke环境变量 -->
        <property name="freemarkerSettings">
            <props>
                  <!--这里暂时不配,防止中文乱码-->
            </props>
        </property>
    </bean>



    <!-- 设置邮件信息 这里就是用163的邮箱做demo,方便-->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.163.com"></property>
        <property name="port" value="25"></property>
        <property name="username" value="邮箱账户"></property>
        <property name="password" value="邮箱密码"></property>
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="protocol" value="smtp"></property>
        <property name="javaMailProperties">
            <props>
                <!-- 设置smtp服务器需要用户验证 -->
                <prop key="mail.smtp.auth">true</prop>
            </props>
        
        </property>
    </bean>
    
    <!-- IOC注入 -->
    <bean id="mailsend" class="cn.jbit.email.Mail">
        <property name="mailSender" ref="mailSender"></property>
        <property name="configuration" ref="freeMarkerConfiguration"></property>
    </bean>

    
</beans>

 

 

//测试类MailTest.java

package cn.jbit.email;

import javax.mail.MessagingException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MialTest {

    /**
     * @param args
     * @throws MessagingException 
     */
    public static void main(String[] args) throws MessagingException {
        // TODO Auto-generated method stub
        /*Mail mail = new Mail();
        mail.send();*/    
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        Mail mail02 = (Mail)applicationContext.getBean("mailsend");
        mail02.send();
        System.out.println("success");
    }

}

 

 

 

这里的功能比较多,注意看设置邮件信息的 mail.java  类。里面涉及了多个附件的发送,是用模板发送。注意导入命名空间的架包。

 

 

 

 posted on 2014-07-01 11:43  蓝博文  阅读(358)  评论(0编辑  收藏  举报