﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-HIMMEL-随笔分类-Java Rules</title><link>http://www.blogjava.net/lazing/category/12906.html</link><description>Future network: Share and Create...</description><language>zh-cn</language><lastBuildDate>Fri, 18 May 2007 02:24:17 GMT</lastBuildDate><pubDate>Fri, 18 May 2007 02:24:17 GMT</pubDate><ttl>60</ttl><item><title>Java Notes: Data types and Operators</title><link>http://www.blogjava.net/lazing/archive/2007/05/17/118189.html</link><dc:creator>HIMMEL</dc:creator><author>HIMMEL</author><pubDate>Thu, 17 May 2007 14:48:00 GMT</pubDate><guid>http://www.blogjava.net/lazing/archive/2007/05/17/118189.html</guid><wfw:comment>http://www.blogjava.net/lazing/comments/118189.html</wfw:comment><comments>http://www.blogjava.net/lazing/archive/2007/05/17/118189.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lazing/comments/commentRss/118189.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lazing/services/trackbacks/118189.html</trackback:ping><description><![CDATA[<h3>Comprehend</h3>
<ul>
    <li>A variable holds a value, while an object reference variable points to the memory that holds the object.</li>
    <li>The short-circuit logical operators &amp;&amp; and || operate only on boolean operands. For example, the expression<br>9&amp;&amp;7 will not compile. Understand the difference between short-circuit logical operators and bitwise<br>operators.</li>
    <li>If the first expression of a &amp;&amp; operator is false, the second expression is not evaluated, and if the first<br>expression of a || operator is true, the second expression is not evaluated.</li>
</ul>
<h3>Look Out</h3>
<ul>
    <li>Be on guard for words that are only slightly different from the
    Java keywords, such as synchronize instead of synchronized, implement
    instead of implements, and protect instead of protected.</li>
    <li>Whether an array stores primitive variables or object references, the array itself is always an object.</li>
    <li>It is illegal to include the array size in the array declaration.</li>
    <li>Division by zero generates a runtime ArithmeticException only if
    the operands are integers. In case of float and double, the result of
    division by zero is infinity.</li>
    <li>The result of the modulo operator always carries the sign of the first operand (i.e. the one before the operator);<br>you can ignore the sign of the second operand.</li>
    <li>You cannot instantiate an enum by using the new operator.</li>
</ul>
<h3>Memorize</h3>
<ul>
    <li>Know the Java language keywords.</li>
    <li>Know the range of values for all primitive data types.</li>
    <li>All primitive data types except boolean and char are signed.</li>
    <li>The first character of an identifier must be a letter, a dollar
    sign ($), or an underscore (_). Characters other than the first
    character in an identifier may be a letter, a dollar sign, an
    underscore, or a digit.</li>
</ul><img src ="http://www.blogjava.net/lazing/aggbug/118189.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lazing/" target="_blank">HIMMEL</a> 2007-05-17 22:48 <a href="http://www.blogjava.net/lazing/archive/2007/05/17/118189.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java Notes: Classes, Methods, and Interface</title><link>http://www.blogjava.net/lazing/archive/2007/05/17/118188.html</link><dc:creator>HIMMEL</dc:creator><author>HIMMEL</author><pubDate>Thu, 17 May 2007 14:47:00 GMT</pubDate><guid>http://www.blogjava.net/lazing/archive/2007/05/17/118188.html</guid><wfw:comment>http://www.blogjava.net/lazing/comments/118188.html</wfw:comment><comments>http://www.blogjava.net/lazing/archive/2007/05/17/118188.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lazing/comments/commentRss/118188.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lazing/services/trackbacks/118188.html</trackback:ping><description><![CDATA[<h3>Comprehend</h3>
<ul>
    <li>The method name and return type are mandatory in a method
    declaration. Even though you are not required to specify a modifier in
    a method declaration, the default modifier is assigned to the method,
    if you don&#8217;t declare one.
    </li>
    <li>A static variable belongs to the class and not to a
    particular instance of the class, and therefore is initialized when the
    class is loaded, and before the class is instantiated.
    </li>
    <li>Because a static method belongs to a class and not to a
    particular instance of the class, it cannot access the nonstatic
    methods and variables of the class in which it is defined.
    </li>
    <li>An instance of an inner class can only exist in an instance
    of the outer class, and has direct access to all the instance variables
    and methods of the outer instance.
    </li>
    <li>If you make a super call or a this call, it must be in the
    beginning of a constructor. That means you can make either a super call
    or a this call, but not both.</li>
</ul>
<h3>Look Out</h3>
<ul>
    <li>The variable-length parameters list must appear last in the
    parentheses of a method and it consists of a data type, three dots, and
    a name, in that order.
    </li>
    <li>A Java class cannot inherit from more than one class, but it can inherit from one class and one or more interfaces.
    </li>
    <li>The class that inherits from an interface must provide
    implementation for all the methods that are declared in the interface
    if the class is not abstract.
    </li>
    <li>An interface can extend another interface but it cannot implement another interface or a class.</li>
</ul>
<h3>Memorize</h3>
<ul>
    <li>If you do not provide any constructor for a class you write, the
    compiler provides the default constructor for that class. If you write
    at least one constructor for the class, the compiler provides no
    constructor.
    </li>
    <li>If you don not make a this or a super call in the beginning of a constructor, the compiler places a super() call there.
    </li>
    <li>You use the keyword extends to write a derived class that
    inherits from a parent class, and use the keyword implements to write a
    class that inherits from an interface.
    </li>
    <li>The methods in an interface are inherently public and
    abstract, and the variables in the interface are inherently public,
    final, and static.</li>
</ul><img src ="http://www.blogjava.net/lazing/aggbug/118188.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lazing/" target="_blank">HIMMEL</a> 2007-05-17 22:47 <a href="http://www.blogjava.net/lazing/archive/2007/05/17/118188.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JavaMail API 实例汇总</title><link>http://www.blogjava.net/lazing/archive/2007/05/17/118184.html</link><dc:creator>HIMMEL</dc:creator><author>HIMMEL</author><pubDate>Thu, 17 May 2007 14:30:00 GMT</pubDate><guid>http://www.blogjava.net/lazing/archive/2007/05/17/118184.html</guid><wfw:comment>http://www.blogjava.net/lazing/comments/118184.html</wfw:comment><comments>http://www.blogjava.net/lazing/archive/2007/05/17/118184.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lazing/comments/commentRss/118184.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lazing/services/trackbacks/118184.html</trackback:ping><description><![CDATA[<div class="postentry">
<p>呃，好吧，我只写过三个JavaMail的程序。</p>
<p>都是批量邮件守护程序。决定总结一下，希望非常幸运找到这篇文章的人不会再对这个困惑。</p>
<h3>必须明白的基础知识</h3>
<ol>
    <li>STMP协议是如何工作的<br>
    协议的标准在这里 <a href="http://www.ietf.org/rfc/rfc2821.txt?number=2821">http://www.ietf.org/rfc/rfc2821.txt?number=2821</a><br>
    下面是扼要说明（<a href="http://www.freesoft.org/CIE/Topics/94.htm">http://www.freesoft.org/CIE/Topics/94.htm</a> ）：
    <blockquote>
    <p>Simple Mail Transfer Protocol (SMTP), documented in RFC
    821, is Internet&#8217;s standard host-to-host mail transport protocol and
    traditionally operates over TCP, port 25. In other words, a UNIX user
    can type telnet hostname 25 and connect with an SMTP server, if one is
    present.</p>
    <p>SMTP uses a style of asymmetric request-response protocol popular in
    the early 1980s, and still seen occasionally, most often in mail
    protocols. The protocol is designed to be equally useful to either a
    computer or a human, though not too forgiving of the human. From the
    server&#8217;s viewpoint, a clear set of commands is provided and
    well-documented in the RFC. For the human, all the commands are clearly
    terminated by newlines and a HELP command lists all of them. From the
    sender&#8217;s viewpoint, the command replies always take the form of text
    lines, each starting with a three-digit code identifying the result of
    the operation, a continuation character to indicate another lines
    following, and then arbitrary text information designed to be
    informative to a human.</p>
    </blockquote>
    <p>事实上，你可以像使用DOS命令一样发送电子邮件。<a href="http://bbs.stcore.com/archiver/tid-8024.html">http://bbs.stcore.com/archiver/tid-8024.htm </a>当然因为各种原因，你的尝试不可能成功。事实上SMTP工作的时候就是简单的发送命令。取得认证，发送数据。得到反馈。确认退出这么简单。
    </p>
    </li>
    <li>SMTP中用于发送的数据<br>
    SMTP中发送的数据，遵从<font size="-1"><strong>Multipurpose Internet Mail</strong> <strong>Extensions</strong> (<strong>MIME</strong>)标准，呃，我不得不说，这是这个星球上最重要的标准之一。所有的互联网通信基本都是基于这个标准的演化。除了电子邮件，常见的应用还包括HTTP报文等（也就是所有网页了），另外即使在20年后发展的XML，其2进制数据发送仍然实用的MIME中的编码方式。<br>
    恩，这里就涉及到邮件附件如何处理的问题。恩，简单地说就是BASE64编码</font></li>
</ol>
<blockquote>
<pre>Table 1: The Base64 Alphabet<br><br>Value Encoding  Value Encoding  Value Encoding  Value Encoding</pre>
<pre>0 A            17 R            34 i            51 z<br><br>1 B            18 S            35 j            52 0<br><br>2 C            19 T            36 k            53 1<br><br>3 D            20 U            37 l            54 2<br><br>4 E            21 V            38 m            55 3<br><br>5 F            22 W            39 n            56 4<br><br>6 G            23 X            40 o            57 5<br><br>7 H            24 Y            41 p            58 6<br><br>8 I            25 Z            42 q            59 7<br><br>9 J            26 a            43 r            60 8<br><br>10 K            27 b            44 s            61 9<br><br>11 L            28 c            45 t            62 +<br><br>12 M            29 d            46 u            63 /<br><br>13 N            30 e            47 v<br><br>14 O            31 f            48 w         (pad) =<br><br>15 P            32 g            49 x<br><br>16 Q            33 h            50 y</pre>
</blockquote>
<p>在这种编码中，我们将字符或者二进制编码以6个比特位为一组，替换成相应的字符形式。比如</p>
<pre>100110111010001011101001</pre>
<p>转换结果就是</p>
<pre>100110 -&gt; 38</pre>
<pre>111010 -&gt; 58<br><br>001011 -&gt; 11<br><br>101001 -&gt; 41</pre>
<pre>38 -&gt; m<br><br>58 -&gt; 6<br><br>11 -&gt; L<br><br>41 -&gt; p</pre>
<pre>m6Lp</pre>
<p>于是，我们就可以以文本的方式编码二进制流以及扩展ASCII字符，比如中文字符。</p>
<p>基础知识完毕，下面是FAQ</p>
<h3>Java发送电子邮件需要哪些软件包</h3>
<p>mail.jar 通常还会需要 activation.jar</p>
<blockquote>
<p>下载地址<br>
http://java.sun.com/products/javabeans/jaf/downloads/index.html<br>
https://maven-repository.dev.java.net/nonav/repository/javax.mail/</p>
</blockquote>
<h3>如何发送邮件</h3>
<p>关于：</p>
<ul>
    <li>如何发送邮件</li>
    <li>如何发送带有附件的邮件</li>
    <li>如何发送中文邮件</li>
    <li>邮件中文标题乱码怎么办</li>
    <li>邮件附件乱码怎么办等等问题</li>
</ul>
<p>请查看以下代码</p>
<pre class="java">	<span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">static</span> <span style="color: blue; font-weight: bold;">synchronized</span> <span style="color: #993333;">void</span> sendMail<span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AProperties+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">Properties</span></a> settings<span style="color: #66cc66;">)</span><br>&nbsp;<br> 		<span style="color: blue; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">{</span><br>&nbsp;<br> 	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AProperties+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">Properties</span></a> props = <span style="color: blue; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AProperties+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">Properties</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	props.<span style="color: #006600;">put</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"mail.smtp.host"</span>, settings.<span style="color: #006600;">get</span><span style="color: #66cc66;">(</span>StartCore.<span style="color: #006600;">MAIL_SERVER</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	props.<span style="color: #006600;">put</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"mail.smtp.user"</span>, settings.<span style="color: #006600;">get</span><span style="color: #66cc66;">(</span>StartCore.<span style="color: #006600;">USER_NAME</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	props.<span style="color: #006600;">put</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"mail.smtp.auth"</span>, <span style="color: #ff0000;">"true"</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br><span style="color: #808080; font-style: italic;">//SMTP服务器用户验证</span><br>&nbsp;<br> 	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AAuthenticator+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">Authenticator</span></a> auth = <span style="color: blue; font-weight: bold;">new</span> SMTPAuthenticator<span style="color: #66cc66;">(</span><span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">String</span></a><span style="color: #66cc66;">)</span> settings<br>&nbsp;<br> 		.<span style="color: #006600;">get</span><span style="color: #66cc66;">(</span>StartCore.<span style="color: #006600;">USER_NAME</span><span style="color: #66cc66;">)</span>, <span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">String</span></a><span style="color: #66cc66;">)</span> settings<br>&nbsp;<br> 		.<span style="color: #006600;">get</span><span style="color: #66cc66;">(</span>StartCore.<span style="color: #006600;">PASSWORD</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	Session session = Session.<span style="color: #006600;">getDefaultInstance</span><span style="color: #66cc66;">(</span>props, auth<span style="color: #66cc66;">)</span>;<br>&nbsp;<br><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #ff0000;">"true"</span>.<span style="color: #006600;">compareToIgnoreCase</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">String</span></a><span style="color: #66cc66;">)</span> settings.<span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"DEBUG"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br>&nbsp;<br> 		session.<span style="color: #006600;">setDebug</span><span style="color: #66cc66;">(</span><span style="color: blue; font-weight: bold;">true</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	<span style="color: #66cc66;">}</span><br>&nbsp;<br><span style="color: #808080; font-style: italic;">//创建消息体</span><br>&nbsp;<br> 	MimeMessage msg = <span style="color: blue; font-weight: bold;">new</span> MimeMessage<span style="color: #66cc66;">(</span>session<span style="color: #66cc66;">)</span>;<br>&nbsp;<br><span style="color: #808080; font-style: italic;">//设置发送人邮件</span><br>&nbsp;<br> 	msg.<span style="color: #006600;">setFrom</span><span style="color: #66cc66;">(</span><span style="color: blue; font-weight: bold;">new</span> InternetAddress<span style="color: #66cc66;">(</span><span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">String</span></a><span style="color: #66cc66;">)</span> settings<br>&nbsp;<br> 		.<span style="color: #006600;">get</span><span style="color: #66cc66;">(</span>StartCore.<span style="color: #006600;">USER_MAIL</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br><span style="color: #808080; font-style: italic;">//设置接收人邮件</span><br>&nbsp;<br> 	address = <span style="color: blue; font-weight: bold;">new</span> InternetAddress<span style="color: #66cc66;">[</span><span style="color: #66cc66;">]</span> <span style="color: #66cc66;">{</span> <span style="color: blue; font-weight: bold;">new</span> InternetAddress<span style="color: #66cc66;">(</span>rs<br>&nbsp;<br> 		.<span style="color: #006600;">getString</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"GRE_mail"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">}</span>;<br>&nbsp;<br> 	msg.<span style="color: #006600;">setRecipients</span><span style="color: #66cc66;">(</span>Message.<span style="color: #006600;">RecipientType</span>.<span style="color: #006600;">TO</span>, address<span style="color: #66cc66;">)</span>;<br>&nbsp;<br><span style="color: #808080; font-style: italic;">//设置主题，中文编码</span><br>&nbsp;<br> 	msg.<span style="color: #006600;">setSubject</span><span style="color: #66cc66;">(</span>subject, <span style="color: #ff0000;">"gbk"</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	msg.<span style="color: #006600;">setSentDate</span><span style="color: #66cc66;">(</span><span style="color: blue; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">Date</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">String</span></a> content = <span style="color: #ff0000;">"邮件正文"</span>;<br>&nbsp;<br> 	MimeBodyPart mbp1 = <span style="color: blue; font-weight: bold;">new</span> MimeBodyPart<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	mbp1.<span style="color: #006600;">setText</span><span style="color: #66cc66;">(</span>content, <span style="color: #ff0000;">"gbk"</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br><span style="color: #808080; font-style: italic;">//邮件附件</span><br>&nbsp;<br>MimeBodyPart attachFilePart = <span style="color: blue; font-weight: bold;">new</span> MimeBodyPart<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFile+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">File</span></a> file = <span style="color: blue; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFile+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">File</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"中文附件.txt"</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br>FileDataSource fds = <span style="color: blue; font-weight: bold;">new</span> FileDataSource<span style="color: #66cc66;">(</span>file.<span style="color: #006600;">getName</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br>attachFilePart.<span style="color: #006600;">setDataHandler</span><span style="color: #66cc66;">(</span><span style="color: blue; font-weight: bold;">new</span> DataHandler<span style="color: #66cc66;">(</span>fds<span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	sun.<span style="color: #006600;">misc</span>.<span style="color: #006600;">BASE64Encoder</span> enc = <span style="color: blue; font-weight: bold;">new</span> sun.<span style="color: #006600;">misc</span>.<span style="color: #006600;">BASE64Encoder</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	<span style="color: #808080; font-style: italic;">//解决中文附件名称</span><br>&nbsp;<br> 	attachFilePart.<span style="color: #006600;">setFileName</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"=?gbk?B?"</span><br>&nbsp;<br> 		+ enc.<span style="color: #006600;">encode</span><span style="color: #66cc66;">(</span>file.<span style="color: #006600;">getName</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>.<span style="color: #006600;">getBytes</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"gbk"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> + <span style="color: #ff0000;">"?="</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br>Multipart mp = <span style="color: blue; font-weight: bold;">new</span> MimeMultipart<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	mp.<span style="color: #006600;">addBodyPart</span><span style="color: #66cc66;">(</span>mbp1<span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	mp.<span style="color: #006600;">addBodyPart</span><span style="color: #66cc66;">(</span>attachFilePart<span style="color: #66cc66;">)</span>;<br>&nbsp;<br>msg.<span style="color: #006600;">setContent</span><span style="color: #66cc66;">(</span>mp<span style="color: #66cc66;">)</span>;<br>&nbsp;<br><span style="color: #808080; font-style: italic;">// send the message</span><br>&nbsp;<br> 	msg.<span style="color: #006600;">saveChanges</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	Transport.<span style="color: #006600;">send</span><span style="color: #66cc66;">(</span>msg<span style="color: #66cc66;">)</span>;<br>&nbsp;<br><span style="color: #66cc66;">}</span></pre>
<p>这是上面用户验证用到的类</p>
<pre class="java"><span style="color: blue; font-weight: bold;">class</span> SMTPAuthenticator <span style="color: blue; font-weight: bold;">extends</span> javax.<span style="color: #006600;">mail</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AAuthenticator+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">Authenticator</span></a> <span style="color: #66cc66;">{</span></pre>
<pre class="java">	<span style="color: blue; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">String</span></a> username;<br>&nbsp;<br>	<span style="color: blue; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">String</span></a> password;<br>&nbsp;<br>	<span style="color: #808080; font-style: italic;">/**<br>&nbsp;<br>  	* @param username<br>&nbsp;<br> 	* @param password<br>&nbsp;<br>  	*/</span><br>&nbsp;<br> 	<span style="color: blue; font-weight: bold;">public</span> SMTPAuthenticator<span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">String</span></a> username, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">String</span></a> password<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br>&nbsp;<br> 		<span style="color: blue; font-weight: bold;">this</span>.<span style="color: #006600;">username</span> = username;<br>&nbsp;<br> 		<span style="color: blue; font-weight: bold;">this</span>.<span style="color: #006600;">password</span> = password;<br>&nbsp;<br>	<span style="color: #66cc66;">}</span><br>&nbsp;<br>	<span style="color: blue; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APasswordAuthentication+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">PasswordAuthentication</span></a> getPasswordAuthentication<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br>&nbsp;<br> 		<span style="color: blue; font-weight: bold;">return</span> <span style="color: blue; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APasswordAuthentication+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="color: red; font-weight: bold;">PasswordAuthentication</span></a><span style="color: #66cc66;">(</span>username, password<span style="color: #66cc66;">)</span>;<br>&nbsp;<br> 	<span style="color: #66cc66;">}</span><br>&nbsp;<br><span style="color: #66cc66;">}</span></pre>
<h3>邮件发送出错</h3>
<blockquote>
<p>What causes an
&#8220;javax.activation.UnsupportedDataTypeException: no object DCH for MIME
type xxx/xxxx javax.mail.MessagingException: IOException while sending
message;&#8221; to be sent and how do I fix this? [This happens for known
MIME types like text/htm</p>
</blockquote>
<p>事实上这个是邮件发送时验证组件设置不当引起的，这个组件配置方法如下</p>
<blockquote>
<p>(http://java.sun.com/j2ee/1.4/docs/api/javax/activation/MailcapCommandMap.html)</p>
<p>The MailcapCommandMap looks in various places in the user&#8217;s system
for mailcap file entries. When requests are made to search for commands
in the MailcapCommandMap, it searches mailcap files in the following
order:</p>
<p>1) Programatically added entries to the MailcapCommandMap instance.<br>
2) The file .mailcap in the user&#8217;s home directory.<br>
3) The file /lib/mailcap.<br>
4) The file or resources named META-INF/mailcap.<br>
5) The file or resource named META-INF/mailcap.default (usually found only in the activation.jar file).</p>
</blockquote>
<p>我选用了第四种方法，在生成的Jar文件中加入了 META-INF/mailcap.</p>
<pre class="apache"><span style="color: #adadad; font-style: italic;">#</span><br><span style="color: #adadad; font-style: italic;"># This is a very simple 'mailcap' file</span><br><span style="color: #adadad; font-style: italic;">#</span><br>image/gif;;		x-java-view=com.sun.activation.viewers.ImageViewer<br>image/jpeg;;		x-java-view=com.sun.activation.viewers.ImageViewer<br>text/*;;		x-java-view=com.sun.activation.viewers.TextViewer<br>text/*;;		x-java-edit=com.sun.activation.viewers.TextEditor<br>text/html;; 	x-java-content-handler=com.sun.mail.handlers.text_html<br>text/xml;; 		x-java-content-handler=com.sun.mail.handlers.text_xml<br>text/plain;; 	x-java-content-handler=com.sun.mail.handlers.text_plain<br>multipart/*;; 	x-java-content-handler=com.sun.mail.handlers.multipart_mixed<br>message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822</pre>
</div><img src ="http://www.blogjava.net/lazing/aggbug/118184.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lazing/" target="_blank">HIMMEL</a> 2007-05-17 22:30 <a href="http://www.blogjava.net/lazing/archive/2007/05/17/118184.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java IO note 1</title><link>http://www.blogjava.net/lazing/archive/2007/01/03/91592.html</link><dc:creator>HIMMEL</dc:creator><author>HIMMEL</author><pubDate>Wed, 03 Jan 2007 09:25:00 GMT</pubDate><guid>http://www.blogjava.net/lazing/archive/2007/01/03/91592.html</guid><wfw:comment>http://www.blogjava.net/lazing/comments/91592.html</wfw:comment><comments>http://www.blogjava.net/lazing/archive/2007/01/03/91592.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lazing/comments/commentRss/91592.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lazing/services/trackbacks/91592.html</trackback:ping><description><![CDATA[<p>
				<span style="font-size: 20px; font-family: 'Courier New',Courier,monospace;">
						<strong>Return value of read()</strong>
				</span>
		</p>
		<p>For instance, according to the Java class library documentation, the <tt>read( )</tt> method of <tt>java.io.InputStream</tt> returns "the next byte of data, or -1 if the end of the stream is reached." Upon reflection, this sounds suspicious. How is a -1 that appears as part of the stream data to be distinguished from a -1 indicating end of stream? In point of fact, the <tt>read( )</tt> method does not return a <tt>byte</tt>; its signature shows that it returns an <tt>int</tt>:<code></code></p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
				<span style="color: rgb(0, 128, 128);">1</span>
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">abstract</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> read( ) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException</span>
		</div>
		<p>This <tt>int</tt> is not a Java <tt>byte</tt> with a value between -128 and 127 but a more general unsigned byte with a value between 0 and 255. Hence, -1 can easily be distinguished from valid data values read from the stream.</p>
		<p>
				<br />
		</p>
		<p>
				<span style="font-size: 20px; font-family: 'Courier New',Courier,monospace;">
						<strong>JVM and byte, byte array</strong>
				</span>
		</p>
		<p>In fact, a single <tt>byte</tt> still takes up four bytes of space inside the Java virtual machine, but a <tt>byte</tt> array occupies only the amount of space it actually needs. The virtual machine includes special instructions for operating on <tt>byte</tt> arrays but does not include any instructions for operating on single <tt>byte</tt>s. They're just promoted to <tt>int</tt>s.</p>
		<p>
				<br />
		</p>
		<p>
				<span style="font-family: 'Courier New',Courier,monospace;">
						<strong style="font-size: 20px;">Input, Output, Reader and Writer</strong>
				</span>
		</p>
		<p class="docText">For the most part, these classes have methods that are extremely similar to the equivalent stream classes. Often the only difference is that a <tt>byte</tt> in the signature of a stream method becomes a <tt>char</tt> in the signature of the matching reader or writer method. For example, the <tt>java.io.OutputStream</tt> class declares these three <tt>write( )</tt> methods:</p>
		<pre xml:space="preserve">
				<br />
		</pre>
		<p>The <tt>java.io.Writer</tt> class, therefore, declares these three <tt>write( )</tt> methods:</p>
		<pre xml:space="preserve">
				<br />
		</pre>
		<p>As you can see, the signatures match except that in the latter two methods the <tt>byte</tt> array <tt>data</tt> has changed to a <tt>char</tt> array. There's also a less obvious difference not reflected in the signature. While the <tt>int</tt> passed to the <tt>OutputStream write( )</tt> method is reduced modulo 256 before being output, the <tt>int</tt> passed to the <tt>Writer write( )</tt> method is reduced modulo 65,536. This reflects the different ranges of <tt>char</tt>s and <tt>byte</tt>s.</p>
		<p class="docText">
				<tt>java.io.Writer</tt> also has two more <tt>write( )</tt> methods that take their data from a string:</p>
		<pre xml:space="preserve">
				<code>public void write(String s) <br />throws IOException<br />public void write(String s, int offset, int length) throws IOException<br /></code>
		</pre>
		<p>Because streams don't know how to deal with character-based data, there are no corresponding methods in the <tt>java.io.OutputStream</tt> class.</p>
		<p>
				<br />
		</p>
		<p>
				<span style="font-size: 20px; font-family: 'Courier New',Courier,monospace;">
						<strong>Hello World!</strong>
				</span>
		</p>
		<p> </p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
				<span style="color: rgb(0, 128, 128);">1   </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);">[] hello </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span id="Codehighlighter1_15_77_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">
				</span>
				<span id="Codehighlighter1_15_77_Open_Text">
						<span style="color: rgb(0, 0, 0);">{</span>
						<span style="color: rgb(0, 0, 0);">72</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">101</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">108</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">108</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">111</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">32</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">87</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">111</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">114</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">108</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">100</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">33</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 0);">10</span>
						<span style="color: rgb(0, 0, 0);">,</span>
						<span style="color: rgb(0, 0, 0);">13</span>
						<span style="color: rgb(0, 0, 0);">}</span>
				</span>
				<span style="color: rgb(0, 0, 0);">;<br /></span>
				<span style="color: rgb(0, 128, 128);">2</span>
				<span style="color: rgb(0, 0, 0);">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />System.out.write(hello);</span>
		</div>
		<p>
				<span class="ztags">
						<span class="ztagspace">Del.icio.us</span> : <a class="ztag" href="http://del.icio.us/tag/io" rel="tag">io</a>, <a class="ztag" href="http://del.icio.us/tag/java" rel="tag">java</a></span>
		</p>
		<p class="poweredbyzoundry">Powered by <a class="poweredbyzoundry_link" href="http://www.zoundry.com" rel="nofollow">Zoundry</a></p><img src ="http://www.blogjava.net/lazing/aggbug/91592.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lazing/" target="_blank">HIMMEL</a> 2007-01-03 17:25 <a href="http://www.blogjava.net/lazing/archive/2007/01/03/91592.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>