| | |
| | | |
| | | 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.*; |
| | |
| | | @RestController |
| | | @RequestMapping("/crm/mail/account") |
| | | public class MailAccount { |
| | | ThreadPoolTaskExecutor threadPoolExecutor = (ThreadPoolTaskExecutor) FactoryBean.getBean("threadPoolExecutor");//线程池 |
| | | |
| | | @Autowired |
| | | MailAccountIfc mailAccountIfc; |
| | | @Autowired |
| | | MailIfc mailIfc; |
| | | |
| | | @Autowired |
| | | MailServiceIfc mailServiceIfc; |
| | | /** |
| | | * 添加邮箱配置信息 |
| | | * |
| | |
| | | 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); |
| | |
| | | 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", "异常"); |
| | |
| | | return msg; |
| | | } |
| | | |
| | | private class ReceivingEmailsSave implements Runnable { |
| | | public static class ReceivingEmailsSave implements Runnable { |
| | | final MailServiceIfc mailServiceIfc; |
| | | final T482102Entity t482102; |
| | | final FoundationEntity foundation; |
| | |
| | | private boolean smtpSSL = true;//SMTP SSL是一种基于SMTP协议的加密传输方式,通过SSL(Secure 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;//当前版本号 |
| | |
| | | * |
| | | * @param account |
| | | */ |
| | | abstract void addEmailAccount(T482102Entity account); |
| | | abstract T482102Entity addEmailAccount(T482102Entity account); |
| | | |
| | | /** |
| | | * 修改邮箱配置 |
| | |
| | | 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" + |
| | |
| | | "'" + 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; |
| | | } |
| | |
| | | "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); |
| | |
| | | "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)); |
| | |
| | | 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); |
| | |
| | | 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表示已读 |
| | |
| | | * @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; |
| | | } |
| | |
| | | |
| | | 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; |
| | |
| | | // 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) { |
| | |
| | | * @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) { |
| | | continue; |
| | | } |
| | | 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; |
| | | } |
| | | Date deliveryTime = m.getReceivedDate();//收件时间 |
| | | Date senderTime = m.getSentDate();//发件时间 |
| | | if (senderTime == null) { |
| | | senderTime = senderTime; |
| | | } |
| | | |
| | | t482101HEntity mail = new t482101HEntity(); |
| | | mail.setSenderTime(sdf.format(senderTime));//发件时间 |
| | | mail.setReceivingTime(sdf.format(deliveryTime));//收件时间 |
| | | mail.setMessageId(messageId);//获取邮件唯一ID |
| | |
| | | } |
| | | t482101HEntityList.add(mail); |
| | | } |
| | | } |
| | | if (t482101HEntityList.size() > 0) { |
| | | String docCodeList = mailIfc.saveReceivingMailList(t482101HEntityList);//保存 |
| | | mailAccountIfc.updateEmailTime(email.getAccountId());//更新一次配置 |
| | | // mailAccountIfc.updateEmailTime(email.getAccountId());//更新一次配置 |
| | | |
| | | if(hasPush) {//是否推送消息 |
| | | //发送通知(极光和webscoket) |
| | | //取用户的手机号 |
| | | 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; |
| | | } |
| | |
| | | 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; |
| | |
| | | //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 { |