📄 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 + -