springBoot中的邮件发送
1. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2. impl
public String sendWord() {
User user1 = new User();
user1.setId("1");
user1.setUsername("李四");
user1.setSex("男");
User user2 = new User();
user2.setId("2");
user2.setUsername("小王");
user2.setSex("女");
User user3 = new User();
user3.setId("3");
user3.setUsername("张三");
user3.setSex("男");
List<User> list = new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
try {
Workbook wb = new SXSSFWorkbook(500);
CellStyle style = wb.createCellStyle();
Sheet sheet = wb.createSheet();
sheet.setColumnWidth(0, 6200);
sheet.setColumnWidth(1, 3800);
sheet.setColumnWidth(2, 6200);
for (int i = 3; i <= 11; i++) {
sheet.setColumnWidth(i, 3766);
}
Row row = sheet.createRow(0);
row.setRowStyle(style);
Cell cell0 = row.createCell(0);
cell0.setCellStyle(style);
cell0.setCellValue("id");
Cell cell1 = row.createCell(1);
cell1.setCellStyle(style);
cell1.setCellValue("姓名");
Cell cell2 = row.createCell(2);
cell2.setCellStyle(style);
cell2.setCellValue("性别");
int rownum = 1;
CellStyle cellStyle = wb.createCellStyle();
for (User user : list) {
Row r = sheet.createRow(rownum);
r.createCell(0);
Cell c0 = r.createCell(0);
c0.setCellStyle(cellStyle);
c0.setCellValue(user.getId());
Cell c1 = r.createCell(1);
c1.setCellStyle(cellStyle);
c1.setCellValue(user.getUsername());
Cell c2 = r.createCell(2);
c2.setCellStyle(cellStyle);
c2.setCellValue(user.getSex());
rownum++;
}
// wb.write(out);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos);
baos.flush();
byte[] bt = baos.toByteArray();
InputStream is = new ByteArrayInputStream(bt, 0, bt.length);
baos.close();
String email = "接收人的@qq.com";
sendMail("保险产品审核推送表", email, "邮件正文", is);
return "success";
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}
/**
*
* @param subject 邮件的主题
* @param toMail 接收人的邮箱
* @param content 邮件的正文
* @param is 发送的附件为流内容
* @return
*/
public static boolean sendMail(String subject, String toMail, String content, InputStream is) {
boolean isFlag = false;
try {
String smtpFromMail = "xxxx@163.com.cn"; //账号
String pwd = "password"; //密码
int port = 25; //端口
String host = "smtp.163.com"; //邮件服务器地址
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(false);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(smtpFromMail, "发件人的名称"));
/*批量发送*/
// InternetAddress[] adc = {new InternetAddress("1067584215@qq.com"), new InternetAddress("1436495697@qq.com")};
// message.addRecipients(Message.RecipientType.TO, adc);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
message.setSubject(subject);
message.addHeader("charset", "UTF-8");
/*添加正文内容*/
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setText(content);
contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");
multipart.addBodyPart(contentPart);
/*添加附件*/
MimeBodyPart fileBody = new MimeBodyPart();
DataSource source = new ByteArrayDataSource(is, "application/msexcel");
fileBody.setDataHandler(new DataHandler(source));
String fileName = "附件.xlsx";
// 中文乱码问题
fileBody.setFileName(MimeUtility.encodeText(fileName));
multipart.addBodyPart(fileBody);
message.setContent(multipart);
message.setSentDate(new Date());
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, port, smtpFromMail, pwd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
isFlag = true;
} catch (Exception e) {
e.printStackTrace();
isFlag = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return isFlag;
}
3. yml配置
server:
port: 8081
spring:
mail:
host: smtp.163.com
username: xxx@163.com
password: password
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
springBoot中的邮件发送的更多相关文章
- Springboot+Javamail实现邮件发送
Springboot+Javamail实现邮件发送 使用的是spring-context-support-5.2.6.RELEASE.jar里的javamail javamail 官方文档:javam ...
- 用ASP.NET Core 1.0中实现邮件发送功能
准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试 ...
- Linux中Postfix邮件发送配置(三)
部署DNS服务器 postfix根据域名和地址做一个MX记录,A记录,PTR记录(一般在互联网上邮件服务器都要反解,没有PTR记录会认为是垃圾邮件) $ service iptables stop $ ...
- SpringBoot实现QQ邮件发送
建项目 创建一个SpringBoot项目 改pom,导入相关依赖 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE & ...
- 用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇
在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还 ...
- Linux中的邮件发送
这里写出两种常用的邮件发送方式: mail: 需要安装sendmail和postfix两个服务 编辑/etc/mail.rc,在最后添加 set from=scottcho@126.com smtp= ...
- springboot下实现邮件发送功能
springboot给我们封装好了邮件功能,非常简单,只需要稍微配置下就ok. 引入jar <dependency> <groupId>org.springframework. ...
- 使用Springboot Email实现邮件发送
在springboot配置文件增加emai配置(此种方式不支持QQ邮箱): spring.datasource.type=com.alibaba.druid.pool.DruidDataSource ...
- C#--中实现邮件发送
MailMessage mailmessage = new MailMessage(); mailmessage.To.Add("接受邮箱");//可以添加多个接收邮箱 mailm ...
随机推荐
- 030、Java中的求模计算
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- MySQL如何获取时间戳
时间戳函数:current_timestamp() 将时间列格式设为timestamp,设定其默认值为CURRENT_TIMESTAMP. 插入一条新纪录,数据库就会自动在时间列存储当前时间.
- mysql 结果排序入门
- C#动态获取本机可用串口的两种方式
1. private void GetSerialPort() //获取串口列表 { RegistryKey keyCom = Registry.LocalMachine.OpenSubKey(&qu ...
- redis.conf文件配置
最重要三个配置 1. bind 127.0.0.1 需要注释掉这一行,使别的主机可以访问 2. daemonize no 需要改为yes,使其后台运行 3. requirepass foobared ...
- C++面试常见问题——06数组排序
数组排序 冒泡.最简单的冒泡,没啥好讲的 #include<iostream> using namespace std; void BubbleSort(int a[],int len){ ...
- 逆战:微信小程序
微信小程序的生命周期 onLaunch: function(options) { // ...
- 第三节MapStruct翻译--Defining a mapper
第三节MapStruct--Defining a mapper 在这一章节你将学到如何用mapstruct和它的一些必要的操作选项来定义一个bean mapper. 3.1 Basic mapping ...
- Web前端开发CSS规范总结
作为Web前端开发必备语言,CSS为大家广为熟知,今天就跟大家分享下CSS规范总结,Web前端的小伙伴们看过来吧! CSS样式的权值(权重) 权值等级的定义 第一等:代表内联样式,如: style=” ...
- 留学Essay写作关键:Intensive Reading
留学生的日常除了写写写还是写写写,有时候还是要换换口味.在自己没有作业压力的时候可以尝试去读一些相关书籍或者一些优秀的essay.当然了,这里的阅读可不是走马观花,囫囵吞枣的读,而是用心去“精读”.那 ...