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. animation 的属性一共有 6 个值,详细介绍在此

    animation 属性是一个简写属性,用于设置六个动画属性: animation-name animation-duration animation-timing-function animatio ...

  2. 一面cvte

    昨天上午去cvte参加一面,和好基友一块,离学校很近,10点多一点到了,一出电梯,傻眼了(不是美女很多),是人真的很多,等了2个小时才轮到我们,一个hr面3个人,我和基友,还有一个本科小盆友,问了5个 ...

  3. python :HTML+CSS (Position)

    position_fixed固定在某一个页面上 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  4. (转) Eclipse连接MySQL数据库(傻瓜篇)

    Eclipse连接MySQL数据库(傻瓜篇) 原帖地址: http://www.cnblogs.com/fnng/archive/2011/07/18/2110023.html Posted on 2 ...

  5. Viking Village维京村落demo中的地面积水效果

    效果如下: 似乎是通过高光贴图实现的,查找后发现具体在这: 它使用了基于Standard的TerrainSurface自定义Shader,关闭该帖图后效果消失: 这个TerrainSurfaceSha ...

  6. flex垂直居中

    最近遇到一个令我绞尽脑汁的布局 T.T.T.T,分享下.重点--垂直居中. 布局说明:1. 场次为一场比赛 2. 比赛双方是交战的两个队伍 3. 一场比赛可以有多种玩法,所以场的每个玩法的布局的高度都 ...

  7. android小知识之fragment中调用startActivityForResult(Intent intent,int requestcode)所遇到的问题

    大家都知道对于Activity和Fragment都可以注册OnActivityResult()方法,但是要注意几点: a.当activity和fragment都注册了OnActivityResult( ...

  8. Aggregation 聚集

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. Adobe Edge Animate CC 不再开发更新!

    Adobe Edge Animate CC停止开发更新! http://blogs.adobe.com/edge/2015/11/30/update-about-edge-tools-services ...

  10. 《K&R》中引用的几个排序算法(shellsort、)以及一个自己乱写的排序

    留待期末考后更新... void shellsort(int v[], int n) { int gap, i, j, temp; ; gap > ; gap /= ) for(i = gap; ...