use of javax.mail.search.AndTerm in project jodd by oblac.
the class EmailFilter method and.
/**
* Defines AND group of filters.
*/
public EmailFilter and(EmailFilter... emailFilters) {
SearchTerm[] searchTerms = new SearchTerm[emailFilters.length];
for (int i = 0; i < emailFilters.length; i++) {
searchTerms[i] = emailFilters[i].searchTerm;
}
concat(new AndTerm(searchTerms));
return this;
}
use of javax.mail.search.AndTerm 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.AndTerm 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) {
}
}
}
use of javax.mail.search.AndTerm in project javautils by jiadongpo.
the class CBCMailFetch method fetchMail2.
/**
* 处理email
*
* @throws Exception
*/
public void fetchMail2() throws Exception {
System.out.println("===================fetchMail2===================");
Properties props = new Properties();
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.disabletop", "true");
props.setProperty("mail.pop3.ssl.enable", "true");
props.setProperty("mail.pop3.useStartTLS", "true");
props.setProperty("mail.pop3.host", "pop3.yeepay.com");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props);
Folder inbox = null;
Store store = session.getStore("pop3");
try {
store.connect("pop3.yeepay.com", receiveEmailAddress, receiveEmailPassword);
Folder folder = store.getDefaultFolder();
if (folder == null) {
throw new MessagingException("读取邮箱失败。");
}
inbox = folder.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
System.out.println("连接成功,开始读取邮箱信息.");
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) {
}
}
}
use of javax.mail.search.AndTerm in project NoraUi by NoraUi.
the class MailSteps method validActivationEmail.
/**
* Valid activation email.
*
* @param mailHost
* example: imap.gmail.com
* @param mailUser
* login of mail box
* @param mailPassword
* password of mail box
* @param firstCssQuery
* the first matching element
* @param conditions
* list of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
* @throws TechnicalException
* is throws if you have a technical error (format, configuration, datas, ...) in NoraUi.
* Exception with message and with screenshot and with exception if functional error but no screenshot and no exception if technical error.
* @throws FailureException
* if the scenario encounters a functional error
*/
@RetryOnFailure(attempts = 3, delay = 60)
@Conditioned
@Et("Je valide le mail d'activation '(.*)'[\\.|\\?]")
@And("I valid activation email '(.*)'[\\.|\\?]")
public void validActivationEmail(String mailHost, String mailUser, String mailPassword, String senderMail, String subjectMail, String firstCssQuery, List<GherkinStepCondition> conditions) throws FailureException, TechnicalException {
RestTemplate restTemplate = createRestTemplate();
try {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imap");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect(mailHost, mailUser, mailPassword);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
SearchTerm filterA = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
SearchTerm filterB = new FromTerm(new InternetAddress(senderMail));
SearchTerm filterC = new SubjectTerm(subjectMail);
SearchTerm[] filters = { filterA, filterB, filterC };
SearchTerm searchTerm = new AndTerm(filters);
Message[] messages = inbox.search(searchTerm);
for (Message message : messages) {
Document doc = Jsoup.parse(getTextFromMessage(message));
Element link = doc.selectFirst(firstCssQuery);
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(link.attr("href"), HttpMethod.GET, entity, String.class);
if (!response.getStatusCode().equals(HttpStatus.OK)) {
new Result.Failure<>("", Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_MAIL_ACTIVATION), subjectMail), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
}
}
} catch (Exception e) {
new Result.Failure<>("", Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_MAIL_ACTIVATION), subjectMail), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
}
}
Aggregations