JavaMail receives return letters from hotmail

Time:2022-1-5

Hotmail is my most commonly used email client. Although the capacity of hotmail is smaller, the speed is often too slow to help greeting Gates’s mother. However, it is undeniable that it has rich functions and is relatively stable. In particular, its page design is an important reference for web programmers such as me.

Unlike many POP3 servers, hotmail implements multipart / report. The email client I wrote in the past only supports receiving multipart / related, multipart / alternative, multipart / mixed and other multimedia emails. I ignored others because they are not common.

The customer didn’t agree. What else to say? What’s more important than satisfying the customer? Before paying the bill.

For the introduction of multipart / report mail types, please refer to rfc1892. Sun’s Chinese JavaMail FAQ also has relevant instructions. I excerpt as follows:

Ask When the message cannot be delivered, a failure message is returned. How do I detect these “rebound” messages?

A: Although there is an Internet standard for reporting such errors (multipart / report MIME type, see rfc1892), it has not been widely implemented. Rfc1211 discusses this issue in depth, including a large number of examples.

In Internet e-mail, the existence of a specific mailbox or user name can only be determined by the final server delivering the message. Messages may pass through several relay servers that cannot detect errors before reaching the final server. Usually, when the final server detects this error, it will return a message to the sender of the original message, indicating the cause of the failure. Many Internet standards discuss this delivery status notifications, but a large number of servers do not support these new standards. Instead, they use special techniques to return this error message. This makes it difficult to correlate the “rebound” message with the original message that caused the problem (note that this problem has nothing to do with JavaMail).

There are many techniques and heuristics to deal with this problem, but they are not perfect. One technology is variable envelope return paths, http://cr.yp.to/proto/verp.txt This technique is described.

I modified the program and only did some simple processing. The code is as follows:

  private void parseMultipart(Multipart multipart) throws Exception {
  for (int i = 0, n = multipart.getCount(); i < n; i++) {
  BodyPart part = multipart.getBodyPart(i);
  String disposition = part.getDisposition();

  if (part.isMimeType(“multipart/*”)) {
   this.parseMultipart((Multipart) part.getContent());
  } else if (part.isMimeType(“text/html”)) {
/ / process text content
  }
/ ******* process attachments, pictures, etc********/
  } else if (part.isMimeType(“message/delivery-status”)) {
   part. setFileName(“details.txt”); // Follow outlook, details Txt content is the header information of the delivery status notification
   saveAttachedFile(part);
  } else if (part.isMimeType(“message/rfc822”)) {
   MimeBodyPart mbp = new MimeBodyPart(part.getInputStream());
   part. setFileName(mbp.getHeader(“Subject”, null) + “.eml”); // Follow outlook and use the title of the returned item as the file name
   saveAttachedFile(part);
  }
  }
  }

If you want to be more friendly, you can also make many improvements, such as finding the original in the sent folder according to the return letter, or triggering a notification event, etc. The customer doesn’t need it. I’d better not be amorous.

As we all know, hotmail uses WebDAV protocol instead of POP3 protocol. Use java to send and receive hotmail email. See accessing hotmail mailbox through JavaMail.

Recommended Today

How to scan and repair Linux disk errors

In this article, you will learn how to use fsck and XFS_ The repair command fixes Linux disk errors.What is fsck?Fsck (file system consistency check) is a tool used to check the file system consistency of Linux operating system.List Linux disk partitions and types:First, you need to determine the disk partitions of the Linux system, […]