前置条件
开启qq邮箱的授权码,使用授权码登录.
maven
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
</dependency>
代码示例
QQ内容Class
public class QqEmailContent {
private String subject; // 邮箱标题
private String from; // 邮箱发送者
private String content; // 邮箱内容
public QqEmailContent(String subject, String from, String content) {
this.subject = subject;
this.from = from;
this.content = content;
}
public QqEmailContent() {
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "QqEmailContent{" +
"subject='" + subject + '\'' +
", from='" + from + '\'' +
", content='" + content + '\'' +
'}';
}
}
获取QQ邮箱最近一封邮件,根据自己的需求更改服务"smtp.qq.com"和端口,我默认是关闭了加密
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class QQEmailUtils {
protected static String username = "xxx@qq.com"; // 使用QQ邮箱账号
protected static String password = "xxxx"; // 使用授权码
public static QqEmailContent getFistMailContent() {
QqEmailContent qqEmailContent = new QqEmailContent();
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "false"); // 关闭TLS加密
props.put("mail.smtp.ssl.enable", "false"); // 关闭SSL加密
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Store store = session.getStore("imap"); // 使用IMAP协议
store.connect("imap.qq.com", username, password); // 连接IMAP服务器
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount(); // 获取邮件总数
Message[] messages = inbox.getMessages(messageCount, messageCount); // 获取最新的一封邮件
for (Message message : messages) {
qqEmailContent.setSubject(message.getSubject());
qqEmailContent.setFrom(String.valueOf(message.getFrom()[0]));
qqEmailContent.setContent(parseRelatedMessage((MimeMessage) message));
return qqEmailContent;
}
inbox.close(false);
store.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
public static String parseRelatedMessage(MimeMessage message) throws Exception {
Multipart multipart = (Multipart) message.getContent();
String mainContent = null;
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if (bodyPart.getContentType().toLowerCase().contains("related")) {
// 获取主体内容
mainContent = getText(bodyPart);
assert mainContent != null;
Document doc = null;
try {
doc = Jsoup.parse(mainContent);
} catch (Exception e) {
return mainContent;
}
mainContent = doc.text();
}
}
return mainContent;
}
private static String getText(BodyPart bodyPart) throws Exception {
if (bodyPart.getContent() instanceof String) {
return (String) bodyPart.getContent();
} else if (bodyPart.getContent() instanceof Multipart) {
StringBuilder sb = new StringBuilder();
Multipart multipart = (Multipart) bodyPart.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart part = multipart.getBodyPart(i);
if (part.getDisposition() == null && (part.getContentType().contains("TEXT/PLAIN") || part.getContentType().contains("TEXT/HTML"))) {
sb.append(part.getContent().toString());
}
}
return sb.toString();
}
return null;
}
public static void main(String[] args) {
System.out.println(getFistMailContent());
}
}