spring整合mail

1.创建简单maven项目

jdk1.8版本的

在这里插入图片描述

2.导入依赖pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spring.eamil</groupId>
  <artifactId>spring_mail</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
 
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.4.RELEASE</version>
    <scope>test</scope>
</dependency>
 <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.6.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

 
  </dependencies>
  
      <!--邮件解析必须-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.integration</groupId>
                <artifactId>spring-integration-bom</artifactId>
                <version>4.3.19.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

3.开启POP3/SMTP服务

打开邮箱,找到“设置”,然后找到“POP3/SMTP服务”,点击开启,他会弹出让你发送短信,发送完短信后,会出现8位密码,这密码下面会用到。

在这里插入图片描述

4.创建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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<context:component-scan base-package="com.xxx"/>
	
	<!-- 邮件发送器(提供了邮件发送接口、透明创建Java Mail的MimeMessage、及邮件发送的配置 -->
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.qq.com"/>
		<!-- <property name="port" value="25"/> -->
		<property name="defaultEncoding" value="utf-8"/>
		<property name="username" value="xxx@qq.com"/><!-- 你的邮箱 -->
		<property name="password" value="xxxxx"/> <!--密码,需要在邮箱设置里开启POP3/SMTP服务,点击密码,会让你发送短信,然后发送完短信后,会出现八位密码  -->
	</bean>
	
	<!-- 普通文本邮件对象 -->
	<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
		<property name="from" value="xxx@qq.com"/><!-- 你的邮箱 -->
		<property name="subject" value="spring_mail"/><!-- 主题名 -->
	</bean>
	
</beans>




5.创建接口和实现类

package com.xxx;

public interface OrderManager {

	public void placeOrder();
}

package com.xxx.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import com.xxx.OrderManager;

@Service
public class OrderManagerImpl implements OrderManager{
	
	@Autowired
	private MailSender mailSender;
	
	@Autowired
	private SimpleMailMessage templateMessage;

	public void placeOrder() {
		SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
		//发送谁
		msg.setTo("xxxx@qq.com");
		//内容
		msg.setText("Hello World");
		this.mailSender.send(msg);
	}

}

6.测试简单文本发送

package com.xxx.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xxx.OrderManager;

public class MailTest {
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		OrderManager orderManager = applicationContext.getBean(OrderManager.class);
		orderManager.placeOrder();
	}

}

7.测试附件发送

package com.xxx.test;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class AttachMail {

	public static void main(String[] args) throws MessagingException {
		//加载Spring的上下文环境
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		//获取邮件发送器的bean对象
		JavaMailSender mailSender = (JavaMailSender) applicationContext.getBean("mailSender");
		//获取邮件对象
		MimeMessage message = mailSender.createMimeMessage();
		//设置邮件主题
		message.setSubject("Spring mail测试");
		//创建带有附件的消息帮助类
		MimeMessageHelper helper = new MimeMessageHelper(message,true,"UTF-8");
		//设置邮件接受者
		helper.setTo("接收者邮箱");
		//设置邮件的发送者
		helper.setFrom("发送者邮箱");
		//设置邮件内容
		helper.setText("这是附件spring整合");
		//设置附件
		File file = new File("D:\\user.txt");
		//添加附件
		helper.addAttachment(file.getName(), file);
		//发送邮件
		mailSender.send(message);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值