Spring集成Java Mail

标签: spring java
319人阅读 评论(1) 收藏 举报
分类:

Java Mail简单使用

1、导入jar包

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.4</version>
</dependency>

2、使用Java Mail

public void testJavaMail() throws MessagingException {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.163.com");    // 使用163发件服务器
    props.put("mail.smtp.auth", "true");            // 启用验证

    Session session = Session.getInstance(props);
    session.setDebug(true);

    // 构造信息体
    MimeMessage message = new MimeMessage(session);

    // 发件地址
    Address address = new InternetAddress("发件地址@163.com");
    message.setFrom(address);

    // 收件
    Address toAddress = new InternetAddress("收件邮箱");
    message.setRecipient(RecipientType.TO, toAddress);

    // 主题
    message.setSubject("主题");

    // 正文
    message.setText("正文");
    message.saveChanges();

    Transport transport = session.getTransport("smtp");

    transport.connect("smtp.163.com", "发件邮箱", "邮箱密码");
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
}

Spring集成Java Mail

1、导入jar包

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.4</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.2.4.RELEASE</version>
</dependency>

2、编写Java Mail配置文件:mail.properties

#发件服务器主机
mail.smtp.host=smtp.163.com
mail.smtp.auth=true
mail.username=发件邮箱用户名
mail.password=密码
mail.from=发件邮箱

3、编写Java Mail的Spring配置文件:applicationContext-mail.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"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/aop    
    http://www.springframework.org/schema/aop/spring-aop.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context.xsd">

    <description>Java Mail Properties</description>

    <context:property-placeholder location="classpath:mail.properties"/>

    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
        <property name="from" value="${mail.from}"></property>
    </bean>

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.smtp.host}"></property>
        <property name="username" value="${mail.username}"></property>
        <property name="password" value="${mail.password}"></property>
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                <prop key="mail.debug">true</prop>
                <prop key="mail.smtp.timeout">0</prop>
            </props>
        </property>
    </bean>

</beans>

4、编写代码

(1)发送简单文本邮件

@Test
public void testSimpleJavaMail() {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

    SimpleMailMessage mailMessage = (SimpleMailMessage) context.getBean("mailMessage");
    JavaMailSenderImpl mailSender = (JavaMailSenderImpl) context.getBean("mailSender");

    mailMessage.setTo("收件邮箱");
    mailMessage.setSubject(主题");
    mailMessage.setText("内容");

    mailSender.send(mailMessage);
}

(2)发送带附件的邮件

@Test
public void testJavaMail() throws MessagingException {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    JavaMailSender sender = (JavaMailSender) context.getBean("mailSender");

    MimeMessage message = sender.createMimeMessage();

    MimeMessageHelper helper = new MimeMessageHelper(message, true);

    helper.setFrom("发件邮箱");
    helper.setTo("收件邮箱");
    helper.setSubject("主题");

    helper.setText("<html><head></head><body><h1>Title</h1>"
                +"<a href=http://www.baidu.com>baidu</a>"   + "<img src=cid:image/></body></html>", true);

    FileSystemResource image = new FileSystemResource(new File("图片路径"));
    helper.addInline("image", image);

    FileSystemResource zip = new FileSystemResource(new File("附件路径"));
    helper.addAttachment("附件名称", zip);

    sender.send(message);
}

发送邮件的工具类

MailUtils.java

public class MailUtils {

    public static void sendMessage(String toAddr, String subject, String content) throws Exception {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.163.com");    // 设置发件服务器
        props.put("mail.smtp.auth", "true");

        Session session = Session.getInstance(props);
        session.setDebug(true);

        // 构造信息体
        MimeMessage message = new MimeMessage(session);

        // 发件地址
        Address address = new InternetAddress("发件邮箱");
        message.setFrom(address);

        // 收件
        Address toAddress = new InternetAddress(toAddr);
        message.setRecipient(RecipientType.TO, toAddress);

        // 主题
        message.setSubject(subject);

        // 正文
        message.setText(content);
        message.saveChanges();

        Transport transport = session.getTransport("smtp");

        transport.connect("smtp.163.com", "发件邮箱", "密码");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}
查看评论

Spring Mail 集成

Spring对mail功能的集成。
  • u011846818
  • u011846818
  • 2015-04-20 17:36:28
  • 862

JavaMail与Spring整合

简介javaMail与spring整合完成后,可大大加大邮件发送效率。当服务器一启动,配置文件就已加载。直接保存用户信息时,邮件可直接发送,大大提高了效率。1.引入坐标 ...
  • mrlin6688
  • mrlin6688
  • 2016-12-07 00:44:43
  • 2943

Spring boot 集成邮件服务

添加依赖 org.springframework.boot spring-boot-starter-mail 添加配置文件stmp.host=smtp.163.com stmp.acc...
  • u010236169
  • u010236169
  • 2016-07-28 16:35:26
  • 1536

spring集成javamail发送邮件

1. 依赖(1)spring基本jar包 &amp;lt;dependency&amp;gt; &amp;lt;groupId&amp;gt;org.springframework&...
  • baidu_30809315
  • baidu_30809315
  • 2017-12-22 12:08:05
  • 70

Spring配置邮件服务

Spring配置邮件服务(1)配置邮件发送服务器     方法1:显示的配置SMTP邮件服务器                          mail.springinaction.com    ...
  • daryl715
  • daryl715
  • 2007-06-05 21:50:00
  • 2819

Spring Boot(十二)集成spring-boot-starter-mail发送邮件

项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot(十二)集成spring-boot-starter...
  • a286352250
  • a286352250
  • 2016-11-14 13:20:35
  • 8807

Spring Javamail实现发送各种邮件

Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性...
  • zmx729618
  • zmx729618
  • 2016-04-06 16:03:18
  • 4277

spring-boot mail 邮件发送

刚开始学习spring-boot,所以就从demo入手,整好需要发送邮件功能,所以就以发送邮件为目的来学习使用spring-boot. 一、POM文件 在pom.xml中增加以下依赖 o...
  • zl18310999566
  • zl18310999566
  • 2016-12-29 17:54:31
  • 2349

springmvc框架下使用java mail发送邮件

1:需要mail.jar包,这个可以去下载 2:java类 (1)重要文件配置(发短信方的账号密码和host等) public class ParamConfig {          //发邮件  ...
  • pangliang_csdn
  • pangliang_csdn
  • 2016-08-09 16:27:19
  • 4324

spring 配置 java mail 发送邮件

配置文件
  • zh921112
  • zh921112
  • 2014-08-06 10:55:27
  • 15632
    个人资料
    持之以恒
    等级:
    访问量: 2万+
    积分: 481
    排名: 10万+
    最新评论