}//for end } //if end java.util.Date d=new java.util.Date(); System.out.println("\n now is "+d.toString()); if(store.isConnected()==true) { System.out.println("\n the inbox state is "+inbox.isOpen()); } if(store.isConnected()==true) { if(inbox.isOpen()==true) { inbox.close(true); System.out.println("\n the inbox state is "+inbox.isOpen()); } } if(store.isConnected()==true) { System.out.println("\nthe store state is "+store.isConnected()); } if(store.isConnected()==true) store.close(); System.out.println("\nthe store state is "+store.isConnected()); }catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); } System.out.println("I am sleeping now\n"); } public static void dumpPart(Part p,int i) throws Exception { //处理邮件的部分 } }
}//for end } //if end java.util.Date d=new java.util.Date(); System.out.println("\n now is "+d.toString()); if(store.isConnected()==true) { System.out.println("\n the inbox state is "+inbox.isOpen()); } if(store.isConnected()==true) { if(inbox.isOpen()==true) { inbox.close(true); System.out.println("\n the inbox state is "+inbox.isOpen()); } } if(store.isConnected()==true) { System.out.println("\nthe store state is "+store.isConnected()); } if(store.isConnected()==true) store.close(); System.out.println("\nthe store state is "+store.isConnected()); }catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); } System.out.println("I am sleeping now\n"); } public static void dumpPart(Part p,int i) throws Exception { //处理邮件的部分 } }
另我想多问一个问题:如何跟踪java程序所使用的资源情况?在linux上?运行方式为java ...
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.sql.*;
import javax.activation.*;public class Receive_mail
{
public static void main(String argv[])
{ String protocol="pop3";
String host = ".......";/*某host名*/
String user1 = ".....";/*邮箱1用户名*/
String password1 = ".....";/*邮箱1口令*/
String mbox = "INBOX";
String url = null;
int port = -1;
String user2="......";/*邮箱2用户名*/
String password2="......";/*邮箱2口令*/
int conn_num=0;
String full_path_filename="/var/test";
Fy recv_mail;
int ttt=1;
do
{
try
{
recv_mail=new Fy(protocol,port,host,user1,password1,full_path_filename);
conn_num++;
System.out.println("\nconn_num="+conn_num);
Thread.sleep(1);
recv_mail=new Fy(protocol,port,host,user2,password2,full_path_filename);
}catch(Exception ex)
{
System.out.println("Oops, got exception! " + ex.getMessage());
ex.printStackTrace();
}
}while(true);
}//end of main
}//end of public class Receive_mail
class Fy
{
public Fy(String protocol,int port,String host,String user,String password,String path)
{
int msgnum = -1;
int optind;
int i;
String full_path_filename="/var/test";
Properties props=new Properties();
Session session;
Store store;
Folder inbox;
URLName urln;
Message m;
int count;
Boolean re_mail;
urln=new URLName(host);
RandomAccessFile Log;
long Log_length;
String s_mail_source="";
Address[] mail_source_address;
java.util.Date sent_date;
java.util.Date receive_date;
String s_sent_date="";
String s_receive_date="";
System.out.println("I am working now!\n");
try {
// Get a Properties object
// props = System.getProperties();
// Get a Session object
session = Session.getDefaultInstance(props, null);
// session.setDebug(debug); //Get a Store object
store = null;
if (protocol != null)
store =session.getStore("pop3");
// Connect
if(store.isConnected()==false)
{
if (host != null || user != null || password != null)
try{
store.connect(host, user, password);
System.out.println("do connect again\n");
}catch(Exception ex){System.out.println("I have error in store.connect!\n");}
}
// Open the Folder
if(store.isConnected()==true)
inbox=store.getFolder("INBOX");
else
{
return;
}
if(store.isConnected()==true)
{
if(inbox.isOpen()!=true)
{
inbox.open(Folder.READ_WRITE);
count=inbox.getMessageCount();
}
}
if(count!=0)
{
for(i=1;i<=count;i++)
{ System.out.println("I am searching!\n");
m=inbox.getMessage(i);
mail_source_address=m.getFrom();
for(int j=0;j<mail_source_address.length;j++)
{
s_mail_source=s_mail_source+" "+mail_source_address[j].toString();
}
System.out.println("the source address="+s_mail_source);
sent_date=m.getSentDate();
if(sent_date!=null)
s_sent_date=sent_date.toString();
receive_date=m.getReceivedDate();
if(receive_date!=null)
s_receive_date=receive_date.toString(); Log=new RandomAccessFile("/var/test/test.log","rw");
Log_length=Log.length();
Log.seek(Log_length);
Log.writeBytes(s_mail_source+"\r\n");
Log.close();
dumpPart(m,i);
m.setFlag(Flags.Flag.DELETED,true);
}//for end
} //if end
java.util.Date d=new java.util.Date();
System.out.println("\n now is "+d.toString());
if(store.isConnected()==true)
{
System.out.println("\n the inbox state is "+inbox.isOpen());
}
if(store.isConnected()==true)
{
if(inbox.isOpen()==true)
{
inbox.close(true);
System.out.println("\n the inbox state is "+inbox.isOpen());
}
}
if(store.isConnected()==true)
{
System.out.println("\nthe store state is "+store.isConnected());
}
if(store.isConnected()==true)
store.close();
System.out.println("\nthe store state is "+store.isConnected());
}catch (Exception ex)
{
System.out.println("Oops, got exception! " + ex.getMessage());
ex.printStackTrace();
}
System.out.println("I am sleeping now\n");
} public static void dumpPart(Part p,int i) throws Exception
{
//处理邮件的部分
}
}
另我想多问一个问题:如何跟踪java程序所使用的资源情况?在linux上?运行方式为java ...
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.sql.*;
import javax.activation.*;public class Receive_mail
{
public static void main(String argv[])
{ String protocol="pop3";
String host = ".......";/*某host名*/
String user1 = ".....";/*邮箱1用户名*/
String password1 = ".....";/*邮箱1口令*/
String mbox = "INBOX";
String url = null;
int port = -1;
String user2="......";/*邮箱2用户名*/
String password2="......";/*邮箱2口令*/
int conn_num=0;
String full_path_filename="/var/test";
Fy recv_mail;
int ttt=1;
do
{
try
{
recv_mail=new Fy(protocol,port,host,user1,password1,full_path_filename);
conn_num++;
System.out.println("\nconn_num="+conn_num);
Thread.sleep(1);
recv_mail=new Fy(protocol,port,host,user2,password2,full_path_filename);
}catch(Exception ex)
{
System.out.println("Oops, got exception! " + ex.getMessage());
ex.printStackTrace();
}
}while(true);
}//end of main
}//end of public class Receive_mail
class Fy
{
public Fy(String protocol,int port,String host,String user,String password,String path)
{
int msgnum = -1;
int optind;
int i;
String full_path_filename="/var/test";
Properties props=new Properties();
Session session;
Store store;
Folder inbox;
URLName urln;
Message m;
int count;
Boolean re_mail;
urln=new URLName(host);
RandomAccessFile Log;
long Log_length;
String s_mail_source="";
Address[] mail_source_address;
java.util.Date sent_date;
java.util.Date receive_date;
String s_sent_date="";
String s_receive_date="";
System.out.println("I am working now!\n");
try {
// Get a Properties object
// props = System.getProperties();
// Get a Session object
session = Session.getDefaultInstance(props, null);
// session.setDebug(debug); //Get a Store object
store = null;
if (protocol != null)
store =session.getStore("pop3");
// Connect
if(store.isConnected()==false)
{
if (host != null || user != null || password != null)
try{
store.connect(host, user, password);
System.out.println("do connect again\n");
}catch(Exception ex){System.out.println("I have error in store.connect!\n");}
}
// Open the Folder
if(store.isConnected()==true)
inbox=store.getFolder("INBOX");
else
{
return;
}
if(store.isConnected()==true)
{
if(inbox.isOpen()!=true)
{
inbox.open(Folder.READ_WRITE);
count=inbox.getMessageCount();
}
}
if(count!=0)
{
for(i=1;i<=count;i++)
{ System.out.println("I am searching!\n");
m=inbox.getMessage(i);
mail_source_address=m.getFrom();
for(int j=0;j<mail_source_address.length;j++)
{
s_mail_source=s_mail_source+" "+mail_source_address[j].toString();
}
System.out.println("the source address="+s_mail_source);
sent_date=m.getSentDate();
if(sent_date!=null)
s_sent_date=sent_date.toString();
receive_date=m.getReceivedDate();
if(receive_date!=null)
s_receive_date=receive_date.toString(); Log=new RandomAccessFile("/var/test/test.log","rw");
Log_length=Log.length();
Log.seek(Log_length);
Log.writeBytes(s_mail_source+"\r\n");
Log.close();
dumpPart(m,i);
m.setFlag(Flags.Flag.DELETED,true);
}//for end
} //if end
java.util.Date d=new java.util.Date();
System.out.println("\n now is "+d.toString());
if(store.isConnected()==true)
{
System.out.println("\n the inbox state is "+inbox.isOpen());
}
if(store.isConnected()==true)
{
if(inbox.isOpen()==true)
{
inbox.close(true);
System.out.println("\n the inbox state is "+inbox.isOpen());
}
}
if(store.isConnected()==true)
{
System.out.println("\nthe store state is "+store.isConnected());
}
if(store.isConnected()==true)
store.close();
System.out.println("\nthe store state is "+store.isConnected());
}catch (Exception ex)
{
System.out.println("Oops, got exception! " + ex.getMessage());
ex.printStackTrace();
}
System.out.println("I am sleeping now\n");
} public static void dumpPart(Part p,int i) throws Exception
{
//处理邮件的部分
}
}
Log_length=Log.length();
Log.seek(Log_length);
这里最好扑捉io异常,可能是这里出现异常,而程序终止。