Preconditions
Open the authorization code of the qq mailbox and log in with the authorization code.
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>
Code sample
QQ content Class
public class QqEmailContent { private String subject; // mailbox subject private String from; // email sender private String content; // mailbox 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 + '\'' + '}'; } }
Get the latest email from QQ mailbox, and change the service “smtp.qq.com” and port according to your own needs. By default, encryption is turned off
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 = "[email protected]"; // use QQ mailbox account protected static String password = "xxxx"; // use authorization code 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"); // turn off TLS encryption props.put("mail.smtp.ssl.enable", "false"); // Turn off SSL encryption Session session = Session. getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Store store = session.getStore("imap"); // use IMAP protocol store.connect("imap.qq.com", username, password); // connect to the IMAP server Folder inbox = store. getFolder("INBOX"); inbox.open(Folder.READ_ONLY); int messageCount = inbox.getMessageCount(); // Get the total number of messages Message[] messages = inbox.getMessages(messageCount, messageCount); // Get the latest email 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")) { // get body content 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 & amp; & amp; (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()); } }
Actual running verification
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge Java skill treeHomepageOverview 108572 people are studying systematically