Java登录QQ邮箱整理邮件的58的简历(⼀)
Java登录QQ邮箱整理邮件的58的简历
实习期间给公司做的第⼀个⼩⼯具,只需下载jxl.jar和mail.jar的第三⽅那个类库,就可以着⼿敲代码了:
1、此次是使⽤POP3协议登录的邮箱,使⽤了⼿机获取的账号配置码
2、遍历收件箱的所有邮件,判断邮件类型,得到邮件的内容
3、解析每⼀封邮件的内容,parseMessage(Message …messages)
4、到符合主题的邮件,如包含(58)……,getSubject(MimeMessage msg)
5、针对特定邮件解析出相应内容,并创建excel表,⼀条条的存⼊其中
6、所有简历的部分信息先放在Excel表中集合,关闭表
登录邮箱
⽹上普遍可寻到遵循POP3协议及IMAP协议的登录⽅式,下⾯我⽤的是POP3协议,缺陷是不能给邮件做标记,只能读,不可改变邮件的状态及属性,后⾯会有IMAP协议的登录的⽅式的博客进⾏补充…….
另外敲代码前,先去邮箱的设置⾥,进去“账户”的设置,开启POP3协议的开启功能,使⽤⼿机发送信息得到登录码,很⽅便,可避免修改了密码,或是邮箱设置了独⽴密码等层层关卡的⿇烦。写成了⽅法,代码如下
public static void receive() throws Exception {
// 准备连接服务器的会话信息
Properties props = new Properties();
//props.setProperty("mail.store.protocol", "pop3"); // 协议
props.setProperty("mail.pop3.port", "110"); // 端⼝
props.setProperty("mail.pop3.host", "pop.qq"); // pop3服务器
// SSL安全连接参数
props.setProperty("mail.pop3.socketFactory.class", "javax.ssl.SSLSocketFactory");
props.setProperty("mail.pop3.socketFactory.fallback", "true");
props.setProperty("mail.pop3.socketFactory.port", "995");
// 创建Session实例对象
Session session = Instance(props);
Store store = Store("pop3");
// 获得收件箱
Folder folder = Folder("INBOX");
/* Folder.READ_ONLY:只读权限
* Folder.READ_WRITE:可读可写(可以修改邮件的状态)
*/
folder.open(Folder.READ_WRITE); //打开收件箱
// 由于POP3协议⽆法获知邮件的状态,所以getUnreadMessageCount得到的是收件箱的邮件总数
// 获得收件箱中的邮件总数
System.out.println("邮件总数: " + MessageCount());
// 得到收件箱中的所有邮件,并解析
try{
Message[] messages = Messages();
parseMessage(messages);
//释放资源
folder.close(true);
store.close();
}catch(Exception e){
e.printStackTrace();
System.out.println("receive内部异常");}
}
解析邮件
判断主题⾥是否有58,并判断该邮件是否是当天的,整理当天的邮件信息存⼊Excel表中去:
public static void parseMessage(Message ...messages) throws MessagingException, IOException {
if (messages == null || messages.length < 1)
throw new MessagingException("未到要解析的邮件!");
//58简历整理⼯具,使⽤正则表达式匹配相应的信息:姓名,性别,年龄,电话,邮箱,经验
Pattern p1 = Patternpile("<h3.*?>([\\s\\S]*)<span.*?>([\\s\\S]*)</span></h3>");
Pattern p2 = Patternpile("<label.*?>([\\s\\S]*)<span.*?><span.*?>([\\s\\S]*)</span></span></label>");
Pattern p3 = Patternpile("<ul.*?>[\\s]*?<li.*?>([^\n]*)</li>[\\s]*?<li.*?>([^\n]*)</li>[\\s]*?<li.*?>([^\n]*)</li>[\\s]*?<li.*?>([^\n]*)</li>[\\s]*?</ul>");
Pattern p4 = Patternpile("<label.*?>([\\s\\S]*)<span.*?>([\\s\\S]*)</span></label>");
//⽃⽶简历整理⼯具,使⽤正则表达式匹配相应的信息:姓名,性别,年龄,电话,邮箱,经验
//
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd_hh:mm:ss");//可以⽅便地修改⽇期格式
String nowDate = dateFormat.format( now );
System.out.println(nowDate);
Calendar c = Instance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int date = c.get(Calendar.DATE);
try{
//创建及打开Excel表,存储58同城的简历信息
String path="C:\\resume\\58Resume"+year+"."+month+"."+date+".xlsx";
//String path="/Users/LiuDuan/workspace/temp_edm/58Resume"+year+"."+month+"."+date+".xlsx";
//InputStream is = new FileInputStream("C:\\Users\\yang\\Desktop\\ResumeData.xlsx");;
WritableWorkbook wb = ateWorkbook(new File(path));
WritableSheet ws = wb.createSheet("Sheet1", 0);
//创建及打开Excel表,存储58同城的简历信息
int j=0;
int count = messages.length;
// 解析所有邮件
for (int i = 130; i < count; i++) {
MimeMessage msg = (MimeMessage) messages[i];
//解决邮件主题乱码的问题
String subject1 = getSubject(msg); //获得邮件主题
String subject = "";
//前⾯必须判断下是否为null,否则会有异常
if (subject1 ==null || subject1 == "" || "".equals(subject1)
|| "null".equals(subject1)) {
subject = "此邮件没有主题";
continue;
} else {
subject = subject1;
}
//
System.out.println("第"+i+"封邮件主题是: " + subject);
String str=getSentDate(msg, null);
System.out.println("------发送时间:" +str);
//if(subject.indexOf("58")>0 && nowDate.equals(getSentDate(msg, null))){
if(subject.indexOf("58")>0 &&judgeDate(nowDate,str)){
StringBuffer content = new StringBuffer(300);
getMailTextContent(msg, content);
//System.out.println(content);
//String(),i);
//得到每条邮件的三个信息
Matcher m = p1.matcher(content);
Matcher n = p2.matcher(content);
Matcher p = p3.matcher(content);
Matcher q = p4.matcher(content);
StringBuilder sb = new StringBuilder();
while (m.find()) {
sb.up(1)+","+m.group(2));
Label label1 = new Label(0,up(1));
ws.addCell(label1);
Label label2 = new Label(1,up(2).substring(1, 2));
ws.addCell(label2);
Label label3 = new Label(2,up(2).substring(3, 5));
ws.addCell(label3);
sb.append(",");
}
while (n.find()) {
sb.up(2));
Label label4 = new Label(3,up(2));
ws.addCell(label4);
sb.append(",");
}
while (p.find()) {
sb.up(2));
Label label5 = new Label(4,up(2));
ws.addCell(label5);
sb.append(",");
}
while (q.find()) {
//System.out.println("4");
sb.up(2));
Label label6 = new Label(5,up(2));
ws.addCell(label6);
sb.append(",");
}
j++;
System.out.println(sb);
}else{
continue;
}
}
wb.write();
wb.close();
}catch(IOException e){
e.printStackTrace();
System.out.println("parseMessage内部1");
}catch(MessagingException e){
e.printStackTrace();
System.out.println("parseMessage内部2");
}catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获得邮件主题
* @param msg 邮件内容
* @return解码后的邮件主题
*/
public static String getSubject(MimeMessage msg) throws UnsupportedEncodingException, MessagingException { try{
if(msg==null || Subject()==null)
return null;
return MimeUtility.Subject());
}
catch(UnsupportedEncodingException e){
return null;
}catch(MessagingException e){
return null;
}
}
/**
* 获得邮件发送时间
* @param msg 邮件内容
* @return yyyy年mm⽉dd⽇星期X HH:mm
* @throws MessagingException
*/
public static String getSentDate(MimeMessage msg, String pattern) throws MessagingException {
try{
Date receivedDate = SentDate();
if (receivedDate == null)
return"";
if (pattern == null || "".equals(pattern))
/
/pattern = "yyyy年MM⽉dd⽇ E HH:mm ";
pattern = "yyyy.MM.dd_HH:mm:ss";
pattern = "yyyy.MM.dd_HH:mm:ss";
return new SimpleDateFormat(pattern).format(receivedDate);} catch(MessagingException e){System.out.println("获取时间内部异常");return null }
//判断邮箱邮件时间是否在昨天的5点到今天的4点之间
public static boolean judgeDate(String s1,String resume_time_str){
//截取系统的年⽉⽇时间
/* int nowY = Integer.parseInt(s1.substring(0, 4));
int nowM = Integer.parseInt(s1.substring(5, 7));
int nowD = Integer.parseInt(s1.substring(8, 10));
//System.out.println(nowY+"."+nowM+"."+nowD);
// 截取邮件时间的时分秒
int emailY = Integer.parseInt(s2.substring(0, 4));
int emailM = Integer.parseInt(s2.substring(5, 7));
int emailD = Integer.parseInt(s2.substring(8, 10));
int emailh = Integer.parseInt(s2.substring(11, 13));
int emailm = Integer.parseInt(s2.substring(14, 16));
int emails = Integer.parseInt(s2.substring(17, 19));
//System.out.println(emailY+"."+emailM+"."+emailD+"."+emailh+"."+emailm+"."+emails);
if(nowY == emailY && nowM == emailM && nowD == emailD){
if(emailh > 0 && emailh <17){
匹配邮箱的正则表达式return true;}
else if(emailh == 0 && emailm >= 0 && emails >= 0){
return true;}
else if(emailh == 17 && emailm < 1 && emails < 1){
return true;}
else
return false;
}else if(nowY==emailY && nowM==emailM && nowD==emailD+1){
if(emailh >= 17 && emailh <=23){return true;}
//else if(emailh == 0 && emailm > 0 && emails > 0){return true;}
//else if(emailh == 17 && emailm <= 30 && emails <= 30){return true;}
else
return false;
}
return false;
*/
//取得昨天中午到今天中午的时间段范围,2017-05-01 12:00:00 ~ 2017-05-02 11:59:59
Date todydate = new Date();
Date yestoday = new Time() - 24 * 3600 * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf_hms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateBeginStr = sdf.format(yestoday)+" 12:00:00";
String dateEndStr = sdf.format(todydate)+" 11:59:59";
System.out.println("格式化后的⽇期:" + dateBeginStr);
System.out.println("格式化后的⽇期:" + dateEndStr);
SimpleDateFormat resume_sdf = new SimpleDateFormat("yyyy.MM.dd_HH:mm:ss");
try {
long time_start = sdf_hms.parse(dateBeginStr).getTime()/1000;
long time_end = sdf_hms.parse(dateEndStr).getTime()/1000;
long resume_time = resume_sdf.parse(resume_time_str).getTime()/1000;
if(time_start < resume_time && resume_time < time_end) {
return true;
}
//System.out.println("time1 "+ String.valueOf(sdf.parse(dateBeginStr).getTime()/1000));
/
/System.out.println("time1 "+ String.valueOf(sdf.parse(dateEndStr).getTime()/1000));
} catch (Exception e) {
System.out.println("get date error!!");
return false;
}
return false;
}
/**
* 获得邮件⽂本内容
* @param part 邮件体
* @param content 存储邮件⽂本内容的字符串
* @throws MessagingException
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论