xinyb
2024-09-19 1956855b43808d81f246d9570d477ac1cbc404b7
提交 | 用户 | age
7caeae 1 package com.yc.crm.mail.service;
X 2
3 import com.sun.mail.imap.IMAPBodyPart;
4 import com.sun.mail.imap.IMAPStore;
5 import com.yc.crm.mail.entity.FoundationEntity;
6 import com.yc.crm.mail.entity.MailFileEntity;
7 import com.yc.crm.mail.entity.T482102Entity;
8 import com.yc.crm.mail.entity.t482101HEntity;
9 import com.yc.entity.attachment.AttachmentEntity;
10 import com.yc.service.BaseService;
11 import org.apache.commons.io.FileUtils;
12 import org.apache.commons.lang3.StringUtils;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.stereotype.Service;
15
16 import javax.mail.*;
17 import javax.mail.internet.*;
18 import javax.servlet.http.HttpServletRequest;
19 import java.io.*;
20 import java.text.SimpleDateFormat;
7cf738 21 import java.time.LocalDate;
X 22 import java.time.ZoneId;
7caeae 23 import java.util.*;
X 24
25 import static com.yc.crm.mail.service.MailImpl.shoppingImageServer;
26
27 /**
28  * @BelongsProject: eCoWorksV3
29  * @BelongsPackage: com.yc.crm.mail.service
30  * @author: xinyb
31  * @CreateTime: 2024-09-12  10:30
32  * @Description:
33  */
34 @Service
35 public class MailServiceImpl extends BaseService implements MailServiceIfc {
36     @Autowired
37     MailIfc mailIfc;
38     @Autowired
39     MailAccountIfc mailAccountIfc;
195685 40     @Autowired
X 41     MailFileIfc mailFileIfc;
7caeae 42
X 43     @Override
44     public void receivingEmails(T482102Entity emailEntity, FoundationEntity foundation) throws MessagingException {
048b74 45         String protocol = emailEntity.getReceiveProtocol().toLowerCase();//接收协议 imap pop3
X 46         String server = emailEntity.getReceiveHost();//"imap.qq.com";
47         Integer port = emailEntity.getReceivePort();//993
48         String user = emailEntity.getReceiveEmail();//邮箱
49         String pwd = emailEntity.getReceivePassword();//密码
7caeae 50
X 51         Properties properties = new Properties();
57c8bf 52         properties.setProperty("mail.store.protocol", protocol); // IMAP over SSL
048b74 53         if (protocol.contains("imap")) {//接收协议imap
57c8bf 54             properties.setProperty("mail.imaps.host", server);
X 55             properties.setProperty("mail.imaps.port", port + "");
048b74 56         } else if (protocol.contains("pop3")) {//接收协议pop3
57c8bf 57             properties.setProperty("mail.pop3.host", server);
X 58             properties.setProperty("mail.pop3.port", port + "");
048b74 59         } else {//其他(再加)
57c8bf 60             properties.setProperty("mail.imaps.host", server);
X 61             properties.setProperty("mail.imaps.port", port + "");
048b74 62         }
X 63 //        properties.put("mail.imaps.starttls.enable", "true");//// IMAP 协议设置 STARTTLS
7caeae 64
X 65         HashMap IAM = new HashMap();
66         //带上IMAP ID信息,由key和value组成,例如name,version,vendor,support-email等。
048b74 67         IAM.put("name", user);
7caeae 68         IAM.put("version", emailEntity.getDocVersion() + "");
X 69         IAM.put("vendor", emailEntity.getCompanyName());
70         IAM.put("support-email", emailEntity.getEmail());
71         //创建会话
72         Session session = Session.getInstance(properties, new Authenticator() {
73             @Override
74             protected PasswordAuthentication getPasswordAuthentication() {
75                 return new PasswordAuthentication(user, pwd);
76             }
77         });
78         //存储对象
048b74 79         IMAPStore store = (IMAPStore) session.getStore(protocol);//imap协议或pop3协议类型(推荐你使用IMAP协议来存取服务器上的邮件。)
7caeae 80         //连接
048b74 81         store.connect(server, user, pwd);
7caeae 82         store.id(IAM);//163邮箱需要,不然会报:A3 NO SELECT Unsafe Login. Please contact kefu@188.com for help
X 83         Folder folder = null;
84         try {
85             // 获得收件箱
86             folder = store.getFolder("INBOX");
87             // 以读写模式打开收件箱
88             folder.open(Folder.READ_WRITE);
7cf738 89
X 90             //false 表示未读 ,true已读,获得收件箱的邮件列表
7caeae 91 //            FlagTerm flagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), true);
X 92 //            Message[] messages = folder.search(flagTerm);
7cf738 93
7caeae 94             //获取收件箱邮件(全部)
X 95             Message[] messages = folder.getMessages();
7cf738 96
7caeae 97             //邮箱封装保存
ad7c8d 98             setMailContent(messages, emailEntity, foundation);
7caeae 99         } catch (NoSuchProviderException e) {
X 100             throw e;
101         } catch (MessagingException e) {
102             throw e;
ad7c8d 103         } catch (IOException e) {
X 104             throw new RuntimeException(e);
7caeae 105         } catch (Exception e) {
X 106             throw new RuntimeException(e);
107         } finally {
108             try {
109                 if (folder != null) {
110                     folder.close(false);
111                 }
112                 if (store != null) {
113                     store.close();
114                 }
115             } catch (MessagingException e) {
116                 throw e;
117             }
118         }
119     }
120
121
122     @Override
123     public void sendEmails(t482101HEntity t482101H, HttpServletRequest request) throws Exception {
124         try {
125             //根据当前用户查询绑定的邮箱信息
126             T482102Entity emailEntity = mailAccountIfc.getAccountInfo(t482101H.getUserCode(), t482101H.getSender());//返回邮箱的账号信息
127             if (emailEntity == null || StringUtils.isBlank(emailEntity.getSmtpEmail())) {
128                 if (StringUtils.isBlank(t482101H.getDocCode())) {
129                     t482101H.setMailType(0);//草稿箱状态
130                     t482101H = mailIfc.saveReceivingMail(t482101H);//保存到草稿箱内
131                 }
f6ae0b 132                 throw new MessagingException("找不到邮箱:" + t482101H.getSender() + "配置信息请完善。邮件已保存到草稿箱#" + t482101H.getDocCode());
7caeae 133             }
X 134             //有发送的邮件保存到后台数据库
135             //邮箱服务器配置
136             Properties properties = new Properties();
048b74 137             properties.setProperty("mail.smtp.auth", "true");// // 设置SMTP是否需要认证 指定客户端是否向邮件服务器提交认证
X 138             properties.setProperty("mail.transport.protocol", "smtp");//设置传输协议 指定采用的邮件发送协议。
57c8bf 139             properties.setProperty("mail.smtp.ssl.enable", emailEntity.isSmtpSSL() + "");// // 设置启用SSL加密
7caeae 140             properties.setProperty("mail.smtp.host", emailEntity.getSmtpHost());
X 141             properties.setProperty("mail.smtp.port", emailEntity.getSmtpPort() + "");
048b74 142
X 143 //            properties.setProperty("mail.smtp.starttls.enable", "true");//// SMTP 协议设置 STARTTLS  开启tls
144 //            properties.setProperty("mail.smtp.socketFactory.port", emailEntity.getSmtpPort() + "");
7caeae 145
X 146             //
147             String sendEmail = emailEntity.getSmtpEmail();//发件人
148             String sendPassword = emailEntity.getSmtpPassword();//密码
149             String recipientEmail = StringUtils.join(t482101H.getReceiver(), ",");//收件人
150             Session session = Session.getInstance(properties, new Authenticator() {
151                 @Override
152                 protected PasswordAuthentication getPasswordAuthentication() {
153                     return new PasswordAuthentication(sendEmail, sendPassword);
154                 }
155             });
156
157             MimeMessage message = new MimeMessage(session);
158             message.setFrom(new InternetAddress(sendEmail));
159             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
57c8bf 160             message.setSubject(t482101H.getSubject());//主题
7caeae 161
X 162             String Unique_ID = "<onBus_" + UUID.randomUUID().toString().toUpperCase() + "@mail.com>";//系统单号唯一码
163             message.setHeader("Message-ID", Unique_ID);//系统内部唯一码
164             t482101H.setMessageId(Unique_ID);//messageId赋值
165
166             if (t482101H.getCc() != null && t482101H.getCc().size() > 0) {//抄送
167                 message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(StringUtils.join(t482101H.getCc(), ",")));
168             }
169             if (t482101H.getBcc() != null && t482101H.getBcc().size() > 0) {//密送
170                 message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(StringUtils.join(t482101H.getBcc(), ",")));
171             }
172
173             //创建多部分消息
174             Multipart multipart = new MimeMultipart();
175
176             MimeBodyPart textPart = new MimeBodyPart();
177             textPart.setText(t482101H.getContent());//内容
178             multipart.addBodyPart(textPart);
179
180             //下面附件内容
181             if (StringUtils.isNotBlank(t482101H.getAttachmentList())) {//有附件内容
182                 //获取到附件内容
195685 183                 List<AttachmentEntity> attachmentEntities = mailFileIfc.getAttachmentEntityList(t482101H.getAttachmentList());
7caeae 184                 if (attachmentEntities != null && attachmentEntities.size() > 0) {
X 185                     for (AttachmentEntity a : attachmentEntities) {
186                         InputStream inputStream = new ByteArrayInputStream(a.getOriginalPicture());
187                         File file = new File(a.getOriginalFileName());
188                         FileUtils.copyInputStreamToFile(inputStream, file);
189                         MimeBodyPart bodyPart = new MimeBodyPart();
190                         bodyPart.attachFile(file);
191                         bodyPart.setFileName(a.getOriginalFileName());
192                         multipart.addBodyPart(bodyPart);
193                     }
194                 }
195             }
196             message.setContent(multipart);
197             try {
198                 if (StringUtils.isBlank(t482101H.getDocCode())) {//不在草稿箱有单号
199                     mailIfc.saveReceivingMail(t482101H);//保存后发送
200                 } else {
201                     t482101H.setMailType(2);//发件箱状态
202                     mailIfc.updateMailDrafts(t482101H);//更新成发件箱
203                 }
204                 Transport.send(message);//发送
205             } catch (MessagingException m) {//发送异常处理
206                 t482101H.setMailType(0);//草稿箱状态
207                 mailIfc.updateMailDrafts(t482101H);//更新成草稿箱
048b74 208                 throw new MessagingException("发送邮件异常,邮件已保存在草稿箱。异常原因:" + m.getMessage() + "#" + t482101H.getDocCode());
7caeae 209             }
X 210         } catch (Exception e) {
211             throw e;
212         }
213     }
214
57c8bf 215     @Override
X 216     public Integer setQuickReply(String docCode, String content) throws MessagingException {
217         String sql = "set nocount on\n";
218         try {
219             sql += "declare @docCode varchar(200) ='" + docCode + "'\n";
220             sql += "update t482101H set receive_time=getdate() where docCode=@docCode\n";//更新回复时间
221             sql += "select a.subject,a.sender,a.receiver,a.messageid,b.smtpEmail,b.smtpPassword,b.smtpHost,b.smtpPort,isnull(b.smtpSSL,0) as smtpSSL " +
222                     " from t482101H a join t482102 b on a.receiver=b.smtpEmail where docCode=@docCode";
223             Map<String, Object> map = jdbcTemplate.queryForMap(sql);
224             if (map == null || map.get("sender") == null) {
225                 throw new MessagingException("回复人信息查找不到,请检查docCode唯一码");
226             }
227             String receiver = (String) map.get("sender");//收件人
228             String sender = (String) map.get("receiver");//发件人
229             String smtpSSL = map.get("smtpSSL").equals(0) ? "false" : "true";
230
231             Properties props = new Properties();
232             props.setProperty("mail.smtp.auth", "true");
233             props.setProperty("mail.transport.protocol", "smtp");//设置传输协议 指定采用的邮件发送协议。
234             props.setProperty("mail.smtp.ssl.enable", smtpSSL);// // 设置启用SSL加密
235             props.setProperty("mail.smtp.host", (String) map.get("smtpHost"));
236             props.setProperty("mail.smtp.port", map.get("smtpPort") + "");
237 //            props.setProperty("mail.smtp.starttls.enable", "true");
238
239             Session session = Session.getInstance(props, new javax.mail.Authenticator() {
240                 protected PasswordAuthentication getPasswordAuthentication() {
241                     return new PasswordAuthentication((String) map.get("smtpEmail"), (String) map.get("smtpPassword"));
242                 }
243             });
244
245             Message replyMessage = new MimeMessage(session);
246             replyMessage.setFrom(new InternetAddress(sender));
247             replyMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiver));//收件人
248             replyMessage.setSubject("Re: " + map.get("subject"));//主题
249
250             // 引用原邮件
251             replyMessage.setHeader("In-Reply-To", (String) map.get("messageid"));
252             replyMessage.setHeader("References", (String) map.get("messageid"));
253
254             replyMessage.setText(content);//这里是回复的内容
255
256             Transport.send(replyMessage);
257
258             return 1;
259         } catch (MessagingException e) {
260             throw new MessagingException("快速回复异常:" + e.getMessage());
261         } catch (Exception e) {
262             throw e;
263         }
264     }
265
7caeae 266     /**
X 267      * 收件箱返回的信息进行封装处理
268      *
269      * @param messages
270      * @return
271      * @throws MessagingException
272      * @throws IOException
273      */
57c8bf 274     public void setMailContent(Message[] messages, T482102Entity email, FoundationEntity foundation) throws
X 275             Exception {
7caeae 276         try {
X 277             List<t482101HEntity> t482101HEntityList = new ArrayList<>();
7cf738 278             List<String> messageIdList = mailIfc.getMessageIdList(email.getReceiveEmail());//存在系统表里的邮件
7caeae 279             Message m = null;
7cf738 280             LocalDate startTime = LocalDate.parse(email.getCreateTime().split(" ")[0]);//创建时间
X 281             LocalDate endTime = LocalDate.parse(email.getUpdateTime().split(" ")[0]);//更新时间
282             if (startTime.equals(endTime)) {
283                 startTime = endTime.minusDays(30);
284             } else {
285                 startTime = endTime;
286                 endTime = LocalDate.now();
287             }
288             Date nowTime = new Date();
7caeae 289             for (int i = 0; i < messages.length; i++) {
X 290                 m = messages[i];
7cf738 291                 LocalDate sendDate = m.getSentDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
X 292                 if ((sendDate.isAfter(startTime) || sendDate.isEqual(startTime)) &&
293                         (sendDate.isBefore(endTime) || sendDate.isEqual(endTime))) {//时间段获取邮件
294                     t482101HEntity mail = new t482101HEntity();
295                     String messageId = m.getHeader("Message-ID")[0];
296                     if (messageIdList != null && messageIdList.contains(messageId)) {//存在就不组装
297                         continue;
7caeae 298                     }
7cf738 299                     mail.setMessageId(messageId);//获取邮件唯一ID
X 300                     mail.setMailType(1);//收件
301                     if (m.isSet(Flags.Flag.SEEN)) {//邮件已标记为已读
302                         mail.setReadFlag(1);//已读
303                     }
304                     if (m.isExpunged()) {//检查一个消息是否已被删除。‌
305                         mail.setDeleteFlag(1);//已删除
306                     }
307                     if (m.isSet(Flags.Flag.ANSWERED)) {//邮件是否已回复
308
309                     }
310                     if (m.isSet(Flags.Flag.DRAFT)) {//是否是草稿箱
311                         mail.setMailType(0);//是草稿箱
312                     }
313                     mail.setUserCode(foundation.getUserCode());
314                     mail.setUserName(foundation.getUserName());
315                     mail.setCompanyId(foundation.getCompanyId());
316                     mail.setCompanyName(foundation.getCompanyName());
317                     mail.setClassType(1);
318                     mail.setSubject(m.getSubject());//标题
319                     Date date = m.getReceivedDate();//时间
320                     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
321                     mail.setReceiveTime(formatter.format(date));
322                     String result = "";
323                     StringBuilder plainText = new StringBuilder();//纯文本
324                     if (m.isMimeType("text/plain")) {
325                         plainText.append(m.getContent());
326                     } else if (m.isMimeType("text/html")) {//html格式
327                         result = m.getContent().toString();
328                     } else if (m.isMimeType("multipart/*")) {
329                         MailFileEntity mailFile = new MailFileEntity();//附件
330                         List<MailFileEntity.MailBodyPart> part = new ArrayList<>();
331                         String unId = UUID.randomUUID().toString().toUpperCase();
332                         StringBuilder attachment = new StringBuilder();
333                         result = getTextFromMimeMultipart((MimeMultipart) m.getContent(), plainText, part, unId, attachment);
334                         mailFile.setPart(part);
335                         mailFile.setUnId(unId);//生成uuid
336                         mail.setMailFile(mailFile);//附件添加到里面
337                         if (StringUtils.isNotBlank(attachment)) {//附件游标保存
338                             mail.setAttachFlag(1);
339                             mail.setAttachmentList(unId + attachment.toString());
340                         }
341                     } else {
342                         result = m.getContent().toString();
343                     }
344                     mail.setContent(result);//保存内容
345                     mail.setPlainText(plainText.toString().trim());//明文
346                     String from = MimeUtility.decodeText(m.getFrom()[0].toString());
347                     InternetAddress internetAddress = new InternetAddress(from);
ad7c8d 348                     mail.setSenderName(internetAddress.getPersonal());//发件人名称
7cf738 349                     mail.setSender(internetAddress.getAddress());//发件人
X 350                     from = MimeUtility.decodeText(m.getRecipients(Message.RecipientType.TO)[0].toString());
351                     internetAddress = new InternetAddress(from);
7caeae 352 //                mail.setReceiver(internetAddress.getAddress());//收件人
7cf738 353                     List<String> receivers = new ArrayList<>();//抄送人
X 354                     receivers.add(email.getReceiveEmail());
355                     mail.setReceiver(receivers);//统一成一个收件人
7caeae 356
7cf738 357                     Address[] ccAddress = m.getRecipients(Message.RecipientType.CC);
X 358                     if (ccAddress != null && ccAddress.length > 0) {
359                         List<String> cc = new ArrayList<>();//抄送人
360                         for (Address c : ccAddress) {
361                             from = MimeUtility.decodeText(c.toString());
362                             internetAddress = new InternetAddress(from);
363                             cc.add(internetAddress.getAddress());
364                         }
365                         mail.setCc(cc);
7caeae 366                     }
7cf738 367                     Address[] bccAddress = m.getRecipients(Message.RecipientType.BCC);
X 368                     if (bccAddress != null && bccAddress.length > 0) {
369                         List<String> bcc = new ArrayList<>();//密送
370                         for (Address c : bccAddress) {
371                             from = MimeUtility.decodeText(c.toString());
372                             internetAddress = new InternetAddress(from);
373                             bcc.add(internetAddress.getAddress());
374                         }
375                         mail.setBcc(bcc);//密送人
7caeae 376                     }
7cf738 377                     t482101HEntityList.add(mail);
7caeae 378                 }
X 379             }
380             if (t482101HEntityList.size() > 0) {
381                 mailIfc.saveReceivingMailList(t482101HEntityList);//保存
ad7c8d 382                 mailAccountIfc.updateEmailTime(email.getAccountId());//更新一次配置
7caeae 383             }
X 384         } catch (Exception e) {
385             throw e;
386         }
387     }
388
389     /**
390      * @param mimeMultipart 邮箱对象
391      * @param plainText     纯文本
392      * @param part          邮件附件集合
393      * @return
394      * @throws Exception
395      */
396     // 辅助方法,用于递归获取纯文本邮件内容
57c8bf 397     private String getTextFromMimeMultipart(MimeMultipart mimeMultipart, StringBuilder
X 398             plainText, List<MailFileEntity.MailBodyPart> part, String unId, StringBuilder attachment) throws Exception {
7caeae 399         int count = mimeMultipart.getCount();
X 400         if (count == 0) {
401             throw new MessagingException("Multipart with no body parts");
402         }
403         boolean multipartAlternative = isMultipartAlternative(mimeMultipart);
404         StringBuilder result = new StringBuilder();
405         if (multipartAlternative) {
406             for (int i = 0; i < count; i++) {
407                 BodyPart bodyPart = mimeMultipart.getBodyPart(i);
408                 if (bodyPart.isMimeType("text/plain")) {//这个是获取纯文本
409                     plainText.append(bodyPart.getContent());
410                 }
411                 if (bodyPart.isMimeType("text/html")) {//这个是获取html格式
412                     result.append(bodyPart.getContent());
413                 }
414             }
415         } else {
416             for (int i = 0; i < count; i++) {
417                 BodyPart bodyPart = mimeMultipart.getBodyPart(i);
418                 if (bodyPart.isMimeType("image/*")) {//图片
419                     MailFileEntity.MailBodyPart p = new MailFileEntity.MailBodyPart();
7cf738 420                     String fileName = MimeUtility.decodeText(bodyPart.getFileName());
X 421                     p.setFileName(fileName);//有些邮件没有扩展名
7caeae 422                     String cId = ((IMAPBodyPart) bodyPart).getContentID();//获取cId
7cf738 423                     if (StringUtils.isNotBlank(cId)) {//在有扩展名时候
X 424                         if (cId.lastIndexOf(".") != -1) {
425                             p.setFileType(cId.substring(cId.lastIndexOf(".") + 1));
426                             p.setFileName(cId);//有些邮件没有扩展名
427                         }
428                     }
429                     if (StringUtils.isBlank(p.getFileType()) && StringUtils.isNotBlank(fileName)) {
430                         if (fileName.lastIndexOf(".") != -1) {
431                             p.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
432                         }
433                     }
7caeae 434                     p.setFileSize(bodyPart.getSize());
7cf738 435                     p.setByteFile(convertInputStreamToByteArray(bodyPart.getInputStream()));
X 436                     String fieldId = UUID.randomUUID().toString();//系统自定义一个随机字段
437                     p.setFieldId(fieldId);
7caeae 438                     String nextResult = result.toString();
X 439                     if (nextResult.contains(cId)) {//有嵌套内容
440                         //替换
195685 441                         nextResult = nextResult.replace("cid:" + cId + "", shoppingImageServer + "/uploads/email/82/482101/" + unId + "@p@" + p.getPhysicalFile());
7caeae 442                         result.setLength(0);//清空先
X 443                         result.append(nextResult);
444                     }
7cf738 445                     part.add(p);
7caeae 446                 } else if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {//附件
X 447                     MailFileEntity.MailBodyPart p = new MailFileEntity.MailBodyPart();
7cf738 448                     String fileName = MimeUtility.decodeText(bodyPart.getFileName());
X 449                     p.setFileName(fileName);
450                     if (StringUtils.isNotBlank(fileName)) {
451                         p.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
452                     }
7caeae 453                     p.setFileSize(bodyPart.getSize());
7cf738 454                     p.setByteFile(convertInputStreamToByteArray(bodyPart.getInputStream()));
X 455                     String fieldId = UUID.randomUUID().toString();//系统自定义一个随机字段
456                     p.setFieldId(fieldId);
457                     attachment.append(";" + fieldId);
7caeae 458                     part.add(p);
X 459                 } else if (bodyPart.isMimeType("multipart/*")) {
460                     result.append(getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent(), plainText, part, unId, attachment));
461                 }
462             }
463         }
464         return result.toString();
465     }
466
467     /**
468      * 辅助方法,判断是否为multipart/alternative类型的邮件
469      *
470      * @param mimeMultipart
471      * @return
472      * @throws Exception
473      */
474     private boolean isMultipartAlternative(MimeMultipart mimeMultipart) throws Exception {
475         boolean textPlainFound = false;
476         boolean textHtmlFound = false;
477         int count = mimeMultipart.getCount();
478         for (int i = 0; i < count; i++) {
479             BodyPart bodyPart = mimeMultipart.getBodyPart(i);
480             if (bodyPart.isMimeType("text/plain")) {
481                 textPlainFound = true;
482             } else if (bodyPart.isMimeType("text/html")) {
483                 textHtmlFound = true;
484             }
485         }
486         return textPlainFound && textHtmlFound;
487     }
488
489     /**
490      * 转换 byte
491      *
492      * @param inputStream
493      * @return
494      * @throws IOException
495      */
496     private byte[] convertInputStreamToByteArray(InputStream inputStream) throws IOException {
497         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
498         byte[] buffer = new byte[1024];
499         int length;
500         while ((length = inputStream.read(buffer)) != -1) {
501             byteArrayOutputStream.write(buffer, 0, length);
502         }
503         return byteArrayOutputStream.toByteArray();
504     }
505 }