Spring整合JavaMail

本文介绍如何在Spring框架中配置并实现简单的文本邮件及包含HTML内容、图片与附件的复杂邮件的发送。通过配置文件和Java代码示例详细展示了整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.添加jar包

#此处省略spring基础相关jar包描述,以下是发送邮件相关jar包
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

2.配置文件

1.mail.properties
mail.smtp.host=smtp.163.com
mail.username=邮箱帐号(例如:abc)
mail.password=邮箱密码(例如:123456)
mail.smtp.auth=true
mail.from=发送邮箱(例如:abc@163.com)
2.applicationContext-mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <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>

3.Java代码

import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
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;

public class MailTest {

    @Test
    public void test() throws MessagingException {
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
    
        SimpleMailMessage message = (SimpleMailMessage) act.getBean("mailMessage");
        message.setSubject("约会");
        message.setText("2017年5月10日在西湖边见面");
        message.setTo("xxxx@163.com");
        
        JavaMailSender sender = (JavaMailSender) act.getBean("mailSender");
        sender.send(message);
        
    }
    
    @Test
    public void test2() throws MessagingException {
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
    
        JavaMailSender sender = (JavaMailSender) act.getBean("mailSender");
        MimeMessage message = sender.createMimeMessage();
        //使用工具类设置附件
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("aaa@163.com");
        helper.setTo("bbb@163.com");
        helper.setSubject("来自百度的主题");
        helper.setText("<html><head></head><body><h2>你好</h2>"
                + "<a href='http://www.baidu.com'>百度</a>"
                + "<img src=cid:image></body></html>",true);
        
        //带图片
        FileSystemResource jpg = new FileSystemResource(new File("d:\\test.jpg"));
        helper.addInline("image", jpg);//此处的image必须与html代码段中的<img src=cid:image>一致
        
        //带附件
        FileSystemResource attach = new FileSystemResource(new File("d:\\intro.csv"));
        helper.addInline("intro.csv", attach);
        
        sender.send(message);
        
    }
}

转载于:https://www.cnblogs.com/moonlightL/p/7271325.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值