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);
}
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);
}
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;
}
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) {
}
}
}
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) {
}
}
}
Aggregations