二、配置文件org.springframework.boot spring-boot-starter-mail
我只开启了qq的imap/smtp
#mail spring.mail.port=465 spring.mail.username=你的邮箱账户 spring.mail.password=imap/smtp的授权码 spring.mail.host=smtp.qq.com spring.mail.properties.mail.smtp.ssl.enable=true三、 收件
业务需要我这里只是获取了未读邮件的数量,想要获取其他具体的内容(如:邮件的标题,内容等。。)在folder的方法中获取即可(可以查看 https://www.cnblogs.com/shihaiming/p/10416383.html )。
方法外定义ssl加密
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//方法代码块
Properties props = new Properties();
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.host", "imap.qq.com");
props.setProperty("mail.imap.port", "993");
// 创建会话
Session session = Session.getInstance(props);
try{
// 创建IMAP协议的Store对象
Store store = session.getStore("imap");
// 连接邮件服务器
store.connect("imap.qq.com",993,"你的邮箱账户", "imap授权码");
// 获得收件箱
Folder folder = store.getFolder("INBOX");
// 以读写模式打开收件箱
folder.open(Folder.READ_WRITE);
System.out.println("收件箱中共" + folder.getUnreadMessageCount() + "封未读邮件!");
int unReadCount=folder.getUnreadMessageCount();
folder.close(false);
store.close();
return Result.ok(unReadCount);
}catch (Exception e){
log.warn("用户{}连接邮箱异常",username);
}
四、发件
收发件(发件一样,他这里收件是使用163邮箱收件)
https://www.cnblogs.com/shihaiming/p/10416383.html
https://www.cnblogs.com/shihaiming/p/10416383.html
QQ邮箱开启imap/smtp服务
https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=166&&id=28



