?? j-javamail-8-6.html
字號(hào):
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>在這個(gè)練習(xí)中,我們創(chuàng)建了一個(gè)程序,它能夠顯示每條消息的 <i>from</i> 地址和主題,并提示消息內(nèi)容的顯示。</p>
<p>需要更多練習(xí)的幫助,請(qǐng)參閱<a href="j-javamail-8-1.html">關(guān)于練習(xí)</a>.</p>
<p>
<b>先決條件:</b>
<ul>
<li>
<a href="j-javamail-8-2.html">練習(xí) 1. 如何設(shè)置 JavaMail 環(huán)境</a>
</li>
</ul>
</p>
<p>
<b>框架代碼</b>
<ul>
<li>
<a href="exercises/MailGetting/GetMessageExample.java">GetMessageExample.java</a>
</li>
</ul>
</p>
<p>
<b>任務(wù) 1:</b>
<br>參考<a href="exercises/MailGetting/GetMessageExample.java">框架代碼</a>開始著手,獲取(或說創(chuàng)建)一個(gè) <code>Properties</code> 對(duì)象。</p>
<blockquote>
<p>
<b>任務(wù) 1 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
Properties props = new Properties();
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 2:</b>
<br>獲取基于 <code>Properties</code> 的 <code>Session</code> 對(duì)象。</p>
<blockquote>
<p>
<b>任務(wù) 2 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
Session session = Session.getDefaultInstance(props, null);
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 3:</b>
<br>獲取電子郵件協(xié)議 <code>pop3</code> 或 <code>imap</code>的 <code>Store</code>。</p>
<blockquote>
<p>
<b>任務(wù) 3 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
Store store = session.getStore("pop3");
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 4:</b>
<br>用恰當(dāng)?shù)挠脩裘兔艽a連接到您的郵件主機(jī)的 store。</p>
<blockquote>
<p>
<b>任務(wù) 4 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
store.connect(host, username, password);
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 5:</b>
<br>獲取您要讀的 folder。很可能會(huì)是 <code>INBOX</code>。</p>
<blockquote>
<p>
<b>任務(wù) 5 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
Folder folder = store.getFolder("INBOX");
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 6:</b>
<br>以只讀方式打開 folder。</p>
<blockquote>
<p>
<b>任務(wù) 6 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
folder.open(Folder.READ_ONLY);
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 7:</b>
<br>獲取一個(gè) folder 中的消息目錄。將消息列表保存在名為 <code>message</code> 的數(shù)組變量中。</p>
<blockquote>
<p>
<b>任務(wù) 7 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
Message message[] = folder.getMessages();
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 8:</b>
<br>
顯示每條消息的 <i>from</i> 域和主題。</p>
<blockquote>
<p>
<b>任務(wù) 8 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
System.out.println(i + ": " + message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 9:</b>
<br>在提示時(shí)顯示消息內(nèi)容。</p>
<blockquote>
<p>
<b>任務(wù) 9 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
System.out.println(message[i].getContent());
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 10:</b>
<br>關(guān)閉到 folder 和 store 的連接。</p>
<blockquote>
<p>
<b>任務(wù) 10 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
folder.close(false);
store.close();
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務(wù) 11:</b>
<br>
在命令行上編譯并運(yùn)行程序 — 傳遞 SMTP 服務(wù)器、用戶名和密碼。對(duì)于想要閱讀的消息,以 YES 響應(yīng)。反之,敲擊 <code>ENTER</code>。如果想在讀完所有消息前中止,可以輸入 QUIT。</p>
<blockquote>
<p>
<b>任務(wù) 11 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
java GetMessageExample POP.Server username password
</code>
</pre>
</p>
</blockquote>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜單" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index8.html"><img alt="章節(jié)菜單" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-javamail-9-3.html"><img alt="給出此教程的反饋意見" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-javamail-8-5.html"><img alt="上頁(yè)" border="0" src="../i/previous.gif" name="bottomprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-javamail-8-7.html"><img alt="下頁(yè)" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">關(guān)于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隱私條約</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律條款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">聯(lián)系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
</body>
</html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -