`
weitao1026
  • 浏览: 994358 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring发送邮件配置文件

阅读更多
    1、发送邮件配置文件springmail_config.xml 
    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="<a href="http://www.springframework.org/schema/beans" style="font-size: 14px;">http://www.springframework.org/schema/beans</a>" 
           xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" style="font-size: 14px;">http://www.w3.org/2001/XMLSchema-instance</a>" 
           xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans" style="font-size: 14px;">http://www.springframework.org/schema/beans</a> 
           <a href="http://www.springframework.org/schema/beans/spring-beans.xsd" style="font-size: 14px;">http://www.springframework.org/schema/beans/spring-beans.xsd</a>" 
    > 
       <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
            <property name="host" value="smtp.qq.com" /> 
            <property name="port" value="25" /> 
            <property name="username" value="xxxx@qq.com" /> 
            <property name="password" value="xxxxxxx" /> 
            <property name="javaMailProperties"> 
              <props> 
               <prop key="mail.smtp.auth">true</prop> 
                <!-- 根据情况可进行设置  
                <prop key="mail.smtp.timeout">2500</prop> 
               --> 
                         </props>  
                   </property>  
         </bean>  
    </beans> 

Java代码  收藏代码

    2、发送邮件java类 
    package com.yihongyu.exec.javamail; 
     
    import java.io.File; 
     
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeMessage; 
    import javax.mail.internet.MimeUtility; 
     
    import org.springframework.context.ApplicationContext; 
    import org.springframework.context.support.ClassPathXmlApplicationContext; 
    import org.springframework.core.io.FileSystemResource; 
    import org.springframework.mail.SimpleMailMessage; 
    import org.springframework.mail.javamail.JavaMailSender; 
    import org.springframework.mail.javamail.MimeMessageHelper; 
     
    /**
     * SpringMail测试类
     * 
     * @author tzz
     * 
     */ 
    public class SpringMailUtil { 
        private ApplicationContext context = null; 
     
        public SpringMailUtil() { 
            context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/springmail_config.xml"); 
        } 
     
        // 简单邮件 
        public void simpleSend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
            SimpleMailMessage mail = new SimpleMailMessage(); 
            mail.setFrom("xxx@qq.com"); 
            mail.setTo("xxx@qq.com"); 
            mail.setSubject(" 测试spring Mail"); 
            mail.setText("你好,java"); 
            mailSender.send(mail); 
        } 
     
        // 带附件 
        public void attachmentSend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
     
            MimeMessage mime = mailSender.createMimeMessage(); 
            MimeMessageHelper helper; 
            try { 
     
                helper = new MimeMessageHelper(mime, true, "utf-8"); 
                helper.setFrom("xxx@qq.com"); 
                helper.setTo("xxx@qq.com"); 
                helper.setSubject("测试spring Mail附件"); 
                // 需要将附件显示在html中 
                helper.setText("你好,java", true); 
                FileSystemResource attachment = new FileSystemResource(new File("E:\\Test2.doc")); 
                helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment);// 解决附件中文编码问题 
     
                mailSender.send(mime); 
     
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
     
        } 
     
        // 多附件 
        public void moreAttachmentSend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
     
            MimeMessage mime = mailSender.createMimeMessage(); 
            MimeMessageHelper helper; 
            try { 
     
                helper = new MimeMessageHelper(mime, true, "utf-8"); 
                helper.setFrom("xxxx@qq.com"); 
                helper.setTo("xxx@qq.com"); 
                helper.setSubject("测试spring Mail附件"); 
                // 需要将附件显示在html中 
                helper.setText("你好,java", true); 
                FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql")); 
                helper.addAttachment("zqt.sql", attachment); 
                FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc")); 
                helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题 
     
                mailSender.send(mime); 
     
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
     
        } 
     
        // 抄送 
        public void copySend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
     
            MimeMessage mime = mailSender.createMimeMessage(); 
            MimeMessageHelper helper; 
            try { 
     
                helper = new MimeMessageHelper(mime, true, "utf-8"); 
                helper.setFrom("xxxxx@qq.com"); 
                helper.setTo("xxxx@qq.com"); 
                helper.setCc("xxxx@qq.com"); 
                helper.setSubject("测试spring Mail附件"); 
                // 需要将附件显示在html中 
                helper.setText("你好,java", true); 
                FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql")); 
                helper.addAttachment("zqt.sql", attachment); 
                FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc")); 
                helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题 
     
                mailSender.send(mime); 
     
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
     
        } 
     
        // 多附件、多人发送/抄送 
        public void moreUserSend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
     
            MimeMessage mime = mailSender.createMimeMessage(); 
            MimeMessageHelper helper; 
            try { 
     
                helper = new MimeMessageHelper(mime, true, "utf-8"); 
                helper.setFrom("xxxxx@qq.com"); 
                helper.setTo("xxxxx@qq.com");// 发送 
                // helper.setCc("xxxxxx@qq.com");//抄送 
                // helper.setTo(new InternetAddress[] { new InternetAddress("xxxxx@qq.com"), 
                // new InternetAddress("xxxx@qq.com") }); 
                helper.setCc(new InternetAddress[] { new InternetAddress("xxxxxx@qq.com"), 
                        new InternetAddress("xxxxxx@qq.com") }); 
                helper.setSubject("测试spring Mail附件"); 
                // 需要将附件显示在html中 
                helper.setText("你好,java", true); 
                FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql")); 
                helper.addAttachment("zqt.sql", attachment); 
                FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc")); 
                helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题 
     
                mailSender.send(mime); 
     
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
     
        } 
     
        public static void main(String[] args) { 
            SpringMailUtil springMailUtil = new SpringMailUtil(); 
            // 简单邮件 
            // springMailUtil.simpleSend(); 
            // 附件 
            // springMailUtil.attachmentSend(); 
            // 多附件 
            // springMailUtil.moreAttachmentSend(); 
            // 抄送 
            // springMailUtil.copySend(); 
            // 多附件、多人发送/抄送 
            springMailUtil.moreUserSend(); 
            System.out.println("发送成功"); 
     
        } 
    } 
分享到:
评论

相关推荐

    Spring Boot整合邮件发送并保存历史发送邮箱

    邮件发送者 from 一般采用固定的形式写到配置文件中。 (2)富文本邮件 在日常使用的过程中,通常在邮件中加入图片或者附件来丰富邮件的内容 发送 HTML 格式邮件 邮件发送支持以 HTML 的形式去构建我们喜欢的...

    Spring AOP、IOC、cxf、任务调度所需jar包以及配置文件

    包含了spring的ioc,aop,任务调度,cxf、jws示例以及邮件发送跟任务调度。里面包含了需要用到的jar包以及xml配置文件。还有示例代码。很方便学习

    Spring Boot邮件发送(powernode document)(源代码)

    2.2修改yml配置文件 2.3编写测试发送邮件 2.4 测试结果 2.4.1 发送基本内容 2.4.2 发送复杂内容 我们使用java程序发送邮件,属于使用第三方客户端发送邮件 使用第三方客户端发送邮件也需要先通过认证(登录): 官方...

    spring-boot-邮件发送

    添加依赖:在Spring Boot项目的pom.xml文件中加入spring-boot-starter-mail依赖,如果需要发送模板邮件,还可以加入Thymeleaf相关的依赖。 配置邮件:在application.properties或者application.yml中配置邮件发送的...

    Spring Boot邮件发送(powernode CD2207)(教学视频+源代码)

    Spring Boot邮件发送(powernode CD2207)(教学视频+源代码) SpringBoot提供了发送邮件的功能 SpringBoot实现邮件功能是非常的方便...3.2 application.yml配置文件 3.3 发送普通内容的邮件 3.4 发送复杂内容的邮件

    基于Spring的邮件发送系统设计源码

    Spring邮件发送系统:基于Java构建,包含67个文件,包括60个XML配置文件、3个Java类文件、2个.gitignore文件、1个Idea项目文件(.iml)和1个YAML配置文件。该项目是一个Spring email的最简单入门案例,用于演示如何...

    编程语言+JAVAspring+邮件服务+邮件发送

    编程语言+JAVAspring+邮件...它介绍了JAVAspring的邮件服务的概念、原理和作用,以及如何使用JAVAspring的邮件服务来发送邮件,包括邮件的配置、构建、发送、接收、附件、模板等内容,以及一些配置文件和注解的用法。

    spring发送邮件

    本文档主要是讲述使用spring自带的邮件发送功能,以配置文件的方式来完成

    spring整合quartz定时发送邮件

    该文档写了spring结合quartz定时发送邮件,有详细的配置文件,和相关代码,稍加研究就可以运行。

    Spring编写的发邮件程序,带定时功能

    我自己从pro spring书上修改过来的发邮件程序,在配置文件javaMailSender.xml中把自己邮箱的地址,用户名,密码,发送邮件的服务器修改一下就能使用了。 此邮件程序还有定时功能,到你定的时刻后它就开始发邮件,...

    Spring Cloud alibaba 集成 SkyWalking 日志 以及告警邮件

    使用邮件请在gateway模块配置文件中配置自己的邮箱信息 跟着课件学习的,启动服务可能需要关联启动:nacos sentinel seata skywalking其中某些中间件 配合博客...

    springboot实现邮箱发送功能

    本案例是通过qq邮箱配置之后通过springboot结合JavaMailSenderIm 发送邮件内容.包含带有纯文本/带有附件文件的

    spring4.1核心包

    配置文件 创建和管理bean。 4.spring-context-4.1.1.RELEASE.jar 在基础IOC功能上提供扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的...

    maven3搭建的spring邮件工程

    参照Maven3 in Action编写的由Maven3搭建的发送邮件的工程,只要简单的修改配置文件和测试类中的邮件用户信息即可运行。

    spring-boot示例项目

    config|[Spring Cloud Alibaba(二)配置中心多项目、多配置文件、分目录实现](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README2.md) Sentinel|[Spring Cloud Alibaba(三)Sentinel...

    基于Springboot+Mybatis+ SpringMvc+springsecrity+Redis完整网站后台管理系统

    邮件管理:发送邮件、搜索邮件 文件管理:上传文件、文件列表、文件删除 公告管理:公告未读提醒、发布公告、查询公告、公告阅读人列表 excel下载:自定义sql导出excel、也可在页面展示sql结果数据 字典管理:...

    spring boot 全面的样例代码

    - chapter2-1-1:[配置文件详解:自定义属性、随机数、多环境配置等](http://blog.didispace.com/springbootproperties/) ### Web开发 - chapter3-1-1:[构建一个较为复杂的RESTful API以及单元测试]...

    Spring 2.0 开发参考手册

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form...

    Spring-Reference_zh_CN(Spring中文参考手册)

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form标签 ...

Global site tag (gtag.js) - Google Analytics