?? j-javamail-8-4.html
字號:
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>練習 2. 如何發送第一條消息</b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr> 第 4 頁(共13 頁)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>在上一個練習中,您使用 JavaMail 實現提供的演示程序發送了一條郵件消息。在這個練習中,您將親自創建程序。</p>
<p>需要更多練習的幫助,請參閱<a href="j-javamail-8-1.html">關于練習</a>。</p>
<p>
<b>先決條件:</b>
<ul>
<li>
<a href="j-javamail-8-2.html">練習 1. 如何設置 JavaMail 環境</a>
</li>
</ul>
</p>
<p>
<b>框架代碼:</b>
<ul>
<li>
<a href="exercises/MailSending/MailExample.java">MailExample.java</a>
</li>
</ul>
</p>
<p>
<b>任務 1:</b>
<br>參照<a href="exercises/MailSending/MailExample.java">框架代碼</a>開始著手,獲取系統 <code>Properties</code>。</p>
<blockquote>
<p>
<b>任務 1 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
Properties props = System.getProperties();
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務 2:</b>
<br>將您的 SMTP 服務器名添加到 <code>mail.smtp.host</code> 關鍵字的屬性中。</p>
<blockquote>
<p>
<b>任務 2 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
props.put("mail.smtp.host", host);
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務 3:</b>
<br>獲取基于 <code>Properties</code> <code>Session</code> 對象。
</p>
<blockquote>
<p>
<b>任務 3 的幫助:</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>任務 4:</b>
<br>從 session 創建一個 <code>MimeMessage</code>。
</p>
<blockquote>
<p>
<b>任務 4 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
MimeMessage message = new MimeMessage(session);
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務 5:</b>
<br>設置消息的 <i>from</i> 域。
</p>
<blockquote>
<p>
<b>任務 5 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
message.setFrom(new InternetAddress(from));
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務 6:</b>
<br>設置消息的 <i>to</i> 域。
</p>
<blockquote>
<p>
<b>任務 6 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務 7:</b>
<br>設置消息主題。
</p>
<blockquote>
<p>
<b>任務 7 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
message.setSubject("Hello JavaMail");
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務 8:</b>
<br>設置消息內容。
</p>
<blockquote>
<p>
<b>任務 8 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
message.setText("Welcome to JavaMail");
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務 9:</b>
<br>用 <code>Transport</code> 發送消息。</p>
<blockquote>
<p>
<b>任務 9 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
Transport.send(message);
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務 10:</b>
<br>
在命令行上編譯并運行程序 — 傳遞 SMTP 服務器、 <i>from</i> 地址和 <i>to</i> 地址。</p>
<blockquote>
<p>
<b>任務 10 的幫助:</b>
<br>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
java MailExample SMTP.Server from@address to@address
</code>
</pre>
</p>
</blockquote>
<p>
<b>任務 11:</b>
<br>
用標準郵件閱讀程序(Eudora、Outlook Express、pine...)檢查,確保消息已收到。
</p>
<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="章節菜單" 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-3.html"><img alt="上頁" 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-5.html"><img alt="下頁" 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">關于 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">聯系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -