www.pudn.com > mailKKKK.rar > Main.java


/* 
 * Main.java 
 * 
 * Created on 2007Äê6ÔÂ12ÈÕ, ÏÂÎç3:51 
 * 
 * To change this template, choose Tools | Template Manager 
 * and open the template in the editor. 
 */ 
 
package mailtest; 
 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 
import java.util.*; 
/** 
 * 
 * @author Administrator 
 */ 
public class Main { 
     
    /** Creates a new instance of Main */ 
    public Main() { 
    } 
     
    /** 
     * @param args the command line arguments 
     */ 
    public static void main(String[] args) throws Exception{ 
        String hostname="localhost"; 
        String username ="admin"; 
        String password ="123456"; 
         
//set properties 
        Properties props=System.getProperties(); 
        props.put("mail.transport.protocol","smtp"); 
        props.put("mail.store.protocol","imap"); 
        props.put("mail.smtp.class","com.sun.mail.smtp.SMTPTransport"); 
        props.put("mail.imap.class","com.sun.mail.imap.IMAPStore"); 
        props.put("mail.smtp.host","hostname"); 
//get aSession object 
         
        Session mailsession = Session.getDefaultInstance(props,null); 
         
//Get a Store object 
        Store store = mailsession.getStore("imap"); 
//Connect 
        store.connect(hostname,username,password); 
         
//check inbox 
        Folder folder=store.getFolder("inbox"); 
        folder.open(Folder.READ_WRITE); 
        System.out.println("You have"+folder.getMessageCount()+"messages in inbox"); 
        System.out.println("You have"+folder.getUnreadMessageCount()+"unread messages in inbox."); 
         
//read first Message in inbox 
        Message msg=folder.getMessage(1); 
        System.out.println("----the first message in inbox----"); 
        System.out.println("Form:"+msg.getFrom()[0]); 
        System.out.println("Subject:"+msg.getSubject()); 
        System.out.println("Text:"+msg.getContent()); 
         
        //create a message 
        msg =new MimeMessage(mailsession); 
        InternetAddress[] toAddrs =InternetAddress.parse("admin@mydomain.com",false); 
        msg.setRecipients(Message.RecipientType.TO,toAddrs); 
        msg.setSubject("hello"); 
        msg.setFrom(new InternetAddress("admin@mydomain.com")); 
        msg.setText("How are you"); 
         
//send amessage 
        Transport.send(msg); 
    } 
}