Search in sources :

Example 6 with FromStringTerm

use of javax.mail.search.FromStringTerm in project jodd by oblac.

the class EmailFilterTest method testAndOrNot.

@Test
public void testAndOrNot() {
    EmailFilter emailFilter = filter().from("from").to("to").or().not().subject("subject").from("from2");
    SearchTerm expected = new OrTerm(new OrTerm(new AndTerm(new FromStringTerm("from"), new RecipientStringTerm(Message.RecipientType.TO, "to")), new NotTerm(new SubjectTerm("subject"))), new FromStringTerm("from2"));
    assertEquals(expected, emailFilter.searchTerm);
}
Also used : AndTerm(javax.mail.search.AndTerm) OrTerm(javax.mail.search.OrTerm) FromStringTerm(javax.mail.search.FromStringTerm) RecipientStringTerm(javax.mail.search.RecipientStringTerm) NotTerm(javax.mail.search.NotTerm) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) Test(org.junit.Test)

Example 7 with FromStringTerm

use of javax.mail.search.FromStringTerm in project jodd by oblac.

the class EmailFilterTest method testOr2.

@Test
public void testOr2() {
    EmailFilter emailFilter = filter().or(filter().from("from"), filter().to("to"));
    SearchTerm expected = new OrTerm(new FromStringTerm("from"), new RecipientStringTerm(Message.RecipientType.TO, "to"));
    assertEquals(expected, emailFilter.searchTerm);
}
Also used : OrTerm(javax.mail.search.OrTerm) FromStringTerm(javax.mail.search.FromStringTerm) RecipientStringTerm(javax.mail.search.RecipientStringTerm) SearchTerm(javax.mail.search.SearchTerm) Test(org.junit.Test)

Example 8 with FromStringTerm

use of javax.mail.search.FromStringTerm in project camel by apache.

the class SearchTermBuilder method from.

public SearchTermBuilder from(Op op, String pattern) {
    SearchTerm st = new FromStringTerm(pattern);
    addTerm(op, st);
    return this;
}
Also used : FromStringTerm(javax.mail.search.FromStringTerm) SearchTerm(javax.mail.search.SearchTerm)

Example 9 with FromStringTerm

use of javax.mail.search.FromStringTerm in project javautils by jiadongpo.

the class CBCMailFetch method fetchMail.

public void fetchMail() throws Exception {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    Properties props = System.getProperties();
    props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.pop3.socketFactory.fallback", "false");
    props.setProperty("mail.pop3.port", "995");
    props.setProperty("mail.pop3.socketFactory.port", "995");
    Session session = Session.getDefaultInstance(props, null);
    URLName urln = new URLName("pop3", "pop.yeepay.com", 995, null, "dongpo.jia@yeepay.com", "*******");
    Store store = session.getStore(urln);
    Folder inbox = null;
    try {
        store.connect();
        inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        FetchProfile profile = new FetchProfile();
        profile.add(FetchProfile.Item.ENVELOPE);
        // 搜索发件人为”智联招聘“或主题包含”中国建设银行“的邮件
        // SearchTerm subjectTerm = new SubjectTerm("中国建设银行");//收件箱主题过滤
        // SearchTerm addressTerm = new FromTerm(new InternetAddress("295445156@qq.com"));//发件人地址
        SearchTerm andTerm = new AndTerm(new FromStringTerm("min.hu@yeepay.com"), new SubjectTerm("中国建设银行"));
        Message[] messages = inbox.search(andTerm);
        // Message[] messages = inbox.getMessages();
        inbox.fetch(messages, profile);
        System.out.println("收件箱的邮件数:" + messages.length);
        CBCMailFetch pmm = null;
        for (int i = 0; i < messages.length; i++) {
            // 邮件发送者、邮件标题、邮件大小、邮件发送时间
            String from = decodeText(messages[i].getFrom()[0].toString());
            InternetAddress ia = new InternetAddress(from);
            System.out.println("邮件信息,发送者:[" + ia.getPersonal() + "],发件人邮箱地址:[" + (ia.getAddress()) + "],标题:[" + messages[i].getSubject() + "],邮件大小:[" + messages[i].getSize() + "],发送时间:[" + messages[i].getSentDate() + "]");
            pmm = new CBCMailFetch((MimeMessage) messages[i]);
            Message message = messages[i];
            Multipart multipart = (Multipart) message.getContent();
            for (int j = 0, n = multipart.getCount(); j < n; j++) {
                Part part = multipart.getBodyPart(j);
                String disposition = part.getDisposition();
                if (disposition != null && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) {
                    String attachmentName = part.getFileName();
                    System.out.println(attachmentName);
                    // SHOP.105584045110069.20161028.zip
                    String[] attachmentNames = attachmentName.split("\\.");
                    System.out.println("第一个元素:" + attachmentNames[0] + "第二个元素:" + attachmentNames[1] + "第三个元素:" + attachmentNames[2] + "第四个元素:" + attachmentNames[3]);
                    if (attachmentNames[1] == null || attachmentNames[2] == null) {
                        throw new Exception("附件结构不符合,记录");
                    }
                    // 格式化日期
                    DateFormat format = new SimpleDateFormat("yyyyMMdd");
                    Date date = null;
                    String str = null;
                    try {
                        // Thu Jan 18 00:00:00 CST 2007
                        date = format.parse(attachmentNames[2]);
                    } catch (ParseException e) {
                        System.out.println("日期格式化错误:" + e);
                        e.printStackTrace();
                    }
                    // 昨天到当前(昨天的0点,到当前时间点)的数据或指定的数据
                    boolean configFlag = getSpecifiedDate().contains(attachmentNames[2]);
                    boolean yesterday = date.after(lastDayWholePointDate(new Date())) && date.before(new Date());
                    // TODO 临时去掉,为了方便测试
                    // if (configFlag || yesterday) {
                    // 105110054111509为建行豹子8011   //105584045110069建行斑马8010
                    File file = pmm.getOutFile(attachmentName);
                    if (attachmentNames[1].equals("105110054111509") || attachmentNames[1].equals("105584045110069")) {
                        pmm.saveFile(file, part.getInputStream());
                        // 解压
                        String fileFullName = file.getAbsolutePath();
                        String fileFullDirTmp = file.getParent() + "/tmp_" + attachmentName;
                        fileFullDirTmp = fileFullDirTmp.substring(0, fileFullDirTmp.length() - 4);
                        unzip(fileFullName, fileFullDirTmp);
                        // 获取解压后的文件全路径
                        List<File> files = Const.searchFile(new File(fileFullDirTmp), ".det.");
                        for (File onefile : files) {
                            // 发送到指定ftp,后删除生成的目录
                            SFTPPUT sftpput = new SFTPPUT();
                            String ftpRemote = "/apps/tomcat7-40-tomcat-air-ticket-merchant/logs" + "/" + onefile.getName();
                            System.out.println("文件路径[" + onefile.getAbsolutePath() + "],上传至ftp路径[" + ftpRemote + "]");
                            sftpput.put(onefile.getAbsolutePath(), ftpRemote);
                            // 删除生成的目录
                            Const.delFolder(onefile.getParent());
                        }
                    }
                // }
                }
            // pmm.setAttachPath(".");
            // pmm.saveAttachMent((Part) messages[i]);
            }
        }
    } finally {
        try {
            inbox.close(false);
        } catch (Exception e) {
        }
        try {
            store.close();
        } catch (Exception e) {
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) AndTerm(javax.mail.search.AndTerm) MimeMessage(javax.mail.internet.MimeMessage) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) ZipException(java.util.zip.ZipException) URISyntaxException(java.net.URISyntaxException) FileSystemException(org.apache.commons.vfs2.FileSystemException) ParseException(java.text.ParseException) FromStringTerm(javax.mail.search.FromStringTerm) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) ZipFile(org.apache.tools.zip.ZipFile)

Example 10 with FromStringTerm

use of javax.mail.search.FromStringTerm in project javautils by jiadongpo.

the class CBCMailFetch method fetchMail.

/**
 * 处理email
 *
 * @throws Exception
 */
public void fetchMail() throws Exception {
    System.out.println("===================fetchMail===================");
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    Properties props = System.getProperties();
    props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.pop3.socketFactory.fallback", "false");
    props.setProperty("mail.pop3.port", "995");
    props.setProperty("mail.pop3.socketFactory.port", "995");
    Session session = Session.getDefaultInstance(props, null);
    URLName urln = new URLName("pop3", "pop3.yeepay.com", 995, null, receiveEmailAddress, receiveEmailPassword);
    Store store = session.getStore(urln);
    Folder inbox = null;
    try {
        store.connect();
        inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        FetchProfile profile = new FetchProfile();
        profile.add(FetchProfile.Item.ENVELOPE);
        // 搜索发件人为”智联招聘“或主题包含”中国建设银行“的邮件
        // SearchTerm subjectTerm = new SubjectTerm("中国建设银行");//收件箱主题过滤
        // SearchTerm addressTerm = new FromTerm(new InternetAddress("295445156@qq.com"));//发件人地址
        SearchTerm andTerm = new AndTerm(new FromStringTerm(fromMailAddress), new SubjectTerm("中国建设银行"));
        Message[] messages = inbox.search(andTerm);
        // Message[] messages = inbox.getMessages();
        inbox.fetch(messages, profile);
        System.out.println("收件箱的邮件数:" + messages.length);
        CBCMailFetch pmm = null;
        for (int i = 0; i < messages.length; i++) {
            // 邮件发送者、邮件标题、邮件大小、邮件发送时间
            String from = Const.decodeText(messages[i].getFrom()[0].toString());
            InternetAddress ia = new InternetAddress(from);
            System.out.println("邮件信息,发送者:[" + ia.getPersonal() + "],发件人邮箱地址:[" + (ia.getAddress()) + "],标题:[" + messages[i].getSubject() + "],邮件大小:[" + messages[i].getSize() + "],发送时间:[" + messages[i].getSentDate() + "]");
            pmm = new CBCMailFetch((MimeMessage) messages[i]);
            Message message = messages[i];
            Multipart multipart = (Multipart) message.getContent();
            for (int j = 0, n = multipart.getCount(); j < n; j++) {
                Part part = multipart.getBodyPart(j);
                String disposition = part.getDisposition();
                if (disposition != null && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) {
                    String attachmentName = part.getFileName();
                    System.out.println(attachmentName);
                    // SHOP.105584045110069.20161028.zip
                    String[] attachmentNames = attachmentName.split("\\.");
                    if (attachmentNames.length != 4) {
                        System.out.println(Arrays.toString(attachmentNames));
                        // 结构不对
                        continue;
                    }
                    System.out.println("第一个元素:" + attachmentNames[0] + "第二个元素:" + attachmentNames[1] + "第三个元素:" + attachmentNames[2] + "第四个元素:" + attachmentNames[3]);
                    // 格式化日期
                    DateFormat format = new SimpleDateFormat("yyyyMMdd");
                    Date date = null;
                    String str = null;
                    try {
                        // Thu Jan 18 00:00:00 CST 2007
                        date = format.parse(attachmentNames[2]);
                    } catch (ParseException e) {
                        System.out.println("日期格式化错误:" + e);
                        e.printStackTrace();
                    }
                    // 昨天到当前(昨天的0点,到当前时间点)的数据或指定的数据
                    boolean configFlag = getSpecifiedDate().contains(attachmentNames[2]);
                    boolean yesterday = date.after(Const.lastDayWholePointDate(new Date())) && date.before(new Date());
                    // TODO 临时去掉,为了方便测试
                    // if (configFlag || yesterday) {
                    // 105110054111509为建行豹子8011   //105584045110069建行斑马8010
                    File file = pmm.getOutFile(attachmentName);
                    if (file.exists()) {
                        // 已存在的文件跳过
                        continue;
                    }
                    if (attachmentNames[1].equals("105110054111509") || attachmentNames[1].equals("105584045110069")) {
                        pmm.saveFile(file, part.getInputStream());
                        // 解压
                        String fileFullName = file.getAbsolutePath();
                        String fileFullDirTmp = file.getParent() + "/tmp_" + attachmentName;
                        fileFullDirTmp = fileFullDirTmp.substring(0, fileFullDirTmp.length() - 4);
                        unzip(fileFullName, fileFullDirTmp);
                        // 获取解压后的文件全路径
                        List<File> files = Const.searchFile(new File(fileFullDirTmp), ".det.");
                        for (File onefile : files) {
                            // 发送到指定ftp,后删除生成的目录
                            SFTPPUT sftpput = new SFTPPUT();
                            String ftpRemote = "/apps/tomcat7-40-tomcat-air-ticket-merchant/logs" + "/" + onefile.getName();
                            System.out.println("文件路径[" + onefile.getAbsolutePath() + "],上传至ftp路径[" + ftpRemote + "]");
                            sftpput.put(onefile.getAbsolutePath(), ftpRemote);
                            // 删除生成的目录
                            Const.delFolder(onefile.getParent());
                        }
                    }
                // }
                }
            }
        }
    } finally {
        try {
            inbox.close(false);
        } catch (Exception e) {
        }
        try {
            store.close();
        } catch (Exception e) {
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) AndTerm(javax.mail.search.AndTerm) MimeMessage(javax.mail.internet.MimeMessage) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) FromStringTerm(javax.mail.search.FromStringTerm) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

FromStringTerm (javax.mail.search.FromStringTerm)12 SearchTerm (javax.mail.search.SearchTerm)12 AndTerm (javax.mail.search.AndTerm)7 SubjectTerm (javax.mail.search.SubjectTerm)6 Test (org.junit.Test)6 RecipientStringTerm (javax.mail.search.RecipientStringTerm)5 OrTerm (javax.mail.search.OrTerm)4 URISyntaxException (java.net.URISyntaxException)3 DateFormat (java.text.DateFormat)3 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 InternetAddress (javax.mail.internet.InternetAddress)3 MimeMessage (javax.mail.internet.MimeMessage)3 NotTerm (javax.mail.search.NotTerm)2 LocalDateTime (java.time.LocalDateTime)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1