package com;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.mail.search.AndTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.OrTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;
public class ReciveMail {
private MimeMessage msg = null;
private String saveAttchPath = "";
private StringBuffer bodytext = new StringBuffer();
private String dateformate = "yy-MM-dd HH:mm";
private String senders = "jiang-anlin@foxmail.com";
private String subjectKeyWords = "test2";
private String contentKeyWords = "";
// public ReciveMail(MimeMessage msg){
// this.msg = msg;
// }
public ReciveMail () {
}
public void setMsg(MimeMessage msg) {
this.msg = msg;
}
/**
* 获取发送邮件者信息
* @return
* @throws MessagingException
*/
public String getFrom() throws MessagingException{
InternetAddress[] address = (InternetAddress[]) msg.getFrom();
String from = address[0].getAddress();
if(from == null){
from = "";
}
String personal = address[0].getPersonal();
if(personal == null){
personal = "";
}
String fromaddr = personal +"<"+from+">";
return fromaddr;
}
/**
* 获取邮件收件人,抄送,密送的地址和信息。
* 根据所传递的参数不同 "to"-->收件人,"cc"-->抄送人地址,"bcc"-->密送地址
* @param type
* @return
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
public String getMailAddress(String type)
throws MessagingException, UnsupportedEncodingException{
String mailaddr = "";
String addrType = type.toUpperCase();
InternetAddress[] address = null;
if(addrType.equals("TO")||addrType.equals("CC")||addrType.equals("BCC")){
if(addrType.equals("TO")){
//收件人
address = (InternetAddress[]) msg.getRecipients(Message.RecipientType.TO);
}
if(addrType.equals("CC")){
//抄送人地址
address = (InternetAddress[]) msg.getRecipients(Message.RecipientType.CC);
}
if(addrType.equals("BCC")){
//密送地址
address = (InternetAddress[]) msg.getRecipients(Message.RecipientType.BCC);
}
if(address != null){
for(int i=0;i<address.length;i++){
String mail = address[i].getAddress();
if(mail == null){
mail = "";
}else{
mail = MimeUtility.decodeText(mail);
}
String personal = address[i].getPersonal();
if(personal == null){
personal = "";
}else{
personal = MimeUtility.decodeText(personal);
}
String compositeto = personal +"<"+mail+">";
mailaddr += ","+compositeto;
}
mailaddr = mailaddr.substring(1);
}
}else{
throw new RuntimeException("Error email Type!");
}
return mailaddr;
}
/**
* 获取邮件主题
* @return
* @throws UnsupportedEncodingException
* @throws MessagingException
*/
public String getSubject()
throws UnsupportedEncodingException, MessagingException{
String subject = "";
subject = MimeUtility.decodeText(msg.getSubject());
if(subject == null){
subject = "";
}
return subject;
}
/**
* 获取邮件发送日期
* @return
* @throws MessagingException
*/
public String getSendDate() throws MessagingException{
Date sendDate = msg.getSentDate();
SimpleDateFormat smd = new SimpleDateFormat(dateformate);
return smd.format(sendDate);
}
/**
* 获取邮件正文内容
* @return
*/
public String getBodyText(){
return bodytext.toString();
}
/**
* 解析邮件,将得到的邮件内容保存到一个stringBuffer对象中,
* 解析邮件 主要根据MimeType的不同执行不同的操作,一步一步的解析
* @param part
* @throws MessagingException
* @throws IOException
*/
public void getMailContent(Part part)
throws MessagingException, IOException{
String contentType = part.getContentType();
int nameindex = contentType.indexOf("name");
boolean conname = false;
if(nameindex != -1){
conname = true;
}
System.out.println("CONTENTTYPE:"+contentType);
if(part.isMimeType("text/plain")&&!conname){
bodytext.append((String)part.getContent());
}else if(part.isMimeType("text/html")&&!conname){
bodytext.append((String)part.getContent());
}else if(part.isMimeType("multipart/*")){
Multipart multipart = (Multipart) part.getContent();
int count = multipart.getCount();
for(int i=0;i<count;i++){
getMailContent(multipart.getBodyPart(i));
}
}else if(part.isMimeType("message/rfc822")){
getMailContent((Part) part.getContent());
}
}
/**
* 判断邮件是否需要回执,如需回执返回true,否则返回false
* @return
* @throws MessagingException
*/
public boolean getReplySign() throws MessagingException{
boolean replySign = false;
String needreply[] = msg.getHeader("Disposition-Notification-TO");
if(needreply != null){
replySign = true;
}
return replySign;
}
/**
* 获取此邮件的message-id
* @return
* @throws MessagingException
*/
public String getMessageId() throws MessagingException{
return msg.getMessageID();
}
/**
* 判断此邮件是否已读,如果未读则返回false,已读返回true
* @return
* @throws MessagingException
*/
public boolean isNew() throws MessagingException{
boolean isnew = false;
Flags flags = ((Message)msg).getFlags();
Flags.Flag[] flag = flags.getSystemFlags();
System.out.println("flags's length:"+flag.length);
for(int i=0;i<flag.length;i++){
if(flag[i]==Flags.Flag.SEEN){
isnew = true;
System.out.println("seen message .......");
break;
}
}
return isnew;
}
/**
* 判断是是否包含附件
* @param part
* @return
* @throws MessagingException
* @throws IOException
*/
public boolean isContainAttch(Part part) throws MessagingException, IOException{
boolean flag = false;
String contentType = part.getContentType();
if(part.isMimeType("multipart/*")){
Multipart multipart = (Multipart) part.getContent();
int count = multipart.getCount();
for(int i=0;i<count;i++){
BodyPart bodypart = multipart.getBodyPart(i);
String dispostion = bodypart.getDisposition();
if((dispostion != null)&&(dispostion.equals(Part.ATTACHMENT)||dispostion.equals(Part.INLINE))){
flag = true;
}else if(bodypart.isMimeType("multipart/*")){
flag = isContainAttch(bodypart);
}else{
String conType = bodypart.getContentType();
if(conType.toLowerCase().indexOf("appliaction")!=-1){
flag = true;
}
if(conType.toLowerCase().indexOf("name")!=-1){
flag = true;
}
}
}
}else if(part.isMimeType("message/rfc822")){
flag = isContainAttch((Part) part.getContent());
}
return flag;
}
/**
* 保存附件
* @param part
* @throws MessagingException
* @throws IOException
*/
public void saveAttchMent(Part part) throws MessagingException, IOException{
String filename = "";
if(part.isMimeType("multipart/*")){
Multipart mp = (Multipart) part.getContent();
for(int i=0;i<mp.getCount();i++){
BodyPart mpart = mp.getBodyPart(i);
String dispostion = mpart.getDisposition();
if((dispostion != null)&&(dispostion.equals(Part.ATTACHMENT)||dispostion.equals(Part.INLINE))){
filename = mpart.getFileName();
if(filename.toLowerCase().indexOf("gb2312")!=-1){
filename = MimeUtility.decodeText(filename);
}
saveFile(filename,mpart.getInputStream());
}else if(mpart.isMimeType("multipart/*")){
saveAttchMent(mpart);
}else{
filename = mpart.getFileName();
if(filename != null&&(filename.toLowerCase().indexOf("gb2312")!=-1)){
filename = MimeUtility.decodeText(filename);
}
saveFile(filename,mpart.getInputStream());
}
}
}else if(part.isMimeType("message/rfc822")){
saveAttchMent((Part) part.getContent());
}
}
/**
* 获得保存附件的地址
* @return
*/
public String getSaveAttchPath() {
return saveAttchPath;
}
/**
* 设置保存附件地址
* @param saveAttchPath
*/
public void setSaveAttchPath(String saveAttchPath) {
this.saveAttchPath = saveAttchPath;
}
/**
* 设置日期格式
* @param dateformate
*/
public void setDateformate(String dateformate) {
this.dateformate = dateformate;
}
/**
* 保存文件内容
* @param filename
* @param inputStream
* @throws IOException
*/
private void saveFile(String filename, InputStream inputStream) throws IOException {
String osname = System.getProperty("os.name");
String storedir = getSaveAttchPath();
String sepatror = "";
if(osname == null){
osname = "";
}
if(osname.toLowerCase().indexOf("win")!=-1){
sepatror = "//";
if(storedir==null||"".equals(storedir)){
storedir = "d://temp";
}
}else{
sepatror = "/";
storedir = "/temp";
}
File storefile = new File(storedir+sepatror+filename);
System.out.println("storefile's path:"+storefile.toString());
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(storefile));
bis = new BufferedInputStream(inputStream);
int c;
while((c= bis.read())!=-1){
bos.write(c);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
bos.close();
bis.close();
}
}
public void recive(Part part,int i) throws MessagingException, IOException{
System.out.println("------------------START-----------------------");
System.out.println("Message"+i+" subject:" + getSubject());
System.out.println("Message"+i+" from:" + getFrom());
System.out.println("Message"+i+" isNew:" + isNew());
boolean flag = isContainAttch(part);
System.out.println("Message"+i+" isContainAttch:" +flag);
System.out.println("Message"+i+" replySign:" + getReplySign());
getMailContent(part);
System.out.println("Message"+i+" content:" + getBodyText());
// setSaveAttchPath("c://temp//"+i);
// if(flag){
// saveAttchMent(part);
// }
System.out.println("------------------END-----------------------");
}
public String getSenders() {
return senders;
}
public void setSenders(String senders) {
this.senders = senders;
}
public String getContentKeyWords() {
return contentKeyWords;
}
public void setContentKeyWords(String contentKeyWords) {
this.contentKeyWords = contentKeyWords;
}
public String getSubjectKeyWords() {
return subjectKeyWords;
}
public void setSubjectKeyWords(String subjectKeyWords) {
this.subjectKeyWords = subjectKeyWords;
}
public OrTerm getSearchTerm () {
ArrayList<SearchTerm> sts = new ArrayList<SearchTerm> ();
if (!senders.isEmpty()) {
String[] spilts = senders.split(" ");
for (int index = 0; index < spilts.length; index++) {
if (!spilts[index].isEmpty()) {
FromStringTerm fst = new FromStringTerm (spilts[index]);
sts.add(fst);
}
}
}
if (!subjectKeyWords.isEmpty()) {
String[] spilts = subjectKeyWords.split(" ");
for (int index = 0; index < spilts.length; index++) {
if (!spilts[index].isEmpty()) {
SubjectTerm st = new SubjectTerm (spilts[index]);
sts.add(st);
}
}
}
// if (!contentKeyWords.isEmpty()) {
// String[] spilts = contentKeyWords.split(" ");
//
// for (int index = 0; index < spilts.length; index++) {
// if (!spilts[index].isEmpty()) {
// SubjectTerm st = new SubjectTerm (spilts[index]);
// sts.add(st);
// }
// }
// }
Iterator<SearchTerm> it = sts.iterator();
SearchTerm[] terms = new SearchTerm[sts.size()];
int index = 0;
while (it.hasNext()) {
terms[index] = it.next();
index++;
}
return new OrTerm (terms);
}
public static void main(String[] args)
throws MessagingException, IOException {
ReciveMail rm = new ReciveMail();
Properties props = new Properties();
// props.setProperty("mail.smtp.host", "smtp.sina.com");
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,null);
//(String protocol, String host, int port, String file, String username, String password)
URLName urlname = new URLName("pop3","pop.qq.com",110,null,"805191839","jal@805191839");
Store store = session.getStore(urlname);
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
// SearchTerm term = new AndTerm (new FromStringTerm("jiang-anlin@foxmail.com"),
// new SubjectTerm("testMail"));
SearchTerm term = rm.getSearchTerm();
Message[] msgs = folder.search(term);
// Message msgs[] = folder.getMessages();
int count = msgs.length;
System.out.println("Message Count:"+count);
for(int i=0;i<count;i++){
rm.setMsg((MimeMessage) msgs[i]);
rm.recive(msgs[i],i);
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.mail.search.AndTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.OrTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;
public class ReciveMail {
private MimeMessage msg = null;
private String saveAttchPath = "";
private StringBuffer bodytext = new StringBuffer();
private String dateformate = "yy-MM-dd HH:mm";
private String senders = "jiang-anlin@foxmail.com";
private String subjectKeyWords = "test2";
private String contentKeyWords = "";
// public ReciveMail(MimeMessage msg){
// this.msg = msg;
// }
public ReciveMail () {
}
public void setMsg(MimeMessage msg) {
this.msg = msg;
}
/**
* 获取发送邮件者信息
* @return
* @throws MessagingException
*/
public String getFrom() throws MessagingException{
InternetAddress[] address = (InternetAddress[]) msg.getFrom();
String from = address[0].getAddress();
if(from == null){
from = "";
}
String personal = address[0].getPersonal();
if(personal == null){
personal = "";
}
String fromaddr = personal +"<"+from+">";
return fromaddr;
}
/**
* 获取邮件收件人,抄送,密送的地址和信息。
* 根据所传递的参数不同 "to"-->收件人,"cc"-->抄送人地址,"bcc"-->密送地址
* @param type
* @return
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
public String getMailAddress(String type)
throws MessagingException, UnsupportedEncodingException{
String mailaddr = "";
String addrType = type.toUpperCase();
InternetAddress[] address = null;
if(addrType.equals("TO")||addrType.equals("CC")||addrType.equals("BCC")){
if(addrType.equals("TO")){
//收件人
address = (InternetAddress[]) msg.getRecipients(Message.RecipientType.TO);
}
if(addrType.equals("CC")){
//抄送人地址
address = (InternetAddress[]) msg.getRecipients(Message.RecipientType.CC);
}
if(addrType.equals("BCC")){
//密送地址
address = (InternetAddress[]) msg.getRecipients(Message.RecipientType.BCC);
}
if(address != null){
for(int i=0;i<address.length;i++){
String mail = address[i].getAddress();
if(mail == null){
mail = "";
}else{
mail = MimeUtility.decodeText(mail);
}
String personal = address[i].getPersonal();
if(personal == null){
personal = "";
}else{
personal = MimeUtility.decodeText(personal);
}
String compositeto = personal +"<"+mail+">";
mailaddr += ","+compositeto;
}
mailaddr = mailaddr.substring(1);
}
}else{
throw new RuntimeException("Error email Type!");
}
return mailaddr;
}
/**
* 获取邮件主题
* @return
* @throws UnsupportedEncodingException
* @throws MessagingException
*/
public String getSubject()
throws UnsupportedEncodingException, MessagingException{
String subject = "";
subject = MimeUtility.decodeText(msg.getSubject());
if(subject == null){
subject = "";
}
return subject;
}
/**
* 获取邮件发送日期
* @return
* @throws MessagingException
*/
public String getSendDate() throws MessagingException{
Date sendDate = msg.getSentDate();
SimpleDateFormat smd = new SimpleDateFormat(dateformate);
return smd.format(sendDate);
}
/**
* 获取邮件正文内容
* @return
*/
public String getBodyText(){
return bodytext.toString();
}
/**
* 解析邮件,将得到的邮件内容保存到一个stringBuffer对象中,
* 解析邮件 主要根据MimeType的不同执行不同的操作,一步一步的解析
* @param part
* @throws MessagingException
* @throws IOException
*/
public void getMailContent(Part part)
throws MessagingException, IOException{
String contentType = part.getContentType();
int nameindex = contentType.indexOf("name");
boolean conname = false;
if(nameindex != -1){
conname = true;
}
System.out.println("CONTENTTYPE:"+contentType);
if(part.isMimeType("text/plain")&&!conname){
bodytext.append((String)part.getContent());
}else if(part.isMimeType("text/html")&&!conname){
bodytext.append((String)part.getContent());
}else if(part.isMimeType("multipart/*")){
Multipart multipart = (Multipart) part.getContent();
int count = multipart.getCount();
for(int i=0;i<count;i++){
getMailContent(multipart.getBodyPart(i));
}
}else if(part.isMimeType("message/rfc822")){
getMailContent((Part) part.getContent());
}
}
/**
* 判断邮件是否需要回执,如需回执返回true,否则返回false
* @return
* @throws MessagingException
*/
public boolean getReplySign() throws MessagingException{
boolean replySign = false;
String needreply[] = msg.getHeader("Disposition-Notification-TO");
if(needreply != null){
replySign = true;
}
return replySign;
}
/**
* 获取此邮件的message-id
* @return
* @throws MessagingException
*/
public String getMessageId() throws MessagingException{
return msg.getMessageID();
}
/**
* 判断此邮件是否已读,如果未读则返回false,已读返回true
* @return
* @throws MessagingException
*/
public boolean isNew() throws MessagingException{
boolean isnew = false;
Flags flags = ((Message)msg).getFlags();
Flags.Flag[] flag = flags.getSystemFlags();
System.out.println("flags's length:"+flag.length);
for(int i=0;i<flag.length;i++){
if(flag[i]==Flags.Flag.SEEN){
isnew = true;
System.out.println("seen message .......");
break;
}
}
return isnew;
}
/**
* 判断是是否包含附件
* @param part
* @return
* @throws MessagingException
* @throws IOException
*/
public boolean isContainAttch(Part part) throws MessagingException, IOException{
boolean flag = false;
String contentType = part.getContentType();
if(part.isMimeType("multipart/*")){
Multipart multipart = (Multipart) part.getContent();
int count = multipart.getCount();
for(int i=0;i<count;i++){
BodyPart bodypart = multipart.getBodyPart(i);
String dispostion = bodypart.getDisposition();
if((dispostion != null)&&(dispostion.equals(Part.ATTACHMENT)||dispostion.equals(Part.INLINE))){
flag = true;
}else if(bodypart.isMimeType("multipart/*")){
flag = isContainAttch(bodypart);
}else{
String conType = bodypart.getContentType();
if(conType.toLowerCase().indexOf("appliaction")!=-1){
flag = true;
}
if(conType.toLowerCase().indexOf("name")!=-1){
flag = true;
}
}
}
}else if(part.isMimeType("message/rfc822")){
flag = isContainAttch((Part) part.getContent());
}
return flag;
}
/**
* 保存附件
* @param part
* @throws MessagingException
* @throws IOException
*/
public void saveAttchMent(Part part) throws MessagingException, IOException{
String filename = "";
if(part.isMimeType("multipart/*")){
Multipart mp = (Multipart) part.getContent();
for(int i=0;i<mp.getCount();i++){
BodyPart mpart = mp.getBodyPart(i);
String dispostion = mpart.getDisposition();
if((dispostion != null)&&(dispostion.equals(Part.ATTACHMENT)||dispostion.equals(Part.INLINE))){
filename = mpart.getFileName();
if(filename.toLowerCase().indexOf("gb2312")!=-1){
filename = MimeUtility.decodeText(filename);
}
saveFile(filename,mpart.getInputStream());
}else if(mpart.isMimeType("multipart/*")){
saveAttchMent(mpart);
}else{
filename = mpart.getFileName();
if(filename != null&&(filename.toLowerCase().indexOf("gb2312")!=-1)){
filename = MimeUtility.decodeText(filename);
}
saveFile(filename,mpart.getInputStream());
}
}
}else if(part.isMimeType("message/rfc822")){
saveAttchMent((Part) part.getContent());
}
}
/**
* 获得保存附件的地址
* @return
*/
public String getSaveAttchPath() {
return saveAttchPath;
}
/**
* 设置保存附件地址
* @param saveAttchPath
*/
public void setSaveAttchPath(String saveAttchPath) {
this.saveAttchPath = saveAttchPath;
}
/**
* 设置日期格式
* @param dateformate
*/
public void setDateformate(String dateformate) {
this.dateformate = dateformate;
}
/**
* 保存文件内容
* @param filename
* @param inputStream
* @throws IOException
*/
private void saveFile(String filename, InputStream inputStream) throws IOException {
String osname = System.getProperty("os.name");
String storedir = getSaveAttchPath();
String sepatror = "";
if(osname == null){
osname = "";
}
if(osname.toLowerCase().indexOf("win")!=-1){
sepatror = "//";
if(storedir==null||"".equals(storedir)){
storedir = "d://temp";
}
}else{
sepatror = "/";
storedir = "/temp";
}
File storefile = new File(storedir+sepatror+filename);
System.out.println("storefile's path:"+storefile.toString());
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(storefile));
bis = new BufferedInputStream(inputStream);
int c;
while((c= bis.read())!=-1){
bos.write(c);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
bos.close();
bis.close();
}
}
public void recive(Part part,int i) throws MessagingException, IOException{
System.out.println("------------------START-----------------------");
System.out.println("Message"+i+" subject:" + getSubject());
System.out.println("Message"+i+" from:" + getFrom());
System.out.println("Message"+i+" isNew:" + isNew());
boolean flag = isContainAttch(part);
System.out.println("Message"+i+" isContainAttch:" +flag);
System.out.println("Message"+i+" replySign:" + getReplySign());
getMailContent(part);
System.out.println("Message"+i+" content:" + getBodyText());
// setSaveAttchPath("c://temp//"+i);
// if(flag){
// saveAttchMent(part);
// }
System.out.println("------------------END-----------------------");
}
public String getSenders() {
return senders;
}
public void setSenders(String senders) {
this.senders = senders;
}
public String getContentKeyWords() {
return contentKeyWords;
}
public void setContentKeyWords(String contentKeyWords) {
this.contentKeyWords = contentKeyWords;
}
public String getSubjectKeyWords() {
return subjectKeyWords;
}
public void setSubjectKeyWords(String subjectKeyWords) {
this.subjectKeyWords = subjectKeyWords;
}
public OrTerm getSearchTerm () {
ArrayList<SearchTerm> sts = new ArrayList<SearchTerm> ();
if (!senders.isEmpty()) {
String[] spilts = senders.split(" ");
for (int index = 0; index < spilts.length; index++) {
if (!spilts[index].isEmpty()) {
FromStringTerm fst = new FromStringTerm (spilts[index]);
sts.add(fst);
}
}
}
if (!subjectKeyWords.isEmpty()) {
String[] spilts = subjectKeyWords.split(" ");
for (int index = 0; index < spilts.length; index++) {
if (!spilts[index].isEmpty()) {
SubjectTerm st = new SubjectTerm (spilts[index]);
sts.add(st);
}
}
}
// if (!contentKeyWords.isEmpty()) {
// String[] spilts = contentKeyWords.split(" ");
//
// for (int index = 0; index < spilts.length; index++) {
// if (!spilts[index].isEmpty()) {
// SubjectTerm st = new SubjectTerm (spilts[index]);
// sts.add(st);
// }
// }
// }
Iterator<SearchTerm> it = sts.iterator();
SearchTerm[] terms = new SearchTerm[sts.size()];
int index = 0;
while (it.hasNext()) {
terms[index] = it.next();
index++;
}
return new OrTerm (terms);
}
public static void main(String[] args)
throws MessagingException, IOException {
ReciveMail rm = new ReciveMail();
Properties props = new Properties();
// props.setProperty("mail.smtp.host", "smtp.sina.com");
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,null);
//(String protocol, String host, int port, String file, String username, String password)
URLName urlname = new URLName("pop3","pop.qq.com",110,null,"805191839","jal@805191839");
Store store = session.getStore(urlname);
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
// SearchTerm term = new AndTerm (new FromStringTerm("jiang-anlin@foxmail.com"),
// new SubjectTerm("testMail"));
SearchTerm term = rm.getSearchTerm();
Message[] msgs = folder.search(term);
// Message msgs[] = folder.getMessages();
int count = msgs.length;
System.out.println("Message Count:"+count);
for(int i=0;i<count;i++){
rm.setMsg((MimeMessage) msgs[i]);
rm.recive(msgs[i],i);
}
}
}
相关推荐
总结来说,通过EWSJavaAPI_1.2.jar,Java开发者可以轻松地实现与Microsoft Exchange Server的集成,完成发送、接收和查询邮件的任务。在实际项目中,还需要考虑错误处理、性能优化以及安全性的相关问题,例如使用SSL...
项目中要求读取指定邮件未读邮件的内容,在网上查了很多资料发现都不行,不是自己想要的,要么是读取最新的,要么是读取第一封邮件,根本就不能读取未读的旧邮件。于是我自己查API写了一个,读取未读邮件,读取最新...
JavaMail提供了查询邮件的接口,可以通过`Folder.search()`方法结合`SearchTerm`来实现。 项目中的"成功界面"可能指的是在执行邮件操作后显示的成功提示,增强了用户体验。而"转发修复"可能是对之前版本中邮件转发...
邮件查询数据库功能说明 在IT领域,邮件查询数据库是一个关键组件,它负责管理和处理与电子邮件...在实际应用中,这样的系统可以支持大量用户同时查询邮件,进行数据分析,并有效地管理邮件附件,满足企业级的需求。
4. **查询邮件**:列出、搜索或获取指定的邮件。 5. **操作邮件**:读取、移动、删除或标记邮件。 6. **断开连接**:使用`LOGOUT`命令安全退出。 ### POP (邮局协议) POP是另一种用于从邮件服务器下载邮件的协议,...
- **查询IP地址**:进一步查询邮件服务器的IP地址。这样,邮件就可以被准确地发送到目标服务器上了。 如果无法找到MX记录,则默认将主机域名视为邮件服务器域名。 #### 三、电子邮件系统的工作原理 ##### 1. 存储...
3. **查询邮件**:使用`NotesView`对象查询邮件视图,并获取邮件文档。可能需要根据日期、发件人、主题等条件进行筛选。 4. **读取邮件内容**:遍历邮件文档,获取邮件的各个字段,如发件人、收件人、主题、正文等...
2. **查询邮件列表**:执行SQL查询语句,从数据库中检索需要发送邮件的收件人地址。可能需要一个表来存储收件人的电子邮件地址和其他相关信息。 3. **构建邮件内容**:在JSP中,我们可以使用EL(Expression ...
3. **查询邮件数量**:使用`GetMessageCount`方法获取邮件服务器上的邮件总数。 4. **选择和下载邮件**:根据用户需求,可以使用`GetMessage`或`GetMessages`方法获取指定编号或所有邮件的信息。这些方法会返回...
2. 查询邮件:使用FindItems或FindFolders方法,可以基于各种条件(如未读、已读、特定日期等)查找邮件。 3. 处理邮件:找到的每一封EmailMessage都可以被读取、标记为已读、移动到其他文件夹或者删除。 需要注意...
- 服务器端会使用IMAP命令查询邮件,获取邮件ID、主题、发件人、收件人等元数据,并将邮件正文和附件作为数据流返回给客户端。 - 客户端需要解析这些流,根据MIME(Multipurpose Internet Mail Extensions)标准来...
2. 邮件检索模块:查询邮件服务器上的新邮件,并选择下载哪些邮件。 3. 邮件解析模块:解析邮件的MIME格式,提取主题、正文、附件等信息。 4. 邮件存储模块:将接收到的邮件保存在数据库或其他存储介质中,便于后续...
8. EXPN:查询邮件列表和别名,也常被禁用以防止滥用。 9. HELP:提供帮助信息。 10. NOOP:无操作,仅确认连接可用。 11. QUIT:结束SMTP会话。 1995年,SMTP进行了扩展,引入了EHLO命令,以支持更多的ESMTP...
IMAP类库提供了与IMAP服务器交互的底层实现,包括登录、查询邮件、下载邮件、管理邮箱等操作。IMAP客户端则是这些功能的封装,提供了友好的用户界面,使得开发者或普通用户能够方便地进行邮件操作。 C#语言是.NET...
用于查询邮件活动的PowerShell模块。 使用此PowerShell模块,您可以使用未记录的ActivityAccess API查询用户的Exchange Online邮件活动。 该API允许访问比“正式”活动日志更长的时间来存储活动的日志。 这有助于...
例如,通过查询邮件服务器上的已发送邮件文件夹,获取发送记录,并允许用户进行查看和删除操作。 5. **源代码分析**: 提供的"JMail"压缩文件很可能是JMail的源代码,这对于学习和自定义功能非常有价值。通过阅读...
4. **查询邮件**: - 使用WebDAV的`PROPFIND`方法查询邮箱中的邮件。这需要构造XML请求体,列出要查询的属性,如邮件ID、主题、发件人等。 - 你可以使用`WebClient.UploadString()`方法发送请求并获取响应。 5. *...
4. **查询邮件数量**:通过调用`GetMessageCount()`方法获取邮箱中的邮件总数。 5. **遍历并下载邮件**:对于每个邮件,使用`GetMessage()`方法获取邮件对象。这个对象包含了邮件的头部信息和内容。 6. **获取邮件...
同时,UI还应提供搜索功能,利用AJAX实时查询邮件服务器,快速返回搜索结果。 接下来是编码篇,这涉及到JavaScript和可能的库如jQuery或axios的使用。AJAX的核心是XMLHttpRequest对象,通过它向服务器发送异步请求...