1. /**
2. * CrazyItTest
3. * 使用JavaMail 编写接收邮件示例
4. */
5. package
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. /**
16. * @author Bill Tu(tujiyue/iwtxokhtd)
17. * May 27, 2011[10:04:20 PM]
18. *
19. */
20. public class
21. private static final String PROTOCOL = "pop3";
22. private static final String HOST = "pop3.163.com";
23. private static final String USER = "iwtxokhtd";
24. private static final String PASS="123456";
25.
26. private static
27. new
28. "mail.store.protocol", PROTOCOL);
29. "mail.pop3.host", HOST);
30.
31. Session session=Session.getDefaultInstance(props);
32. return
33. }
34.
35.
36. private static void receiveEmail() throws
37. Store store=getMailSession().getStore();
38. store.connect(HOST,USER,PASS);
39.
40. "inbox");//对于POP3协议此参数值只能是inbox
41. //设置收件夹打开后的读写权限
42.
43. int
44. "总共拥有邮件数:"+messageCount);
45.
46. if(messageCount>0){
47. //得到所有的邮件
48. for(int i=0;i<messages.length;i++){
49. Message msg=messages[i];
50. "第"+(i+1)+"封邮件的主题为:"+msg.getSubject()
51. " ,其发送人地址为:"+msg.getFrom()[0]);
52. "您是否要阅读此邮件?若要请输入yes,否则就输入no:");
53. new BufferedReader(new
54. String input=br.readLine();
55. if(input!=null && !"".equals(input)
56. "yes".equals(input.trim())){
57. //直接输出到控制台,当然也输出到文件中。
58. }
59. }
60. }
61. true);//关闭邮件夹对象
62. //断开连接
63.
64.
65. }
66.
67.
68.
69. /**
70. * @param args
71. * @throws IOException
72. * @throws MessagingException
73. */
74. public static void main(String[] args) throws
75. receiveEmail();
76. }
77. }
/** * CrazyItTest * 使用JavaMail 编写接收邮件示例 */ package com.labci.javamail.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Properties; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Store; /** * @author Bill Tu(tujiyue/iwtxokhtd) * May 27, 2011[10:04:20 PM] * */ public class ReceiveEmailTest { private static final String PROTOCOL = "pop3"; private static final String HOST = "pop3.163.com"; private static final String USER = "iwtxokhtd"; private static final String PASS="123456"; private static Session getMailSession(){ Properties props=new Properties(); props.put("mail.store.protocol", PROTOCOL); props.put("mail.pop3.host", HOST); Session session=Session.getDefaultInstance(props); return session; } private static void receiveEmail() throws MessagingException, IOException{ Store store=getMailSession().getStore(); store.connect(HOST,USER,PASS); Folder receiveFolder=store.getFolder("inbox");//对于POP3协议此参数值只能是inbox receiveFolder.open(Folder.READ_WRITE);//设置收件夹打开后的读写权限 int messageCount=receiveFolder.getMessageCount(); System.out.println("总共拥有邮件数:"+messageCount); if(messageCount>0){ Message []messages=receiveFolder.getMessages();//得到所有的邮件 for(int i=0;i<messages.length;i++){ Message msg=messages[i]; System.out.println("第"+(i+1)+"封邮件的主题为:"+msg.getSubject() +" ,其发送人地址为:"+msg.getFrom()[0]); System.out.println("您是否要阅读此邮件?若要请输入yes,否则就输入no:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String input=br.readLine(); if(input!=null && !"".equals(input) &&"yes".equals(input.trim())){ msg.writeTo(System.out);//直接输出到控制台,当然也输出到文件中。 } } } receiveFolder.close(true);//关闭邮件夹对象 store.close();//断开连接 } /** * @param args * @throws IOException * @throws MessagingException */ public static void main(String[] args) throws MessagingException, IOException { receiveEmail(); } }
标签:String,示例,电子邮件系统,private,POP3,static,import,final,邮件
From: https://blog.51cto.com/iwtxokhtd/6248076