mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-07 01:18:33 +08:00
修复 邮件监听在复杂邮件下无法识别主体内容
This commit is contained in:
parent
2f5cbf6e5c
commit
c1a4482797
@ -5,13 +5,19 @@ import org.dromara.email.comm.config.MailImapConfig;
|
||||
import org.dromara.email.comm.entity.MonitorMessage;
|
||||
import org.dromara.email.comm.errors.MailException;
|
||||
|
||||
import javax.mail.BodyPart;
|
||||
import javax.mail.Flags;
|
||||
import javax.mail.Folder;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Multipart;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Store;
|
||||
import javax.mail.search.FlagTerm;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Properties;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
@ -47,7 +53,7 @@ public class MonitorService{
|
||||
private void startListening() {
|
||||
try {
|
||||
Folder inbox = store.getFolder("Inbox");
|
||||
inbox.open(Folder.READ_ONLY);
|
||||
inbox.open(Folder.READ_WRITE);
|
||||
Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
|
||||
for (Message message : messages) {
|
||||
MonitorMessage monitorMessage = new MonitorMessage();
|
||||
@ -57,9 +63,25 @@ public class MonitorService{
|
||||
monitorMessage.setTitle(message.getSubject());
|
||||
// 获取邮件的内容
|
||||
if (message.isMimeType("text/plain")) {
|
||||
monitorMessage.setText(message.getContent().toString());
|
||||
Object content = message.getContent();
|
||||
if (content == null){
|
||||
StringBuilder stringBuilder = getStringBuilder(message);
|
||||
content = stringBuilder.toString();
|
||||
}
|
||||
monitorMessage.setText(content.toString());
|
||||
} else if (message.isMimeType("multipart/*")) {
|
||||
Multipart mp = (Multipart) message.getContent();
|
||||
for (int i = 0; i < mp.getCount();i++){
|
||||
BodyPart bodyPart = mp.getBodyPart(i);
|
||||
String contentType = bodyPart.getContentType().toLowerCase();
|
||||
if (contentType.startsWith("text/plain")) {
|
||||
// 纯文本内容
|
||||
monitorMessage.setText(bodyPart.getContent().toString());
|
||||
} else if (contentType.startsWith("text/html")) {
|
||||
// HTML内容
|
||||
monitorMessage.setText(bodyPart.getContent().toString());
|
||||
}
|
||||
}
|
||||
monitorMessage.setBody(mp);
|
||||
}
|
||||
monitorMessage.setMessageIndex(message.getMessageNumber());
|
||||
@ -75,6 +97,20 @@ public class MonitorService{
|
||||
}
|
||||
}
|
||||
|
||||
private static StringBuilder getStringBuilder(Message message) throws IOException, MessagingException {
|
||||
InputStream inputStream = message.getInputStream();
|
||||
|
||||
// 解析输入流以获取内容
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
stringBuilder.append(line);
|
||||
}
|
||||
}
|
||||
return stringBuilder;
|
||||
}
|
||||
|
||||
public MonitorService start(){
|
||||
Timer timer = new Timer();
|
||||
this.timer = timer;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user