spingboot之Java邮件发送
2024-10-07 04:54:05
注意: 该项目的工具类可以直接应用于项目
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1..RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rocketmq</groupId>
<artifactId>demo</artifactId>
<version>0.0.-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--基础 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- activemq -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.</version>
</dependency>
<!-- spring-activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency> <!-- rocketmq -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.2.</version>
</dependency>
<!-- fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
注意:pom.xml包含了一些其他的jar依赖,其实是不需要的,只需要有:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、application.properties
注意:这个配置文件只是针对QQ邮箱,如果是163或者其他企业邮箱,需要根据需要配置
server.port=
spring.mail.host=smtp.qq.com
spring.mail.username=自己的qq邮箱
spring.mail.password=密码,不是登陆密码,是开启smtp的授权码
spring.mail.default-encoding=UTF-
spring.mail.port=
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
3、SendMailUtil.java
package com.rocketmq.demo.Utils; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.internet.MimeMessage;
import java.io.File; @Component
public class SendMailUtil {
@Autowired
JavaMailSender mailSender; /**
* 发送文本消息
* @param senderMail 发送者邮箱
* @param receiveMail 接受者邮箱
* @param subject 主题
* @param text 主体内容
* @return
*/
public String sendMail(String senderMail,String receiveMail,String subject,String text){
try {
final MimeMessage message = this.mailSender.createMimeMessage();
final MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(senderMail);//发送者邮箱
helper.setTo(receiveMail);//接收者邮箱
helper.setSubject(subject);
helper.setText(text);
this.mailSender.send(message);
return "sucesss";
} catch (Exception ex) {
ex.printStackTrace();
return "error";
}
} /**
* 带附件发送邮件----可多个附件 图片,doc都可以发送
* @param senderMail 发送者邮箱
* @param receiveMail 接受者邮箱
* @param subject 主题
* @param text 主体内容
* @param fileArray 附件路径
* @return
*/
public String sendMailFile(String senderMail,String receiveMail,String subject,String text,String[] fileArray) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(senderMail);
helper.setTo(receiveMail);
helper.setSubject(subject);
helper.setText(text);
//验证文件数据是否为空
if (null != fileArray) {
FileSystemResource file = null;
for (int i = 0; i < fileArray.length; i++) {
//添加附件
file = new FileSystemResource(fileArray[i]);
helper.addAttachment(fileArray[i].substring(fileArray[i].lastIndexOf(File.separator)), file);
}
}
mailSender.send(message);
System.out.println("带附件的邮件发送成功");
return "sucesss";
} catch (Exception e) {
e.printStackTrace();
System.out.println("发送带附件的邮件失败");
return "error";
}
}
}
4、mailController.java
package com.rocketmq.demo.controller; import com.rocketmq.demo.Utils.SendMailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/mail")
public class mailController {
@Autowired
SendMailUtil sendMailUtil; /**
* 发送文本消息,不带附件
* @return
*/
@ResponseBody
@RequestMapping("/send")
public Object sendEmail() {
String senderMial = "发送者@qq.com";
String receiveMail = "接收者@qq.com";
String subject = "测试主题";
String text = "测试内容";
return sendMailUtil.sendMail(senderMial,receiveMail,subject,text);
} @GetMapping("/sendFile")
public String sendAttachmentsMail() {
String senderMial = "发送者@qq.com";
String receiveMail = "接收者@qq.com";
String subject = "测试主题";
String text = "测试内容";
String[] path={"C:\\Users\\yangwj\\Desktop\\公司\\正式员工详细信息表.xls"};
return sendMailUtil.sendMailFile(senderMial,receiveMail,subject,text,path); } }
总结:
setFrom:指的是发送者的邮箱,要和配置文件的一致。
具体代码:https://github.com/812406210/RocketMQ-and-mail.git
最新文章
- ASP.NET Button、ImageButton、LinkButton、HyperLink区别
- 改变UIButton 图片和文字的位置
- 转--Android实用的代码片段 常用代码总结
- IOS开发之上传APP
- EXTJS 4.2 资料 将store 传到后台
- Extjs发票管理系统
- HTML 中<;style>;中<;/style>;里面<;!-- -->;标签是干嘛的
- 【转】java中float与byte[]的互转 -- 不错
- ERROR 1062 (23000): Duplicate entry &#39;1-1&#39; for key &#39;PRIMARY&#39;
- mysql5.1,5.5,5.6做partition时支持的函数
- tomcat安装自制作ssl证书
- Javascript 设计模式 单例
- IDEA Failed to load dx.jar
- 【Spring Boot】使用JDBC 获取相关的数据
- tar命令-压缩,解压缩文件
- java算法----排序----(5)归并排序
- 将.ipynb文件导入到另外的文件中
- P1417 烹调方案 背包DP
- ZOJ 2476 Total Amount 字符串模拟
- hdu 1532 最大流