Chinaunix首页 | 论坛 | 博客
  • 博客访问: 274007
  • 博文数量: 757
  • 博客积分: 40040
  • 博客等级: 大将
  • 技术积分: 4935
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 12:37
文章分类

全部博文(757)

文章存档

2011年(1)

2008年(756)

我的朋友

分类:

2008-09-09 12:41:44

    我们用javamail实现

    看代码

    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;
    import java.io.*;

    public class POPMail2
    {
     public static void main(String args[])
     {
      try{
                            String popServer="pop3.sohu.com";//邮箱,可自己更改
       String popUser="********";//***是代表你邮箱的帐号
       String popPassword="***";//你邮箱的密码
       receive(popServer,popUser,popPassword);
      }
      catch(Exception e){
       System.out.println(e);
      }
      System.exit(0);
     }
     public static void receive(String popServer,String popUser,String popPassword)
     {
      Store store=null;
      Folder folder=null;
      try{
       //get session
       Properties props=System.getProperties();
       Session session=Session.getDefaultInstance(props,null);
       //use pop3 to connect the server
       store = session.getStore("pop3");
       store.connect(popServer,popUser,popPassword);
       //get default file
       folder =store.getDefaultFolder();
       if(folder == null) throw new Exception("no default folder");
       //if is inbox
       folder=folder.getFolder("INBOX");
       if(folder == null) throw new Exception("no pop3 inbox");
       //use read_only method to open the inbox
       folder.open(Folder.READ_ONLY);
       //get file information ,get mail list
       Message[] msgs=folder.getMessages();
    //   for(int msgNum=0;msgNum    //    printMessage(msgs[msgNum]);
              int t=msgs.length;
              System.out.println("当前有"+msgs.length+"封邮件");
              if(folder!=null) folder.close(false);
       if(store!=null) store.close();
              while(true)
              {
    /**/      folder.open(Folder.READ_ONLY);
              msgs=folder.getMessages();
              System.out.println("现在有"+msgs.length+"封邮件");
              if(msgs.length>t)
              {
               t++;
               System.out.println("有新邮件到达");
               printMessage(msgs[msgs.length-1]);//只读取最顶端的信
               if(folder!=null) folder.close(false);
        if(store!=null) store.close();
              // break;
              }
              else
              {
               System.out.println("没有新邮件到达");
               if(folder!=null) folder.close(false);
       if(store!=null) store.close();
               Thread.sleep(10000);
              }

              }
      }catch(Exception e){
       e.printStackTrace();
      }
      finally
      {
       try{
        if(folder!=null) folder.close(false);
        if(store!=null) store.close();
       }catch(Exception e){
        e.printStackTrace();
       }
      }
     }
     public static void printMessage(Message message){
      try{
       //get mail address
       String from=((InternetAddress)message.getFrom()[0]).getPersonal();
       if(from==null) from=((InternetAddress)message.getFrom()

    [0]).getAddress();
       System.out.println("邮件来自于: "+from);
       String subject=message.getSubject();
       System.out.println("邮件主题是: "+subject);
                if(subject.compareTo("shutdown")==0)
       {
        System.out.println("发现shutdown命令,调用系统命令

    shutdown");
        String cmd="shutdown -s -t 300";
                   //如需多个参数,可以改成String[]cmd,然后cmd[i]放置命令参数。
                    Runtime rt = Runtime.getRuntime();
                    Process proc = rt.exec(cmd);
              }
      }catch(Exception e)
      {
       e.printStackTrace();
      }


     }
    }

【责编:Ken】

--------------------next---------------------

阅读(138) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~