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  类。里面涉及了多个附件的发送,是用模板发送。注意导入命名空间的架包。

使用Spring整合javaMail发用邮件的更多相关文章

  1. 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

    1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器, ...

  2. Spring整合JavaMail

    1.添加jar包 #此处省略spring基础相关jar包描述,以下是发送邮件相关jar包 <dependency> <groupId>org.springframework&l ...

  3. Selenium 2自动化测试实战38(整合自动发邮件功能)

    整合自动发邮件功能 解决了前面的问题后,现在就可以将自动发邮件功能集成到自动化测试项目中了.下面重新编辑runtest.py文件 #runtest.py #coding:utf-8 from HTML ...

  4. Spring MVC+javamail实现邮件发送

    Spring MVC+javamail实现邮件发送 开启邮箱的POP3/SMTP服务(这里以QQ邮箱举例) 设置 --> 账户 -- > 开启POP3/STMP服务,然后得到一个授权码. ...

  5. 通过spring实现javamail发送邮件功能

    以前很早的时候大家都用javamail实现发送邮件的功能,而且我们也一直沿用至今,代码拷过来用用就行了,现在我们改为用spring来实现,这样一来减少代码的复杂度,也能更好的契合spring理念 首先 ...

  6. Spring的JavaMail实现异步发送邮件

    具体背景就不说了,可以网上搜索相关知识,或者直接看Sping MailSender的官坊网页.这里就直接实战了(Java实现异步发送电子邮件,包含中文无乱码). Maven: <dependen ...

  7. 使用spring的JavaMail发送邮件

    以前我们使用JavaMail发送邮件,步骤挺多的.现在的项目跟Spring整合的比较多.所以这里主要谈谈SpringMail发送. 导入jar包. 配置applicationContext-email ...

  8. 初识quartz 并分析 项目中spring整合quartz的配置【原创+转载】

    初识quartz 并分析 项目中spring整合quartz的配置[原创+转载]2018年01月29日 12:08:07 守望dfdfdf 阅读数:114 标签: quartz 更多个人分类: 工具 ...

  9. Hibernate+Spring整合开发步骤

    Hibernate是一款ORM关系映射框架+Spring是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升. 整合开发步骤如下: 第一步:导入架包: 1.Hibernate ...

随机推荐

  1. struts2笔记4

    1.自定义struts拦截器 应用场景:如果用户登陆后可以访问action中的所有方法,如果用户没有登陆不允许访问action中的方法,并且提示“你没有操作权限” 1)两个页面,一个用户登陆user. ...

  2. Visual Studio 2010(.NET 4.0)中使用SQLite.NET

    Visual Studio 2010(.NET 4.0)中使用SQLite.NET   2011年4月1日 | 分类: DataBase, DOTNET | 标签: .net 4.0, SQLite. ...

  3. 50个常用的JQuery代码

    1. 如何创建嵌套的过滤器 //允许你减少集合中的匹配元素的过滤器, //只剩下那些与给定的选择器匹配的部分.在这种情况下, //查询删除了任何没(:not)有(:has) //包含class为“se ...

  4. s3c2440 移值u-boot-2016.03 第3篇 支持Nor flash 识别

    当选择,NOR flash 启用时,才可以访问 NOR FLASH ./common/board_r.c 364 line:initr_flash()flash_size = flash_init() ...

  5. odd_even_list

    public class Solution { public ListNode OddEvenList(ListNode head) { if(head == null || head.next == ...

  6. POJ 2516:Minimum Cost(最小费用流)

    https://vjudge.net/problem/11079/origin 题意:有N个商店和M个供应商和K种物品,每个商店每种物品有一个需求数,每个供应商每种物品有一个供应量,供应商到商店之间的 ...

  7. LR11启动卡修改

    LR11启动卡修改 C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config <runtime>改为<r ...

  8. 从零开始学习jQuery(转)

    本系列文章导航 从零开始学习jQuery (一) 开天辟地入门篇 从零开始学习jQuery (二) 万能的选择器 从零开始学习jQuery (三) 管理jQuery包装集 从零开始学习jQuery ( ...

  9. (原创)LAMP搭建之二:apache配置文件详解(中英文对照版)

    LAMP搭建之二:apache配置文件详解(中英文对照版) # This is the main Apache server configuration file. It contains the # ...

  10. 如何理解 Java 中的 <T extends Comparable<? super T>>

    Java 中类似 <T extends Comparable<? super T>> 这样的类型参数 (Type Parameter) 在 JDK 中或工具类方法中经常能看到. ...