最后
光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性
Java面试宝典2021版
最常见Java面试题解析(2021最新版)
2021企业Java面试题精选
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
### application.yml配置
![在这里插入图片描述](https://img-blog.csdnimg.cn/87e5c22c5a1743e39c500317a561d473.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/178ab6f0551a4d5a9393db9f5d402aa9.png)
### email业务类
>
> 接口
>
>
>
package com.tianju.auth.service;
public interface IEmailService {
/**
* 发送右键
* @param to 邮件接收方
* @param subject 邮件主题
* @param content 邮件内容
*/
void sendEmail(String to,String subject,String content);
}
>
> 实现
>
>
>
package com.tianju.auth.service.impl;
import com.tianju.auth.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
@Service
@Slf4j
public class EmailServiceImpl implements IEmailService {
@Value("${spring.mail.username}")
private String from;
@Resource
private JavaMailSender javaMailSender;
@Override
public void sendEmail(String to, String subject, String content) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject(subject);
mailMessage.setTo(to);
mailMessage.setText(content);
mailMessage.setSentDate(new Date());
mailMessage.setFrom(from);
javaMailSender.send(mailMessage);
log.debug("在{}发送一条邮件{}给{}",mailMessage.getSentDate(),mailMessage.getText(),mailMessage.getTo());
}
}
### 测试类
package com.tianju.auth.service.impl;
import com.tianju.auth.service.IEmailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.xml.ws.soap.Addressing;
import java.util.UUID;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class EmailServiceImplTest {
@Autowired
private IEmailService emailService;
@Test
public void sendEmail() {
emailService.sendEmail("xxxx@qq.com", "我是老王,我在测试代码", UUID.randomUUID().toString());
}
}
## springboot整合阿里云短信服务
### 申请阿里云短信服务
[短信服务 (aliyun.com)]( )
![在这里插入图片描述](https://img-blog.csdnimg.cn/5e595034c3cf4d339d1e4179d9bcce90.png)
### 测试短信服务
![在这里插入图片描述](https://img-blog.csdnimg.cn/45da1c7a1ca9406ba7c685686dd585e5.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/7ad0a1ff6e2842d6a2bdb551f57ac952.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/7d3c42a28bdd4216baf2243a7c759b7d.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/7b7f69719e09419798f84ca86e387ff0.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/7cd77ee2b55c4c36b15a79277379ec0f.png)
>
> 进行测试
>
>
>
![在这里插入图片描述](https://img-blog.csdnimg.cn/298f55205ef54fa0bfaee0d20f99be58.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/b836ebc4df6a40708299ad77fb832531.png)
>
> 调用结果
>
>
>
![在这里插入图片描述](https://img-blog.csdnimg.cn/c3ec7763f7cc44bba9d954c5c5c7cc9a.png)
>
> 成功接收短信
>
>
>
![在这里插入图片描述](https://img-blog.csdnimg.cn/15567de2ba0c46918a46143ff3c1a3f1.png)
### 获取阿里云的accessKey
![在这里插入图片描述](https://img-blog.csdnimg.cn/f4750b507d624791aabf538a0e3da52b.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/adf1be4e30d64f6eb8557a8c619fa5d4.png)
### springboot整合阿里云短信
#### 导包
<!-- 阿里云短信验证码相关包-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.3</version>
</dependency>
#### 工具类
package com.tianju.auth.util;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import net.minidev.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
本次面试答案,以及收集到的大厂必问面试题分享:
面试题分享:
[外链图片转存中…(img-G7bzmBOD-1715468969064)]