﻿<?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-民工的IT生活</title><link>http://www.blogjava.net/monster/</link><description>本人不是什么编程专家,只是ctrl+c,ctrl+v别人的成果而已</description><language>zh-cn</language><lastBuildDate>Sun, 12 Apr 2026 05:59:49 GMT</lastBuildDate><pubDate>Sun, 12 Apr 2026 05:59:49 GMT</pubDate><ttl>60</ttl><item><title>通过开源代码EasySMTPConnection了解SMTP工作流程,sun公司的实现中屏蔽了这个流程</title><link>http://www.blogjava.net/monster/archive/2007/06/15/124502.html</link><dc:creator>monster</dc:creator><author>monster</author><pubDate>Fri, 15 Jun 2007 06:52:00 GMT</pubDate><guid>http://www.blogjava.net/monster/archive/2007/06/15/124502.html</guid><description><![CDATA[现贴出主要类,主要流程都在这个类中:<br>
package com.blankenhorn.net.mail;<br>
<br>
import java.io.*;<br>
import java.net.InetAddress;<br>
import java.net.Socket;<br>
import java.net.SocketTimeoutException;<br>
import java.util.Date;<br>
import java.util.StringTokenizer;<br>
<br>
<br>
/**<br>
&nbsp;* Provides a simple way to send emails to an SMTP server.<br>
&nbsp;* <br>
&nbsp;* @author Kai Blankenhorn &amp;lt;&lt;a href="mailto:pub01@bitfolge.de"&gt;pub01@bitfolge.de&lt;/a&gt;&amp;gt;<br>
&nbsp;*/<br>
public class EasySMTPConnection {<br>
<br>
&nbsp;&nbsp;&nbsp; private String server = null;<br>
&nbsp;&nbsp;&nbsp; private BufferedReader in = null;<br>
&nbsp;&nbsp;&nbsp; private BufferedWriter out = null;<br>
&nbsp;&nbsp;&nbsp; private Socket socket = null;<br>
<br>
&nbsp;&nbsp;&nbsp; /**<br>
&nbsp;&nbsp;&nbsp; &nbsp;* Usage example.<br>
&nbsp;&nbsp;&nbsp; &nbsp;*/<br>
&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EasySMTPConnection smtp = new EasySMTPConnection("127.0.0.1");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Message msg = new Message();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; msg.setTo("");<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String[] source = {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "From: Test User &lt;test.user@foo.bar&gt;", <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; "To: Kai Blankenhorn &lt;pub01@bitfolge.de&gt;,
Somebody &lt;somebodysaddress@somebodysserver.com&gt;", <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "Subject: EasySMTPConnection Test",<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "Date: insertdate", <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "Content-Type: text/plain; charset=iso-8859-1", <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // you may set the message ID, but you don't have to<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; // "Message-ID:
"+EasySMTPConnection.createMessageID("yourserver.com"), <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "",<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "first line,",<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "second line",<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "as you can see, no need for newlines (\\n)", <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ""<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; };<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; msg = new Message(source);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; smtp.sendMessage(msg);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch(MailProtocolException e) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.printStackTrace();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;catch(InstantiationException e) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.printStackTrace();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; /**<br>
&nbsp;&nbsp;&nbsp; &nbsp;* Creates a unique message ID for an email message.<br>
&nbsp;&nbsp;&nbsp; &nbsp;* <br>
&nbsp;&nbsp;&nbsp; &nbsp;* @param hostName the internet name of the computer the mail is being sent from<br>
&nbsp;&nbsp;&nbsp; &nbsp;*/<br>
&nbsp;&nbsp;&nbsp; public static String createMessageID(String hostName) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String msgID = new Date().getTime() + ".";<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; msgID += Thread.currentThread().hashCode();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; msgID += hostName;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; msgID = "&lt;"+msgID+"&gt;";<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return msgID;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; /**<br>
&nbsp;&nbsp;&nbsp; &nbsp;* Establishes a connection to the specified SMTP server.<br>
&nbsp;&nbsp;&nbsp; &nbsp;* <br>
&nbsp;&nbsp;&nbsp; &nbsp;* @param server the SMTP server to connect to<br>
&nbsp;&nbsp;&nbsp; &nbsp;*/<br>
&nbsp;&nbsp;&nbsp; public EasySMTPConnection(String server) throws InstantiationException {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.server = server;<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.open();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch(SocketTimeoutException e) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; throw new InstantiationException("Timeout: " + e.getMessage());<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;catch(IOException e) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; throw new InstantiationException("IO error: " + e.getMessage());<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; /**<br>
&nbsp;&nbsp;&nbsp; &nbsp;* Opens the connection to the server stored in &lt;code&gt;server&lt;/code&gt;.<br>
&nbsp;&nbsp;&nbsp; &nbsp;*/<br>
&nbsp;&nbsp;&nbsp; protected void open() throws SocketTimeoutException, IOException {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; socket = new Socket(server, 25);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; socket.setSoTimeout(5000);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; socket.setKeepAlive(true);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; in = new BufferedReader(new InputStreamReader(socket.getInputStream()));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String response = read();<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(!response.startsWith("220")) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; throw new IOException(response);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln("HELO " + InetAddress.getByName(InetAddress.getLocalHost().getHostAddress())<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; .getHostName());<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; response = read();<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(!response.startsWith("2")) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; throw new IOException(response);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; /**<br>
&nbsp;&nbsp;&nbsp; &nbsp;* Close the connection.<br>
&nbsp;&nbsp;&nbsp; &nbsp;*/<br>
&nbsp;&nbsp;&nbsp; public void close() {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln("QUIT");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; socket.close();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch(SocketTimeoutException e) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;catch(IOException e) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; private String read() throws SocketTimeoutException, IOException {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String line = in.readLine();<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return line;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; private void writeln(String line) throws SocketTimeoutException, IOException {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; out.write(line);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; out.newLine();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; out.flush();<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; /**<br>
&nbsp;&nbsp;&nbsp; &nbsp;* Reads the server's response and checks the response code.<br>
&nbsp;&nbsp;&nbsp; &nbsp;* <br>
&nbsp;&nbsp;&nbsp; &nbsp;* @throws MailProtocolException if the code from the server is an error code (4xx or 5xx)<br>
&nbsp;&nbsp;&nbsp; &nbsp;* @throws SocketTimeoutException if there was a timeout while reading from the server<br>
&nbsp;&nbsp;&nbsp; &nbsp;* @throws IOException if there was some sort of network error<br>
&nbsp;&nbsp;&nbsp; &nbsp;*/<br>
&nbsp;&nbsp;&nbsp; protected void checkResponse() throws MailProtocolException, SocketTimeoutException, IOException {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String response = read();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(response.startsWith("4") || response.startsWith("5")) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; throw new MailProtocolException(response);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; /**<br>
&nbsp;&nbsp;&nbsp; &nbsp;* Sends an array of Strings to the server. This method just constructs a new Message object<br>
&nbsp;&nbsp;&nbsp; &nbsp;* based on &lt;code&gt;msgSource&lt;/code&gt; and then calls {@link #sendMessage(Message)}<br>
&nbsp;&nbsp;&nbsp; &nbsp;* <br>
&nbsp;&nbsp;&nbsp; &nbsp;* @param msgSource An array of Strings, each element containing one line of the message source.<br>
&nbsp;&nbsp;&nbsp; &nbsp;* &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Note that
there has to be an empty line to seperate header and body of the
message.<br>
&nbsp;&nbsp;&nbsp; &nbsp;* &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; You don't have
to (and you're not able to) end your message with a "." line, though.<br>
&nbsp;&nbsp;&nbsp; &nbsp;*/<br>
&nbsp;&nbsp;&nbsp; public void send(String[] msgSource) throws MailProtocolException {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Message msg = new Message(msgSource);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.sendMessage(msg);<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; /**<br>
&nbsp;&nbsp;&nbsp; &nbsp;* Sends a Message through the server corresponding to this instance of EasySMTPConnection.<br>
&nbsp;&nbsp;&nbsp; &nbsp;* <br>
&nbsp;&nbsp;&nbsp; &nbsp;* @param msg the Message to send<br>
&nbsp;&nbsp;&nbsp; &nbsp;* @throws MailProtocolException if the server replied an error to one of the commands<br>
&nbsp;&nbsp;&nbsp; &nbsp;*/<br>
&nbsp;&nbsp;&nbsp; public void sendMessage(Message msg) throws MailProtocolException {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (msg.getMessageID()==null || msg.getMessageID().equals("")) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; msg.setMessageID(EasySMTPConnection.createMessageID("yourserver.com"));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; socket.setSoTimeout(10000);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln("MAIL FROM:" + extractEmail(msg.getFrom()));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; checkResponse();<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; StringTokenizer t = new StringTokenizer(msg.getTo(), ";,");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while(t.hasMoreTokens()) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln("RCPT TO:" + extractEmail(t.nextToken()));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; checkResponse();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; t = new StringTokenizer(msg.getCC(), ";,");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while(t.hasMoreTokens()) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln("RCPT TO:" + extractEmail(t.nextToken()));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; checkResponse();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; t = new StringTokenizer(msg.getBCC(), ";,");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while(t.hasMoreTokens()) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln("RCPT TO:" + extractEmail(t.nextToken()));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; checkResponse();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln("DATA");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; checkResponse();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for(int i = 0; i &lt; msg.getHeader().length; i++) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln(encodeDot(msg.getHeader()[i]));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
writeln("X-Sent-Through: EasySMTPConnection Java class
(http://www.bitfolge.de/en)");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln("");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for(int i = 0; i &lt; msg.getBody().length; i++) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln(encodeDot(msg.getBody()[i]));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; writeln(".");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; checkResponse();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch(IOException io) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; throw new MailProtocolException(io);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; protected String extractEmail(String nameAndEmail) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String result = nameAndEmail;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(nameAndEmail.indexOf('&lt;') &gt; -1) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; result =
nameAndEmail.substring(nameAndEmail.indexOf('&lt;') + 1,
nameAndEmail.indexOf('&gt;'));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return result;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; protected String encodeDot(String line) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String result = line;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(line.startsWith(".")) {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; result = "." + result;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return result;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; /**<br>
&nbsp;&nbsp;&nbsp; &nbsp;* Closes the connection to the server upon finalization.<br>
&nbsp;&nbsp;&nbsp; &nbsp;*/<br>
&nbsp;&nbsp;&nbsp; public void finalize() {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.close();<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br><img src ="http://www.blogjava.net/monster/aggbug/124502.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/monster/" target="_blank">monster</a> 2007-06-15 14:52 <a href="http://www.blogjava.net/monster/archive/2007/06/15/124502.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一些JFreeChart设置</title><link>http://www.blogjava.net/monster/archive/2007/06/15/124499.html</link><dc:creator>monster</dc:creator><author>monster</author><pubDate>Fri, 15 Jun 2007 06:47:00 GMT</pubDate><guid>http://www.blogjava.net/monster/archive/2007/06/15/124499.html</guid><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;只有注册用户登录后才能阅读该文。<a href='http://www.blogjava.net/monster/archive/2007/06/15/124499.html'>阅读全文</a><img src ="http://www.blogjava.net/monster/aggbug/124499.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/monster/" target="_blank">monster</a> 2007-06-15 14:47 <a href="http://www.blogjava.net/monster/archive/2007/06/15/124499.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>log4j配置文件的位置(class文件的根目录下,或者源文件根根目录下)</title><link>http://www.blogjava.net/monster/archive/2007/06/15/124493.html</link><dc:creator>monster</dc:creator><author>monster</author><pubDate>Fri, 15 Jun 2007 06:32:00 GMT</pubDate><guid>http://www.blogjava.net/monster/archive/2007/06/15/124493.html</guid><description><![CDATA[log4j配置文件的位置应该放在class文件的根目录下(或者源文件根根目录下),不要放在子目录下,否则会由于找不到log4j配置文件而出错.<br>
一个quartz用的log4j配置文件内容如下:<br>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br>
&lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;<br>
<br>
&lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;<br>
<br>
&nbsp; &lt;appender name="default" class="org.apache.log4j.ConsoleAppender"&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;param name="target" value="System.out"/&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;layout class="org.apache.log4j.PatternLayout"&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c]%n%m%n%n"/&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;/layout&gt;<br>
&nbsp; &lt;/appender&gt;<br>
<br>
<br>
&nbsp;&lt;logger name="org.quartz"&gt;<br>
&nbsp;&nbsp; &lt;level value="debug" /&gt;<br>
&nbsp;&lt;/logger&gt;<br>
<br>
&nbsp; &lt;root&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;level value="debug" /&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;appender-ref ref="default" /&gt;<br>
&nbsp; &lt;/root&gt;<br>
<br>
&nbsp; <br>
&lt;/log4j:configuration&gt;<br><br><br>#DEBUG用来定义输出级别，比DEBUG高的都可以输出，后面的stdout，File为输出日志的输出地方<br>log4j.rootLogger=DEBUG, stdout,FILE<br>#定义stdout输出的格式<br>log4j.appender.stdout=org.apache.log4j.ConsoleAppender<br>log4j.appender.stdout.layout=org.apache.log4j.PatternLayout<br>log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n<br>#定义文件输出的格式<br>log4j.appender.FILE=org.apache.log4j.FileAppender<br>log4j.appender.FILE.File=d:\\log.txt<br>log4j.appender.FILE.Append=true<br>log4j.appender.FILE.layout=org.apache.log4j.PatternLayout<br>log4j.appender.FILE.layout.ConversionPattern=[send mail info] %d - %c -%-4r [%t] %-5p %c %x - %m%n<br><br><br><br>#其中，Log4j提供的appender有以下几种：&nbsp; <br>#org.apache.log4j.ConsoleAppender（控制台），&nbsp; <br>#org.apache.log4j.FileAppender（文件），&nbsp; <br>#org.apache.log4j.DailyRollingFileAppender（每天产生一个日志文件），&nbsp; <br>#org.apache.log4j.RollingFileAppender（文件大小到达指定尺寸的时候产生一个新的文件），&nbsp; <br>#org.apache.log4j.WriterAppender（将日志信息以流格式发送到任意指定的地方）&nbsp; <br>&nbsp;<br>#其中，Log4j提供的layout有以下几种：&nbsp; <br>#org.apache.log4j.HTMLLayout（以HTML表格形式布局），&nbsp; <br>#org.apache.log4j.PatternLayout（可以灵活地指定布局模式），&nbsp; <br>#org.apache.log4j.SimpleLayout（包含日志信息的级别和信息字符串），&nbsp; <br>#org.apache.log4j.TTCCLayout（包含日志产生的时间、线程、类别等等信息）&nbsp; <br>&nbsp; <br># 下面是步骤，共10步 <br><br>#1 定义了两个输出端<br>#log4j.rootLogger = debug, A1, A2<br>#2 定义A1输出到控制器<br>#log4j.appender.A1 = org.apache.log4j.ConsoleAppender<br>#3 定义A1的布局模式为PatternLayout<br>#log4j.appender.A1.layout = org.apache.log4j.PatternLayout<br>#4 定义A1的输出格式<br>#log4j.appender.A1.layout.ConversionPattern = %-4r [%t] %-5p %c - %m%n<br>#5 定义A2输出到文件<br>#log4j.appender.A2 = org.apache.log4j.RollingFileAppender<br>#6 定义A2要输出到哪一个文件<br>#log4j.appender.A2.File = D:\\hello.log<br>#7 定义A2的输出文件的最大长度<br>#log4j.appender.A2.MaxFileSize = 1KB<br>#8 定义A2的备份文件数<br>#log4j.appender.A2.MaxBackupIndex = 3<br>#9 定义A2的布局模式为PatternLayout<br>#log4j.appender.A2.layout = org.apache.log4j.PatternLayout<br>#10 定义A2的输出格式<br>#log4j.appender.A2.layout.ConversionPattern = %d{yyyy-MM-dd hh:mm:ss}:%p %t %c<br>&nbsp;&nbsp; <br>&nbsp;&nbsp; <br>#log4j.appender.R=org.apache.log4j.RollingFileAppender&nbsp;&nbsp; //指定以文件的方式输出日志&nbsp; <br>#log4j.appender.R.File=c:/sys.html&nbsp;&nbsp; //文件位置&nbsp; <br>#log4j.appender.R.MaxFileSize=500KB&nbsp;&nbsp; //文件最大尺寸&nbsp; <br>#log4j.appender.R.MaxBackupIndex=1&nbsp;&nbsp; //备份数&nbsp; <br>#log4j.appender.R.layout=org.apache.log4j.HTMLLayout&nbsp;&nbsp; //文件的格式为Html格式&nbsp; <br>#log4j.appender.R.layout=org.apache.log4j.PatternLayout&nbsp;&nbsp;&nbsp; <br>#log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd&nbsp;&nbsp; HH:mm:ss,SSS}&nbsp;&nbsp; [%t]&nbsp;&nbsp; [%c]&nbsp;&nbsp; [%p]&nbsp;&nbsp; -&nbsp;&nbsp; %m%n&nbsp;&nbsp; <br><br>#输出格式<br>#%m 输出代码中指定的消息<br>#%p 输出优先级，即DEBUG，INFO，WARN，ERROR，FATAL<br>#%r 输出自应用启动到输出该log信息耗费的毫秒数<br>#%c 输出所属的类目，通常就是所在类的全名<br>#%t 输出产生该日志事件的线程名<br>#%n 输出一个回车换行符，Windows平台为&#8220;\r\n&#8221;，Unix平台为&#8220;\n&#8221;<br>#%d 输出日志时间点的日期或时间，默认格式为ISO8601，也可以在其后指定格式，比如：%d{yyy MMM dd HH:mm:ss,SSS}，输出类似：2002年10月18日 22：10：28，921<br>#%l 输出日志事件的发生位置，包括类目名、发生的线程，以及在代码中的行数。举例：Testlog4.main(TestLog4.java:10)<br><br>#一个教程网址：http://www.solol.org/technologic/java/j-log4j/<br><br><br>&nbsp;<br><br>
<br> <img src ="http://www.blogjava.net/monster/aggbug/124493.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/monster/" target="_blank">monster</a> 2007-06-15 14:32 <a href="http://www.blogjava.net/monster/archive/2007/06/15/124493.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java发送邮件,举例http://jakarta.apache.org/commons/email/userguide.html(zz),注意要设置字符集</title><link>http://www.blogjava.net/monster/archive/2007/06/15/124451.html</link><dc:creator>monster</dc:creator><author>monster</author><pubDate>Fri, 15 Jun 2007 03:33:00 GMT</pubDate><guid>http://www.blogjava.net/monster/archive/2007/06/15/124451.html</guid><description><![CDATA[http://jakarta.apache.org/commons/email/userguide.html.<br>
注意要设置字符集<br>
email.setCharset("gb2312");<br>
<br><img src ="http://www.blogjava.net/monster/aggbug/124451.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/monster/" target="_blank">monster</a> 2007-06-15 11:33 <a href="http://www.blogjava.net/monster/archive/2007/06/15/124451.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java设置代理</title><link>http://www.blogjava.net/monster/archive/2007/06/15/124447.html</link><dc:creator>monster</dc:creator><author>monster</author><pubDate>Fri, 15 Jun 2007 02:58:00 GMT</pubDate><guid>http://www.blogjava.net/monster/archive/2007/06/15/124447.html</guid><description><![CDATA[Properties props = System.getProperties();<br>
props.setProperty("proxySet", "true");<br>
props.setProperty("http.proxyHost", "192.168.0.200");<br>
props.setProperty("http.proxyPort", "3128");<br><img src ="http://www.blogjava.net/monster/aggbug/124447.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/monster/" target="_blank">monster</a> 2007-06-15 10:58 <a href="http://www.blogjava.net/monster/archive/2007/06/15/124447.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>