查看郵件資訊列表

來源:互聯網
上載者:User

下面是查看郵件資訊列表的JAVA代碼:

import javax.mail.Store;   
import javax.mail.FetchProfile;   
import java.util.Properties;   
import javax.mail.Session;   
import javax.mail.Folder;   
import javax.mail.Message;   
import javax.mail.internet.InternetAddress;   
  
public class Semail {   
    public static void main(String[] args) {   
        try {   
  
            Properties props = new Properties();   
            Session s = Session.getInstance(props);   
            Store store = s.getStore("pop3");   
            store.connect("pop.163.com", "bluebit_cn", "xiaohao");   
  
            Folder folder = store.getFolder("Inbox");   
            folder.open(Folder.READ_WRITE);   
  
            FetchProfile profile = new FetchProfile();   
            profile.add(FetchProfile.Item.ENVELOPE);   
            Message arraymessage[] = folder.getMessages();   
            folder.fetch(arraymessage, profile);   
  
            System.out.println("收件匣的郵件數:" + arraymessage.length);   
            for (int i = 0; i < arraymessage.length; i++) {   
                //郵件寄件者   
                String from = arraymessage[i].getFrom()[0].toString();   
                InternetAddress ia = new InternetAddress(from);   
                System.out.println("FROM:" + ia.getPersonal() + '(' +   
                                   ia.getAddress() + ')');   
                //郵件標題   
                System.out.println("TITLE:" + arraymessage[i].getSubject());   
                //郵件大小   
                System.out.println("SIZE:" + arraymessage[i].getSize());   
                //郵件發送時間   
                System.out.println("DATE:" + arraymessage[i].getSentDate());   
            }   
  
            folder.close(false);   
            store.close();   
        } catch (Exception ee) {   
            ee.printStackTrace();   
        }   
    }   
}

==刪除=====================
Question  How do you delete a message from the mail server?  
Derived from  An unanswered question originally posed by Benjamin Alejandro Rodriguez Rengifo  
Topics  Java:API:JavaMail:IMAP4, Java:API:JavaMail:POP3  
Author  John Zukowski  
Created  23-Feb-00  Modified  25-Jul-00  

Answer 

The basic process of deleting a message is to call setFlag() on the message and set the Flags.Flag.DeleteD flag to true.

message.setFlag(Flags.Flag.DeleteD, true);

Then, when you close the folder, deleted messages will be removed.

Be sure to open the folder for read/write access:

folder.open(Folder.READ_WRITE);

The following program demonstrates listing each message in the folder and prompting for deletion:

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

public class DeleteMessageExample {
  public static void main (String args[]) throws Exception {
    String host = args[0];
    String username = args[1];
    String password = args[2];

    // Get session
    Session session = Session.getInstance(
      System.getProperties(), null);

    // Get the store
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);

    BufferedReader reader = new BufferedReader (
      new InputStreamReader(System.in));

    // Get directory
    Message message[] = folder.getMessages();
    for (int i=0, n=message.length; i<n; i++) {
       System.out.println(i + ": " + message[i].getFrom()[0] 
         + "\t" + message[i].getSubject());

       System.out.println("Do you want to delete message? [YES to delete]");
       String line = reader.readLine();
       // Mark as deleted if appropriate
       if ("YES".equals(line)) {
         message[i].setFlag(Flags.Flag.DeleteD, true);
       }
    }

    // Close connection 
    folder.close(true);
    store.close();
  }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.