[蓝莓商城]使用Spring的mail API发送邮件

2024-06-15 01:08

本文主要是介绍[蓝莓商城]使用Spring的mail API发送邮件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

蓝莓商城

今天使用Spring的mail API发送邮件。
下面说一下流程。

Maven依赖
<dependency>  <groupId>javax.mail</groupId>  <artifactId>mail</artifactId>  <version>1.4.7</version>  
</dependency>  
<!--Spring mail 类库-->
<dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>5.0.4.RELEASE</version>
</dependency>
创建bean

启用邮箱smtp功能
这里写图片描述

可以看到smtp的服务器地址是:smtp.sina.com。即为下面的host值。
填入该邮箱的username和password,邮件将会从该邮箱发出。

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host" value="smtp.sina.com"></property><property name="username" value="xxxx@sina.com"></property><property name="password" value="xxxxx"></property><property name="javaMailProperties"><props><prop key="mail.smtp.auth">true</prop><prop key="mail.smtp.timeout">20000</prop></props></property></bean>

创建自定义发送的bean,

<bean id="userMailSender" class="org.lanmei.cms.email.UserMailSender"><property name="mailSender" ref="mailSender" /> 
</bean>

完整的配置文件:applicationContext-email.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: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.3.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host" value="smtp.sina.com"></property><property name="username" value="lanmeishop1@sina.com"></property><property name="password" value="lanmei"></property><property name="javaMailProperties"><props><prop key="mail.smtp.auth">true</prop><prop key="mail.smtp.timeout">20000</prop></props></property></bean><bean id="userMailSender" class="org.lanmei.cms.email.UserMailSender"><property name="mailSender" ref="mailSender" /> </bean>
</beans>
编写UserMailSender类

mailSender已经在bean定义中进行依赖注入。

package org.lanmei.cms.email;import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;public class UserMailSender {private JavaMailSenderImpl   mailSender;public void send(SimpleMailMessage mail) {mailSender.send(mail);}public JavaMailSenderImpl getMailSender() {return mailSender;}public void setMailSender(JavaMailSenderImpl mailSender) {this.mailSender = mailSender;}   
}
编写测试类
package org.lanmei.cms;import org.junit.Test;
import org.lanmei.cms.email.UserMailSender;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;public class UserMailSenderTest {@Testpublic void emailTest() {ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-email.xml");UserMailSender sender = (UserMailSender) ac.getBean("userMailSender");SimpleMailMessage mail = new SimpleMailMessage();mail.setTo("xxxxx@qq.com");//收件人邮箱地址mail.setFrom("xxxxx@sina.com");//发件人,需与mailSender  bean中的username一致mail.setSubject("spring自带javamail发送的邮件");//主题mail.setText("hello this mail is from spring javaMail ");//正文sender.send(mail);}}
完成

这篇关于[蓝莓商城]使用Spring的mail API发送邮件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1061998

相关文章

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

SpringBoot整合Zuul全过程

《SpringBoot整合Zuul全过程》Zuul网关是微服务架构中的重要组件,具备统一入口、鉴权校验、动态路由等功能,它通过配置文件进行灵活的路由和过滤器设置,支持Hystrix进行容错处理,还提供... 目录Zuul网关的作用Zuul网关的应用1、网关访问方式2、网关依赖注入3、网关启动器4、网关全局变

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术