There are several processes for receiving mail (with attachments)
1. Receiving mail
a. determine the protocol, port, password and other parameters of the object mailbox
b. configure (protocol, port, and server) parameters through the Properties class
c. use session to store these configurations for defining environment information
d. use the Store to access the parameters in the session to connect to the mailbox
f. get inbox () through Folder (Folder is the object to operate mail)
2. Analyze mail
a. get mail in inbox through Message []
b. get a single Message processing object through MimeMessage, which is a Message subclass, and the instance object represents a Message
3. Save attachments
a. get complex mail processing objects through Multipart
b. get an email body of a complex email through BodyPart. BodyPart is an attribute of Multipart. Nodes such as pictures and attachments can be combined into a mixed Multipart whole through BodyPart
4. Read and process input stream
Learning links:
Application of MIME Protocol in mail
Simple example of JavaMail sending mail
Attach the personal learning demo:
public class mytest { public static void main(String[] args) throws Exception { String duankou = "110";// Port number String servicePath = "pop.qiye.aliyun.com";// Server address (recipient) String xieyi = "pop3";// Protocol (mailbox type) String mail = " ";// mailing address String password = ""; String mydes = "H:\\ ";// Attachment saving path (file does not need to be named separately) receive(duankou, servicePath, xieyi, mail, password, mydes); } // Receiving mail public static void receive(String duankou, String servicePath, String xieyi, String mail, String password, String mydes) throws Exception { // Preparing session information to connect to the server Properties props = new Properties(); props.setProperty("mail.store.protocol", xieyi); // Use pop3 protocol (specify protocol to receive mail) // props.setProperty("mail.transport.protocol", xieyi); / / use pop3 protocol (specify the protocol to send mail) props.setProperty("mail.pop3.port", duankou); // port props.setProperty("mail.pop3.host", servicePath); // pop3 server // Create Session instance object Session session = Session.getInstance(props); Store store = session.getStore(xieyi); // store.connect(host, user, password); store.connect(mail, password); // Get inbox Folder folder = store.getFolder("INBOX");//The name of a mail folder obtained through pop3 protocol can only be inbox folder.open(Folder.READ_WRITE); // Open inbox //folder.open(Folder.READ_ONLY); // Get all the messages in the inbox and parse them // Message[] messages = folder.getMessages(); Message[] messages = folder.search(new SubjectTerm("Message subject name to access")); parsingMessage(messages, mydes); } // Parsing mail public static void parsingMessage(Message[] messages, String mydes) throws Exception { // Resolve all messages for (int i = 0, count = messages.length; i < count; i++) { MimeMessage msg = (MimeMessage) messages[i]; msg.getMessageNumber();//What's the mail MimeUtility.decodeText(msg.getSubject());//Mail theme msg.getSentDate();//Sending time msg.getSize();//Message size (megabytes) boolean isContainerAttachment = isContainAttachment(msg);//Include attachments or not if (isContainerAttachment) { saveAttachment(msg, mydes); // Save attachments } StringBuffer content = new StringBuffer(30);//content is the body of the message //Get message body content.toString();//Application to plain text //getMailTextContent(msg, content); / / for complex body mail } } //Get message text content /* public static void getMailTextContent(Part part, StringBuffer content) throws Exception { boolean isContainTextAttach = part.getContentType().indexOf("name") > 0; if (part.isMimeType("text/*") && !isContainTextAttach) {//The message is plain text and the text is not empty content.append(part.getContent().toString()); } else if (part.isMimeType("message/rfc822")) { //message/rfc822 Single mail body file getMailTextContent((Part) part.getContent(), content); } else if (part.isMimeType("multipart/*")) { //multipart/*Complex mail, such as mail with attachments Multipart multipart = (Multipart) part.getContent(); int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); getMailTextContent(bodyPart, content); } } } **/ // Save attachments public static void saveAttachment(Part part, String destDir) throws Exception { if (part.isMimeType("multipart/*")) { // multipart / * messages with attachments Multipart multipart = (Multipart) part.getContent(); // Complex mail // Complex message contains multiple message bodies int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { // Get one of the complex body messages BodyPart bodyPart = multipart.getBodyPart(i); // A mail body may also be a complex body composed of multiple mail bodies String disp = bodyPart.getDisposition(); if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { // Indicates that the msg has content, the file has come down or can be opened InputStream is = bodyPart.getInputStream(); saveFile(is, destDir, MimeUtility.decodeText(bodyPart.getFileName())); //MimeUtility.decodeText file decoding } else if (bodyPart.isMimeType("multipart/*")) { saveAttachment(bodyPart, destDir); } else { String contentType = bodyPart.getContentType(); if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) { saveFile(bodyPart.getInputStream(), destDir, MimeUtility.decodeText(bodyPart.getFileName())); //MimeUtility.decodeText file decoding } } } } else if (part.isMimeType("message/rfc822")) { saveAttachment((Part) part.getContent(), destDir);// getContent is to get and display the message content } } // Include attachments or not public static boolean isContainAttachment(Part part) throws Exception { boolean flag = false; if (part.isMimeType("multipart/*")) { MimeMultipart multipart = (MimeMultipart) part.getContent(); int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); String disp = bodyPart.getDisposition(); if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { flag = true; } else if (bodyPart.isMimeType("multipart/*")) { flag = isContainAttachment(bodyPart); } else { String contentType = bodyPart.getContentType(); if (contentType.indexOf("application") != -1) { flag = true; } if (contentType.indexOf("name") != -1) { flag = true; } } if (flag) break; } } else if (part.isMimeType("message/rfc822")) { flag = isContainAttachment((Part) part.getContent()); } return flag; } // Read input is saved to the specified location in turn public static void saveFile(InputStream is, String destDir, String fileName) throws Exception { BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(destDir + fileName))); int len = -1; while ((len = bis.read()) != -1) { bos.write(len); bos.flush(); } bos.close(); bis.close(); } }