当前位置: 技术问答>java相关
javamail 高手看过来(低手免进!!!)
来源: 互联网 发布时间:2015-02-07
本文导语: import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.event.*; import javax.activation.*; /* Monitors given mailbox for new mail */ public class monitor { public static void main(String argv[]) { String ar []...
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.activation.*;
/* Monitors given mailbox for new mail */
public class monitor
{
public static void main(String argv[])
{
String ar [] = new String[5];
ar[0] = 协议;//imap or pop
ar[1] = 用户名;
ar[2] = 密码;
ar[3] = "INBOX";
ar[4] = "1000";
try
{
Properties props = System.getProperties();
props.put("mail.imap.partialfetch", "false");
// Get a Session object
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
// Get a Store object
Store store = session.getStore("imap");
// Store store = session.getStore("pop3");
// Connect
try
{
System.out.println("start store.connect :"+ar[0]+" "+ar[1]+" "+ar[2]);
store.connect(ar[0] ,ar[1], ar[2]);
System.out.println("store.connect ok");
}
catch(MessagingException m)
{
System.out.println("store.connect error : "+m.getMessage());
}
// Open a Folder
Folder folder = store.getFolder(ar[3]);
if (folder == null || !folder.exists())
{
System.out.println("Invalid folder");
System.exit(1);
}
folder.open(Folder.READ_WRITE);
folder.addMessageChangedListener(new MessageChangedListener()
{
public void messageChanged(MessageChangedEvent e)
{
System.out.println(" public void messageChanged(MessageChangedEvent e)");
System.out.println("e.getMessage().getMessageNumber() : "+e.getMessage().getMessageNumber());
}
});
// Add messageCountListener to listen for new messages
folder.addMessageCountListener(new MessageCountAdapter()
{
public void messagesAdded(MessageCountEvent ev)
{
Message[] msgs = ev.getMessages();
System.out.println("Got " + msgs.length + " new messages");
// Just dump out the new messages
for (int i = 0; i
import java.io.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.activation.*;
/* Monitors given mailbox for new mail */
public class monitor
{
public static void main(String argv[])
{
String ar [] = new String[5];
ar[0] = 协议;//imap or pop
ar[1] = 用户名;
ar[2] = 密码;
ar[3] = "INBOX";
ar[4] = "1000";
try
{
Properties props = System.getProperties();
props.put("mail.imap.partialfetch", "false");
// Get a Session object
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
// Get a Store object
Store store = session.getStore("imap");
// Store store = session.getStore("pop3");
// Connect
try
{
System.out.println("start store.connect :"+ar[0]+" "+ar[1]+" "+ar[2]);
store.connect(ar[0] ,ar[1], ar[2]);
System.out.println("store.connect ok");
}
catch(MessagingException m)
{
System.out.println("store.connect error : "+m.getMessage());
}
// Open a Folder
Folder folder = store.getFolder(ar[3]);
if (folder == null || !folder.exists())
{
System.out.println("Invalid folder");
System.exit(1);
}
folder.open(Folder.READ_WRITE);
folder.addMessageChangedListener(new MessageChangedListener()
{
public void messageChanged(MessageChangedEvent e)
{
System.out.println(" public void messageChanged(MessageChangedEvent e)");
System.out.println("e.getMessage().getMessageNumber() : "+e.getMessage().getMessageNumber());
}
});
// Add messageCountListener to listen for new messages
folder.addMessageCountListener(new MessageCountAdapter()
{
public void messagesAdded(MessageCountEvent ev)
{
Message[] msgs = ev.getMessages();
System.out.println("Got " + msgs.length + " new messages");
// Just dump out the new messages
for (int i = 0; i