[JAVA100例]048、操纵JavaMail采取邮件[Java编程]
本文“[JAVA100例]048、操纵JavaMail采取邮件[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
/**
* <p>Title: 利用JavaMail接纳邮件</p>
* <p>Description: 实例JavaMail包接纳邮件,本实例没有实现接纳邮件的附件.</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: POPMail.java</p>
* @version 1.0
*/
public class POPMail{
/**
*<br>办法阐明:主办法,接纳用户输入的邮箱服务器、用户名和密码
*<br>输入参数:
*<br>返回范例:
*/
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: java com.lotontech.mail.POPMail"+" popServer popUser popPassword");
}
System.exit(0);
}
/**
*<br>办法阐明:接纳邮件信息
*<br>输入参数:
*<br>返回范例:
*/
public static void receive(String popServer, String popUser, String popPassword){
Store store=null;
Folder folder=null;
try{
//获得默许会话
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
//利用POP3会话机制,衔接服务器
store = session.getStore("pop3");
store.connect(popServer, popUser, popPassword);
//获得默许文件夹
folder = store.getDefaultFolder();
if (folder == null) throw new Exception("No default folder");
//假如是收件箱
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();
}
}
}
/**
*<br>办法阐明:打印邮件信息
*<br>输入参数:Message message 信息对象
*<br>返回范例:
*/
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();
//附件
if (content instanceof Multipart){
messagePart=((Multipart)content).getBodyPart(0);
System.out.println("[ Multipart Message ]");
}
//获得content范例
String contentType=messagePart.getContentType();
//假如邮件内容是纯文本大概是HTML,那么打印出信息
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("-------------- END ---------------");
}catch (Exception ex){
ex.printStackTrace();
}
}
}
以上是“[JAVA100例]048、操纵JavaMail采取邮件[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |