From 3b74e3df72726e188d36393ecfd7964d095ef7e8 Mon Sep 17 00:00:00 2001
From: xinyb <574600396@qq.com>
Date: 星期一, 30 九月 2024 11:38:39 +0800
Subject: [PATCH] CRM邮箱推送和拉取

---
 src/com/yc/crm/mail/service/MailAccountImpl.java |   26 ++++
 src/com/yc/crm/mail/service/MailServiceIfc.java  |    2 
 src/com/yc/crm/mail/service/MailServiceImpl.java |  221 +++++++++++++++++++++----------------------
 src/com/yc/crm/mail/action/MailAccount.java      |   12 ++
 src/com/yc/crm/mail/action/MailController.java   |    2 
 src/com/yc/crm/mail/service/MailAccountIfc.java  |    2 
 src/com/yc/crm/schedule/EmailSchedule.java       |    3 
 src/com/yc/crm/mail/service/MailImpl.java        |   10 +
 src/com/yc/crm/mail/entity/T482102Entity.java    |    2 
 9 files changed, 151 insertions(+), 129 deletions(-)

diff --git a/src/com/yc/crm/mail/action/MailAccount.java b/src/com/yc/crm/mail/action/MailAccount.java
index f4f5859..ef1f360 100644
--- a/src/com/yc/crm/mail/action/MailAccount.java
+++ b/src/com/yc/crm/mail/action/MailAccount.java
@@ -2,16 +2,20 @@
 
 import com.sun.mail.imap.IMAPStore;
 import com.yc.action.mail.EmailConfigUtils;
+import com.yc.crm.mail.entity.FoundationEntity;
 import com.yc.crm.mail.entity.T482102Entity;
 import com.yc.crm.mail.service.MailAccountIfc;
 import com.yc.crm.mail.service.MailIfc;
+import com.yc.crm.mail.service.MailServiceIfc;
 import com.yc.crm.mail.util.AllBackMsg;
 import com.yc.entity.DataSourceEntity;
+import com.yc.factory.FactoryBean;
 import com.yc.multiData.MultiDataSource;
 import com.yc.multiData.SpObserver;
 import com.yc.utils.SessionKey;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.web.bind.annotation.*;
 
 import javax.mail.*;
@@ -34,12 +38,14 @@
 @RestController
 @RequestMapping("/crm/mail/account")
 public class MailAccount {
+    ThreadPoolTaskExecutor threadPoolExecutor = (ThreadPoolTaskExecutor) FactoryBean.getBean("threadPoolExecutor");//绾跨▼姹�
 
     @Autowired
     MailAccountIfc mailAccountIfc;
     @Autowired
     MailIfc mailIfc;
-
+    @Autowired
+    MailServiceIfc mailServiceIfc;
     /**
      * 娣诲姞閭閰嶇疆淇℃伅
      *
@@ -126,7 +132,7 @@
             if (StringUtils.isNotBlank(error)) {
                 account.setMailStatus("寮傚父");
             }
-            mailAccountIfc.addEmailAccount(account);//娣诲姞鏂扮殑閭閰嶇疆
+            account= mailAccountIfc.addEmailAccount(account);//娣诲姞鏂扮殑閭閰嶇疆
             Map<String, Object> map = new HashMap<>();
             if (StringUtils.isBlank(error)) {
                 map.put("code", 0);
@@ -136,6 +142,8 @@
                 map.put("userName", userName);
                 map.put("email", account.getEmail());
                 msg.setSuccess("宸蹭繚瀛�", map);
+                //澶氱嚎绋嬫敹閭欢
+                threadPoolExecutor.execute(new MailController.ReceivingEmailsSave(mailServiceIfc, account, new FoundationEntity(request)));
             } else {
                 map.put("code", -1);
                 map.put("mailStatus", "寮傚父");
diff --git a/src/com/yc/crm/mail/action/MailController.java b/src/com/yc/crm/mail/action/MailController.java
index 8171eeb..d475291 100644
--- a/src/com/yc/crm/mail/action/MailController.java
+++ b/src/com/yc/crm/mail/action/MailController.java
@@ -686,7 +686,7 @@
         return msg;
     }
 
-    private class ReceivingEmailsSave implements Runnable {
+    public static class ReceivingEmailsSave implements Runnable {
         final MailServiceIfc mailServiceIfc;
         final T482102Entity t482102;
         final FoundationEntity foundation;
diff --git a/src/com/yc/crm/mail/entity/T482102Entity.java b/src/com/yc/crm/mail/entity/T482102Entity.java
index fae8a1f..456bb19 100644
--- a/src/com/yc/crm/mail/entity/T482102Entity.java
+++ b/src/com/yc/crm/mail/entity/T482102Entity.java
@@ -39,7 +39,7 @@
     private boolean smtpSSL = true;//SMTP SSL鏄竴绉嶅熀浜嶴MTP鍗忚鐨勫姞瀵嗕紶杈撴柟寮忥紝閫氳繃SSL锛圫ecure Sockets Layer锛屽畨鍏ㄥ鎺ュ眰锛夊崗璁SMTP閫氫俊杩涜鍔犲瘑淇濇姢
     private Integer smtpPort = 0;//鍙戦偖浠舵湇鍔″櫒绔彛 濡傦細25
     private String smtpHost;//鍙戦偖浠舵湇鍔″櫒(绠�鍗曢偖浠朵紶閫佸崗璁湇鍔″櫒:smtp.163.com)
-    private boolean invalid;//鏄惁宸茬粦瀹�
+    private boolean invalid=true;//鏄惁宸茬粦瀹�
     private String createTime;//鍒涘缓鏃堕棿
     private String updateTime;//鏇存柊鏃堕棿
     private Integer docVersion;//褰撳墠鐗堟湰鍙�
diff --git a/src/com/yc/crm/mail/service/MailAccountIfc.java b/src/com/yc/crm/mail/service/MailAccountIfc.java
index 3b75231..f6149b2 100644
--- a/src/com/yc/crm/mail/service/MailAccountIfc.java
+++ b/src/com/yc/crm/mail/service/MailAccountIfc.java
@@ -11,7 +11,7 @@
      *
      * @param account
      */
-    abstract void addEmailAccount(T482102Entity account);
+    abstract T482102Entity addEmailAccount(T482102Entity account);
 
     /**
      * 淇敼閭閰嶇疆
diff --git a/src/com/yc/crm/mail/service/MailAccountImpl.java b/src/com/yc/crm/mail/service/MailAccountImpl.java
index a62ae7f..d995f2c 100644
--- a/src/com/yc/crm/mail/service/MailAccountImpl.java
+++ b/src/com/yc/crm/mail/service/MailAccountImpl.java
@@ -21,9 +21,16 @@
 public class MailAccountImpl extends BaseService implements MailAccountIfc {
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public void addEmailAccount(T482102Entity account) {
+    public T482102Entity addEmailAccount(T482102Entity account) {
         String sql = "set nocount on\n";
         try {
+            sql+="declare @hasMail varchar(200)\n" +
+                    "select @hasMail=isnull(email,'') from t482102 where email=" +GridUtils.prossSqlParm(account.getEmail())+
+                    "if @hasMail !=''\n" +
+                    "begin\n" +
+                    "raiserror('閭574600396@qq.com宸茬粡缁戝畾杩囷紝涓嶈兘閲嶅缁戝畾',16,1)\n" +
+                    "return\n" +
+                    "end \n";
             sql += "insert into t482102(companyId,companyName,email,password,alias_email,userCode,userName,receiveProtocol," +
                     "receiveEmail,receivePassword,receiveSSL,receivePort,receiveHost," +
                     "smtpEmail,smtpPassword,smtpSSL,smtpPort,smtpHost,invalid,bisync_flag,proxyFlag,mailType,mailStatus,create_time,update_time)\n" +
@@ -34,8 +41,19 @@
                     "'" + account.isReceiveSSL() + "'," + account.getReceivePort() + "," + GridUtils.prossSqlParm(account.getReceiveHost()) + "," +
                     GridUtils.prossSqlParm(account.getSmtpEmail()) + "," + GridUtils.prossSqlParm(account.getSmtpPassword()) + ",'" + account.isSmtpSSL() + "'," +
                     account.getSmtpPort() + "," + GridUtils.prossSqlParm(account.getSmtpHost()) + ",'" + account.isInvalid() + "','" + account.isBiSyncFlag() + "','" + account.isProxyFlag() + "'," +
-                    account.getMailType() + "," + GridUtils.prossSqlParm(account.getMailStatus()) + ",convert(varchar(19),getdate(),120),convert(varchar(19),getdate(),120))";
-            jdbcTemplate.update(sql);
+                    account.getMailType() + "," + GridUtils.prossSqlParm(account.getMailStatus()) + ",convert(varchar(19),getdate(),120),convert(varchar(19),getdate(),120)) \n";
+            sql += "select top 1 companyId,companyName,email,password,alias_email as aliasEmail,userCode,userName,accountId,receive_server_user_name as receiveServerUserName," +
+                    "isnull(bisync_flag,0) as biSyncFlag,isnull(proxyFlag,0) as proxyFlag,mailDomain,isnull(mailType,0) as mailType,mailStatus,keywordList,siteUrl,receiveProtocol," +
+                    "receiveEmail,receivePassword,receiveSSL,receivePort,receiveHost,smtpEmail,smtpPassword,smtpSSL,smtpPort,smtpHost," +
+                    "isnull(invalid,0) as invalid,create_time as createTime,update_time as updateTime,DocVersion " +
+                    "from t482102 " +
+                    "where userCode=" + GridUtils.prossSqlParm(account.getUserCode()) + " and email=" + GridUtils.prossSqlParm(account.getEmail());
+            sql += " order by accountId desc";
+            List<T482102Entity> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(T482102Entity.class));
+            if (list.size() > 0) {
+                return list.get(0);
+            }
+            return null;
         } catch (Exception e) {
             throw e;
         }
@@ -166,7 +184,7 @@
                     "isnull(invalid,0) as invalid,create_time as createTime,update_time as updateTime,DocVersion " +
                     "from t482102 " +
                     "where userCode=" + GridUtils.prossSqlParm(userCode) + " and accountId=" + accountId;
-            sql += " order by accountId";
+            sql += " order by accountId desc";
             List<T482102Entity> t482101HEntity = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(T482102Entity.class));
             if (t482101HEntity.size() > 0) {
                 return t482101HEntity.get(0);
diff --git a/src/com/yc/crm/mail/service/MailImpl.java b/src/com/yc/crm/mail/service/MailImpl.java
index 6508b19..4b65a54 100644
--- a/src/com/yc/crm/mail/service/MailImpl.java
+++ b/src/com/yc/crm/mail/service/MailImpl.java
@@ -250,7 +250,7 @@
                     "read_Flag,(case when (isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate()) then 1 else 0 end) as handle," +
                     "handle_time,sender_time,receiving_time from t482101H ";
             sql += " where isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate()";
-            sql += " and userCode=" + GridUtils.prossSqlParm(userCode) + " and receiver like '%" + email + "%'";
+            sql += " and userCode=" + GridUtils.prossSqlParm(userCode) ;//+ " and receiver like '%" + email + "%'";//涓嶉渶瑕佸甫閭鍦板潃
             sql += " order by docCode asc \n";
             sql += " OFFSET (" + page + " - 1) * " + limit + " ROWS FETCH NEXT " + limit + " ROWS ONLY";
             return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(t482101HList.class));
@@ -338,7 +338,7 @@
     public void updateRead(String docCode, boolean status) {
         String sql = " set nocount on \n";
         try {
-            sql += "declare @docCode varchar(200) ='" + docCode + "' \n";
+            sql += "declare @docCode varchar(1000) ='" + docCode + "' \n";
             sql += "update t482101H set read_flag=" + (status ? 1 : 0) + " where docCode in (select list from GetInStr(@docCode))\n";
             sql += "select @@ROWCOUNT";
             jdbcTemplate.queryForObject(sql, Integer.class);
@@ -506,16 +506,18 @@
         try {
             sql += "select count(*) from t482101H ";
             sql += " where userCode=" + GridUtils.prossSqlParm(userCode);
+            sql += " and isnull(delete_Flag,0) = 0 ";//鏈垹闄ょ殑
             if (mailType < 3) {
                 sql += " and mailType=" + mailType;//0锛氳崏绋跨 1锛氭敹浠剁 2锛氬彂浠剁
+            }
+            if (mailType == 3) {//寰呭鐞嗛偖浠�
+                sql += " and isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate() ";
             }
             if (StringUtils.isNotBlank(email)) {
                 if (mailType == 1) {
                     sql += " and receiver like '%" + email + "%'";
                 } else if (mailType == 2) {
                     sql += " and sender =" + GridUtils.prossSqlParm(email);
-                } else if (mailType == 3) {
-                    sql += " and isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate() ";
                 }
             }
             if (isNoRead) {//0琛ㄧず鏈锛�1琛ㄧず宸茶
diff --git a/src/com/yc/crm/mail/service/MailServiceIfc.java b/src/com/yc/crm/mail/service/MailServiceIfc.java
index 89491ab..c4f3ab1 100644
--- a/src/com/yc/crm/mail/service/MailServiceIfc.java
+++ b/src/com/yc/crm/mail/service/MailServiceIfc.java
@@ -49,6 +49,6 @@
      * @param email
      * @param foundation
      */
-    public void setMailContent(Message[] messages, T482102Entity email, FoundationEntity foundation)throws
+    public void setMailContent(Message[] messages, T482102Entity email, FoundationEntity foundation,boolean hasPush)throws
             Exception;
 }
diff --git a/src/com/yc/crm/mail/service/MailServiceImpl.java b/src/com/yc/crm/mail/service/MailServiceImpl.java
index 439a8da..b333f29 100644
--- a/src/com/yc/crm/mail/service/MailServiceImpl.java
+++ b/src/com/yc/crm/mail/service/MailServiceImpl.java
@@ -18,11 +18,11 @@
 
 import javax.mail.*;
 import javax.mail.internet.*;
+import javax.mail.search.ComparisonTerm;
+import javax.mail.search.ReceivedDateTerm;
 import javax.servlet.http.HttpServletRequest;
 import java.io.*;
 import java.text.SimpleDateFormat;
-import java.time.LocalDate;
-import java.time.ZoneId;
 import java.util.*;
 
 import static com.yc.crm.mail.service.MailImpl.shoppingImageServer;
@@ -94,11 +94,19 @@
 //            FlagTerm flagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), true);
 //            Message[] messages = folder.search(flagTerm);
 
-            //鑾峰彇鏀朵欢绠遍偖浠讹紙鍏ㄩ儴锛�
-            Message[] messages = folder.getMessages();
+            //鑾峰彇鏀朵欢绠遍偖浠讹紙30澶╋級
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+            String time=emailEntity.getCreateTime();
+            Date dateTime=sdf.parse(time);
+            Date thirtyDaysAgo = new Date(dateTime.getTime() - (30 * 24 * 60 * 60 * 1000L)); // 30澶╁墠鐨勬棩鏈�
+            Message[] messages = folder.search(new ReceivedDateTerm(ComparisonTerm.GE, thirtyDaysAgo));
+
+//            SearchTerm start=new FromTerm(new date)
+//            SearchTerm end=new BeforeTerm(new Date(""));//寮�濮�
+//            Message[] messages = folder.search(new AndTerm(start,end));//folder.getMessages();
 
             //閭灏佽淇濆瓨
-            setMailContent(messages, emailEntity, foundation);
+            setMailContent(messages, emailEntity, foundation,false);
         } catch (NoSuchProviderException e) {
             throw e;
         } catch (MessagingException e) {
@@ -279,132 +287,119 @@
      * @throws IOException
      */
     @Override
-    public void setMailContent(Message[] messages, T482102Entity email, FoundationEntity foundation) throws
+    public void setMailContent(Message[] messages, T482102Entity email, FoundationEntity foundation,boolean hasPush) throws
             Exception {
         try {
             List<t482101HEntity> t482101HEntityList = new ArrayList<>();
             List<String> messageIdList = mailIfc.getMessageIdList(email.getReceiveEmail());//瀛樺湪绯荤粺琛ㄩ噷鐨勯偖浠�
             Message m = null;
-            LocalDate startTime = LocalDate.parse(email.getCreateTime().split(" ")[0]);//鍒涘缓鏃堕棿
-            LocalDate endTime = LocalDate.parse(email.getUpdateTime().split(" ")[0]);//鏇存柊鏃堕棿
-            if (startTime.equals(endTime)) {
-                startTime = endTime.minusDays(30);
-            } else {
-                startTime = endTime;
-                endTime = LocalDate.now();
-            }
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-            Date nowTime = new Date();
             for (int i = 0; i < messages.length; i++) {
                 m = messages[i];
-                Date deliveryTime = m.getReceivedDate();//鏀朵欢鏃堕棿
-                Date senderTime = m.getSentDate();//鍙戜欢鏃堕棿
-                if (deliveryTime == null) {
+                String messageId = m.getHeader("Message-ID")[0];
+                if (messageIdList != null && messageIdList.contains(messageId)) {//瀛樺湪灏变笉缁勮
                     continue;
                 }
+                Date deliveryTime = m.getReceivedDate();//鏀朵欢鏃堕棿
+                Date senderTime = m.getSentDate();//鍙戜欢鏃堕棿
                 if (senderTime == null) {
                     senderTime = senderTime;
                 }
-                LocalDate sendDate = deliveryTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
-                if ((sendDate.isAfter(startTime) || sendDate.isEqual(startTime)) &&
-                        (sendDate.isBefore(endTime) || sendDate.isEqual(endTime))) {//鏃堕棿娈佃幏鍙栭偖浠�
-                    t482101HEntity mail = new t482101HEntity();
-                    String messageId = m.getHeader("Message-ID")[0];
-                    if (messageIdList != null && messageIdList.contains(messageId)) {//瀛樺湪灏变笉缁勮
-                        continue;
-                    }
 
-                    mail.setSenderTime(sdf.format(senderTime));//鍙戜欢鏃堕棿
-                    mail.setReceivingTime(sdf.format(deliveryTime));//鏀朵欢鏃堕棿
-                    mail.setMessageId(messageId);//鑾峰彇閭欢鍞竴ID
-                    mail.setMailType(1);//鏀朵欢
-                    if (m.isSet(Flags.Flag.SEEN)) {//閭欢宸叉爣璁颁负宸茶
-                        mail.setReadFlag(1);//宸茶
-                    }
-                    if (m.isExpunged()) {//妫�鏌ヤ竴涓秷鎭槸鍚﹀凡琚垹闄ゃ�傗��
-                        mail.setDeleteFlag(1);//宸插垹闄�
-                    }
-                    if (m.isSet(Flags.Flag.ANSWERED)) {//閭欢鏄惁宸插洖澶�
-
-                    }
-                    if (m.isSet(Flags.Flag.DRAFT)) {//鏄惁鏄崏绋跨
-                        mail.setMailType(0);//鏄崏绋跨
-                    }
-                    mail.setUserCode(foundation.getUserCode());
-                    mail.setUserName(foundation.getUserName());
-                    mail.setCompanyId(foundation.getCompanyId());
-                    mail.setCompanyName(foundation.getCompanyName());
-                    mail.setClassType(1);
-                    mail.setSubject(m.getSubject());//鏍囬
-                    Date date = m.getReceivedDate();//鏃堕棿
-                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-                    mail.setReceiveTime(formatter.format(date));
-                    String result = "";
-                    StringBuilder plainText = new StringBuilder();//绾枃鏈�
-                    if (m.isMimeType("text/plain")) {
-                        plainText.append(m.getContent());
-                    } else if (m.isMimeType("text/html")) {//html鏍煎紡
-                        result = m.getContent().toString();
-                    } else if (m.isMimeType("multipart/*")) {
-                        MailFileEntity mailFile = new MailFileEntity();//闄勪欢
-                        List<MailFileEntity.MailBodyPart> part = new ArrayList<>();
-                        String unId = UUID.randomUUID().toString().toUpperCase();
-                        StringBuilder attachment = new StringBuilder();
-                        result = getTextFromMimeMultipart((MimeMultipart) m.getContent(), plainText, part, unId, attachment, foundation.getDbId());
-                        mailFile.setPart(part);
-                        mailFile.setUnId(unId);//鐢熸垚uuid
-                        mail.setMailFile(mailFile);//闄勪欢娣诲姞鍒伴噷闈�
-                        if (StringUtils.isNotBlank(attachment)) {//闄勪欢娓告爣淇濆瓨
-                            mail.setAttachFlag(1);
-                            mail.setAttachmentList(unId + attachment.toString());
-                        }
-                    } else {
-                        result = m.getContent().toString();
-                    }
-                    mail.setContent(result);//淇濆瓨鍐呭
-                    mail.setPlainText(plainText.toString().trim());//鏄庢枃
-                    String from = MimeUtility.decodeText(m.getFrom()[0].toString());
-                    InternetAddress internetAddress = new InternetAddress(from);
-                    mail.setSenderName(internetAddress.getPersonal());//鍙戜欢浜哄悕绉�
-                    mail.setSender(internetAddress.getAddress());//鍙戜欢浜�
-                    from = MimeUtility.decodeText(m.getRecipients(Message.RecipientType.TO)[0].toString());
-                    internetAddress = new InternetAddress(from);
-//                mail.setReceiver(internetAddress.getAddress());//鏀朵欢浜�
-                    List<String> receivers = new ArrayList<>();//鎶勯�佷汉
-                    receivers.add(email.getReceiveEmail());
-                    mail.setReceiver(receivers);//缁熶竴鎴愪竴涓敹浠朵汉
-
-                    Address[] ccAddress = m.getRecipients(Message.RecipientType.CC);
-                    if (ccAddress != null && ccAddress.length > 0) {
-                        List<String> cc = new ArrayList<>();//鎶勯�佷汉
-                        for (Address c : ccAddress) {
-                            from = MimeUtility.decodeText(c.toString());
-                            internetAddress = new InternetAddress(from);
-                            cc.add(internetAddress.getAddress());
-                        }
-                        mail.setCc(cc);
-                    }
-                    Address[] bccAddress = m.getRecipients(Message.RecipientType.BCC);
-                    if (bccAddress != null && bccAddress.length > 0) {
-                        List<String> bcc = new ArrayList<>();//瀵嗛��
-                        for (Address c : bccAddress) {
-                            from = MimeUtility.decodeText(c.toString());
-                            internetAddress = new InternetAddress(from);
-                            bcc.add(internetAddress.getAddress());
-                        }
-                        mail.setBcc(bcc);//瀵嗛�佷汉
-                    }
-                    t482101HEntityList.add(mail);
+                t482101HEntity mail = new t482101HEntity();
+                mail.setSenderTime(sdf.format(senderTime));//鍙戜欢鏃堕棿
+                mail.setReceivingTime(sdf.format(deliveryTime));//鏀朵欢鏃堕棿
+                mail.setMessageId(messageId);//鑾峰彇閭欢鍞竴ID
+                mail.setMailType(1);//鏀朵欢
+                if (m.isSet(Flags.Flag.SEEN)) {//閭欢宸叉爣璁颁负宸茶
+                    mail.setReadFlag(1);//宸茶
                 }
+                if (m.isExpunged()) {//妫�鏌ヤ竴涓秷鎭槸鍚﹀凡琚垹闄ゃ�傗��
+                    mail.setDeleteFlag(1);//宸插垹闄�
+                }
+                if (m.isSet(Flags.Flag.ANSWERED)) {//閭欢鏄惁宸插洖澶�
+
+                }
+                if (m.isSet(Flags.Flag.DRAFT)) {//鏄惁鏄崏绋跨
+                    mail.setMailType(0);//鏄崏绋跨
+                }
+                mail.setUserCode(foundation.getUserCode());
+                mail.setUserName(foundation.getUserName());
+                mail.setCompanyId(foundation.getCompanyId());
+                mail.setCompanyName(foundation.getCompanyName());
+                mail.setClassType(1);
+                mail.setSubject(m.getSubject());//鏍囬
+                Date date = m.getReceivedDate();//鏃堕棿
+                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                mail.setReceiveTime(formatter.format(date));
+                String result = "";
+                StringBuilder plainText = new StringBuilder();//绾枃鏈�
+                if (m.isMimeType("text/plain")) {
+                    plainText.append(m.getContent());
+                } else if (m.isMimeType("text/html")) {//html鏍煎紡
+                    result = m.getContent().toString();
+                } else if (m.isMimeType("multipart/*")) {
+                    MailFileEntity mailFile = new MailFileEntity();//闄勪欢
+                    List<MailFileEntity.MailBodyPart> part = new ArrayList<>();
+                    String unId = UUID.randomUUID().toString().toUpperCase();
+                    StringBuilder attachment = new StringBuilder();
+                    result = getTextFromMimeMultipart((MimeMultipart) m.getContent(), plainText, part, unId, attachment, foundation.getDbId());
+                    mailFile.setPart(part);
+                    mailFile.setUnId(unId);//鐢熸垚uuid
+                    mail.setMailFile(mailFile);//闄勪欢娣诲姞鍒伴噷闈�
+                    if (StringUtils.isNotBlank(attachment)) {//闄勪欢娓告爣淇濆瓨
+                        mail.setAttachFlag(1);
+                        mail.setAttachmentList(unId + attachment.toString());
+                    }
+                } else {
+                    result = m.getContent().toString();
+                }
+                mail.setContent(result);//淇濆瓨鍐呭
+                mail.setPlainText(plainText.toString().trim());//鏄庢枃
+                String from = MimeUtility.decodeText(m.getFrom()[0].toString());
+                InternetAddress internetAddress = new InternetAddress(from);
+                mail.setSenderName(internetAddress.getPersonal());//鍙戜欢浜哄悕绉�
+                mail.setSender(internetAddress.getAddress());//鍙戜欢浜�
+                from = MimeUtility.decodeText(m.getRecipients(Message.RecipientType.TO)[0].toString());
+                internetAddress = new InternetAddress(from);
+//                mail.setReceiver(internetAddress.getAddress());//鏀朵欢浜�
+                List<String> receivers = new ArrayList<>();//鎶勯�佷汉
+                receivers.add(email.getReceiveEmail());
+                mail.setReceiver(receivers);//缁熶竴鎴愪竴涓敹浠朵汉
+
+                Address[] ccAddress = m.getRecipients(Message.RecipientType.CC);
+                if (ccAddress != null && ccAddress.length > 0) {
+                    List<String> cc = new ArrayList<>();//鎶勯�佷汉
+                    for (Address c : ccAddress) {
+                        from = MimeUtility.decodeText(c.toString());
+                        internetAddress = new InternetAddress(from);
+                        cc.add(internetAddress.getAddress());
+                    }
+                    mail.setCc(cc);
+                }
+                Address[] bccAddress = m.getRecipients(Message.RecipientType.BCC);
+                if (bccAddress != null && bccAddress.length > 0) {
+                    List<String> bcc = new ArrayList<>();//瀵嗛��
+                    for (Address c : bccAddress) {
+                        from = MimeUtility.decodeText(c.toString());
+                        internetAddress = new InternetAddress(from);
+                        bcc.add(internetAddress.getAddress());
+                    }
+                    mail.setBcc(bcc);//瀵嗛�佷汉
+                }
+                t482101HEntityList.add(mail);
             }
             if (t482101HEntityList.size() > 0) {
                 String docCodeList = mailIfc.saveReceivingMailList(t482101HEntityList);//淇濆瓨
-                mailAccountIfc.updateEmailTime(email.getAccountId());//鏇存柊涓�娆¢厤缃�
-                //鍙戦�侀�氱煡(鏋佸厜鍜寃ebscoket)
-                //鍙栫敤鎴风殑鎵嬫満鍙�
-                BaseService baseService = (BaseService) FactoryBean.getBean("BaseService");
-                String tel = baseService.getJdbcTemplate().queryForObject("select tel  from    _sys_LoginUser where usercode=" + GridUtils.prossSqlParm(foundation.getUserCode()), String.class);
-                MailPush.pushEmailInfo(t482101HEntityList, docCodeList, foundation.getDbId(), tel);
+//                mailAccountIfc.updateEmailTime(email.getAccountId());//鏇存柊涓�娆¢厤缃�
+
+                if(hasPush) {//鏄惁鎺ㄩ�佹秷鎭�
+                    //鍙戦�侀�氱煡(鏋佸厜鍜寃ebscoket)
+                    //鍙栫敤鎴风殑鎵嬫満鍙�
+                    BaseService baseService = (BaseService) FactoryBean.getBean("BaseService");
+                    String tel = baseService.getJdbcTemplate().queryForObject("select tel  from    _sys_LoginUser where usercode=" + GridUtils.prossSqlParm(foundation.getUserCode()), String.class);
+                    MailPush.pushEmailInfo(t482101HEntityList, docCodeList, foundation.getDbId(), tel);
+                }
             }
         } catch (Exception e) {
             throw e;
diff --git a/src/com/yc/crm/schedule/EmailSchedule.java b/src/com/yc/crm/schedule/EmailSchedule.java
index feee214..690b6de 100644
--- a/src/com/yc/crm/schedule/EmailSchedule.java
+++ b/src/com/yc/crm/schedule/EmailSchedule.java
@@ -1,6 +1,5 @@
 package com.yc.crm.schedule;
 
-import com.esotericsoftware.minlog.Log;
 import com.sun.mail.imap.IMAPFolder;
 import com.sun.mail.imap.IMAPStore;
 import com.yc.api.schedule.ScheduleUtils;
@@ -151,7 +150,7 @@
                         //TODO 澶勭悊鏂伴偖浠�
                         try {
                             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());
-                            mailServiceIfc.setMailContent(e.getMessages(), emailEntity, foundation);
+                            mailServiceIfc.setMailContent(e.getMessages(), emailEntity, foundation,true);
                         }catch (Exception e1){
                             e1.printStackTrace();
                         }finally {

--
Gitblit v1.8.0