你可以用Javamail API中的Stor与Folder类来读取消息,下列程序清单在我的机器上通过,供你参考。
// MessageView.javaimport javax.mail.*;public class MessageView { public static void main(String[] args) {
/*if (args.length != 4) {
System.out.println("Usage: java MessageView <pop3host> <user> <password> <msgnum>");
System.exit(1);
}*/ String host = "192.168.123.186";
String user = "gubaohua3";
String password = "111111";
int msgNum = 1; try {
// Step 1: Configure the mail session
System.out.println("Configuring mail session for: " + host);
java.util.Properties props = new java.util.Properties();
props.put("mail.pop3.host", host);
Session mailSession = Session.getDefaultInstance(props); // Step 2: Retrieve and connect to the Store
System.out.println("Connecting to message store: " + host);
Store msgStore = mailSession.getStore("pop3");
msgStore.connect(host, user, password);
System.out.println("Connected!"); // Step 3: Retrieve the INBOX folder
Folder inbox = msgStore.getDefaultFolder().getFolder("INBOX");
inbox.open(Folder.READ_ONLY); // Step 4: Display the subject and date for each message
if (msgNum <= inbox.getMessageCount()) {
Message theMessage = inbox.getMessage(msgNum);
theMessage.writeTo(System.out);
}
else {
System.out.println("Message number not found");
} // Step 5: Close up shop
inbox.close(false);
msgStore.close();
}
catch (Exception exc) {
exc.printStackTrace();
}
}
}
// MessageView.javaimport javax.mail.*;public class MessageView { public static void main(String[] args) {
/*if (args.length != 4) {
System.out.println("Usage: java MessageView <pop3host> <user> <password> <msgnum>");
System.exit(1);
}*/ String host = "192.168.123.186";
String user = "gubaohua3";
String password = "111111";
int msgNum = 1; try {
// Step 1: Configure the mail session
System.out.println("Configuring mail session for: " + host);
java.util.Properties props = new java.util.Properties();
props.put("mail.pop3.host", host);
Session mailSession = Session.getDefaultInstance(props); // Step 2: Retrieve and connect to the Store
System.out.println("Connecting to message store: " + host);
Store msgStore = mailSession.getStore("pop3");
msgStore.connect(host, user, password);
System.out.println("Connected!"); // Step 3: Retrieve the INBOX folder
Folder inbox = msgStore.getDefaultFolder().getFolder("INBOX");
inbox.open(Folder.READ_ONLY); // Step 4: Display the subject and date for each message
if (msgNum <= inbox.getMessageCount()) {
Message theMessage = inbox.getMessage(msgNum);
theMessage.writeTo(System.out);
}
else {
System.out.println("Message number not found");
} // Step 5: Close up shop
inbox.close(false);
msgStore.close();
}
catch (Exception exc) {
exc.printStackTrace();
}
}
}
解决方案 »
- 求教:你们有遇到过在本地样式没问题,上传到服务器之后样式加载不出来的问题吗?
- hibernate+oracle 应该用什么主键生成策略?
- .net与java的区别
- EJB独立工作能力讨论
- 这个问题不给分的 不过你们会有N 多好处的
- MyEclipse6中存放jar问题
- netbean开发j2ee需要安装什么,如何配置?
- 在struts表单标签中有一个属性叫property应该是用来与actionform的属性连接的,html表单标签中有没有类似的属性呢?
- 浏览器(jsp页面)数据可以导入到excel中吗??请大侠指教。,再线等。。
- Struts 表单检测不能正确显示其数据填写错误
- 请问,jbuilder中光标不跟随应该怎么解决?
- 救救小弟,请问:JavaMail中javax.activation包的DataHandler类在哪里有??
楼主的邮件头是个啥啥想法?