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. javascript的语法结构

    字符规范: javascript程序是采用的Unicode字符集编写的,并且区分大小写.但是html代码不区分大小写,比如,在html中点击事件就可以写成onClick或则onclick,但是在jav ...

  2. 思维导图软件MindManager for Windows中如何修改思维导图布局

    MindManager for Windows是 Mindjet公司旗下应用于Windows桌面系统的一款思维导图软件,目前已经更新到了v14版本.对于很多刚开始使用MindManager for W ...

  3. python实现动态更新远程机器列表的SSH登录脚本

    在公司里, 常常要远程到很多机器上执行命令.机器列表会逐渐增多, 记忆这么多机器的IP或域名显然不是人脑所擅长的.因此, 需要保持一份SSH机器列表,从这些机器列表生成一个用于SSH到机器列表中机器的 ...

  4. Dynamics AX 2012 R2 AIF自定义服务中的事务回滚Bug

    Reinhard在一个Customer Service里的一个Method中,发现一个Transcation RollBack Bug.先看该Method的代码: [SysEntryPointAttr ...

  5. WordPress 添加Meta Box的方法步骤

    需要使用到add meta boxes Action,该Action允许我们为任何文章类型注册Meta Box,在该Action中,我们需要使用add_meta_box()方法来添加Meta Box的 ...

  6. Linux之常用快捷键

    tab:自动补齐命令或者路径 ESC+u:将字符小写变大写 ctrl+s:在终端中冻结stdin ctrl+q:在终端中恢复stdin ctrl+a:光标移动到行首 ctrl+e:光标移动到行尾 ct ...

  7. 【转】 MySQL与PostgreSQL:该选择哪个开源数据库?哪一个更好?

    转载地址:http://www.infoq.com/cn/news/2013/12/mysql-vs-postgresql 如果打算为项目选择一款免费.开源的数据库,那么你可能会在MySQL与Post ...

  8. 关于一个新的DOM选择器querySelector

    在传统的javascript中,提到DOM选择器,大家比较熟悉的方式是通过tag,name,id来获取,其实大家都发现如果获取比较复杂的话,用这个方法会很繁琐,这时大家应该都会想到jquery里获取一 ...

  9. 相似性度量(Similarity Measurement)与“距离”(Distance)

    在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间的“距离”(Distance).采用什么样的方法计算距离是很讲究,甚至关 ...

  10. Android--菜单详解

    Android中的菜单分为三种,即选项菜单(系统菜单),上下文菜单和弹出式菜单. 选项菜单: 一个activity只有一个选项菜单,选项菜单的创建方式有低版本创建和高版本创建两种.最常用的是干版本创建 ...