收邮件的演示程序 站点:爱心种子小博士 关键字:收邮件的演示程序
|
收邮件的演示程序 /*
* @(#)MailSend.java
*/
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
/**
* 接收邮件
*/
public class MailReceiver {
/**
* main方法从命令行得到邮件服务器的参数
*/
public static void main(String args[]) {
try {
String popServer=args[0];
String popUser=args[1];
String popPassword=args[2];
receive(popServer, popUser, popPassword);
}
catch (Exception ex) {
System.out.println("Usage: MailSend popServer popUser popPassword");
}
System.exit(0);
}
/**
* 获得并处理消息
*/
public static void receive(String popServer, String popUser, String popPassword) {
Store store=null;
Folder folder=null;
try {
// 得到缺省的session
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
// 得到POP3 message store, 并连接
store = session.getStore("pop3");
store.connect(popServer, popUser, popPassword);
// 得到缺省的Folder对象
folder = store.getDefaultFolder();
if (folder == null) throw new Exception("No default folder");
// 得到Folder对象的INBOX
folder = folder.getFolder("INBOX");
if (folder == null) throw new Exception("No POP3 INBOX");
// 以只读的方式打开文件夹
folder.open(Folder.READ_ONLY);
// 获得消息并显示
Message[] msgs = folder.getMessages();
for (int msgNum = 0; msgNum < msgs.length; msgNum++) {
printMessage(msgs[msgNum]);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
//关闭对象
try {
if (folder!=null) folder.close(false);
if (store!=null) store.close();
}
catch (Exception ex2) {ex2.printStackTrace();}
}
}
/**
* 输出邮件的内容
*/
public static void printMessage(Message message) {
try {
// 得到邮件头的信息
String from=((InternetAddress)message.getFrom()[0]).getPersonal();
if (from==null) from=((InternetAddress)message.getFrom()[0]).getAddress();
System.out.println("FROM: "+from);
String subject=message.getSubject();
System.out.println("SUBJECT: "+subject);
// 得到邮件的体
Part messagePart=message;
Object content=messagePart.getContent();
// 如果是multipart消息
if (content instanceof Multipart) {
messagePart=((Multipart)content).getBodyPart(0);
System.out.println("[ Multipart Message ]");
}
// 得到contentType
String contentType=messagePart.getContentType();
// 如果邮件的内容是简单无格式的文本,则输出到屏幕
System.out.println("CONTENT:"+contentType);
if (contentType.startsWith("text/plain")
|| contentType.startsWith("text/html")) {
InputStream is = messagePart.getInputStream();
BufferedReader reader
=new BufferedReader(new InputStreamReader(is));
String thisLine=reader.readLine();
while (thisLine!=null) {
System.out.println(thisLine);
thisLine=reader.readLine();
}
}
System.out.println("-----------------------------");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
运行结果:
D:\java>java MailReceiver pop3.163.com zz3zcwb 123456
FROM: [email protected]
SUBJECT: 我发的信,
CONTENT:text/plain; charset=GBK
I am OK,很好!请不要发大量拉圾邮件
-----------------------------
FROM: [email protected]
SUBJECT: 我发的信,
CONTENT:text/plain; charset=GBK
I am OK,很好!请不要发大量拉圾邮件
-----------------------------
FROM: [email protected]
SUBJECT: ????????
[ Multipart Message ]
CONTENT:Text/Plain
----------------------------- |
|
|
|