Apache James 簡稱 James, 是 Java Apache Mail Enterprise Server的縮寫。James 是100%基于Java的電子郵件服務器。它是一種獨立的郵件服務器,并提供了一個完整的電子郵件解決方案,用來收、發電子郵件。
本文不贅述James服務器的參數細節,這個以后有機會再詳細討論,這里先從服務器搭建開始,用搭建的服務器收、發電子郵件來熟悉一下Apache James。本人也是新手,希望能得到各位高手的指點,使大家能悟到進階的方向。
開始之前,先聲明一下本文操作的系統環境:
1. JDK 6u10b
2. JMail API v1.4.2
3. JAF API v1.1(JavaBean Activation Framework)
4. Apache James 2.3.1
5. Eclipse 3.4 for J2EE Developers
6. Windows 2000 Server sp4
1。請下載安裝JDK,并設置好其環境變量:JAVA_HOME=…… 、PATH=…… 、CLASSPATH=…… Eclipse 3.4 是目前的最新版本,可以從http://www.eclipse.org/ 下載。
2。請下載JMail API、JAF API,可以在www.java.sun.com 找到他們,找不到也不要緊,本文附件示例代碼免費提供一份(^_^)。
3。Apache James 的官方網站為:http://james.apache.org/ ,當前 Server 的最新版為 2.3.1版. 安裝非常簡單,下載后得到james-binary-2.3.1.zip 文件,解壓后即可。這里假設解壓到D盤(D:/james-2.3.1),目錄結構如下:
D:/james-2.3.1
|--- apps
|--- bin
|
…………
4。啟動 James 服務器
雙擊或在命令行下執行D:/james-2.3.1/bin 目錄下的 run.bat文件,即可啟動 James 服務器。
啟動后能在控制臺看到如下信息,說明啟動成功:
Java代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=Using%20PHOENIX_HOME%3A%20D%3A%5Cjames-2.3.1%20%0AUsing%20PHOENIX_TMPDIR%3A%20D%3A%5Cjames-2.3.1%5Ctemp%20%0AUsing%20JAVA_HOME%3A%20D%3A%5CJava%5Cjdk1.6.0_10%20%0A%0APhoenix%204.2%20%0A%0AJames%20Mail%20Server%202.3.1%20%0ARemote%20Manager%20Service%20started%20plain%3A4555%20%0APOP3%20Service%20started%20plain%3A110%20%0ASMTP%20Service%20started%20plain%3A25%20%0ANNTP%20Service%20started%20plain%3A119%20%0AFetchMail%20Disabled" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- Using PHOENIX_HOME: D:/james- 2.3 . 1
- Using PHOENIX_TMPDIR: D:/james-2.3 . 1 /temp
- Using JAVA_HOME: D:/Java/jdk1.6 .0_10
-
- Phoenix 4.2
-
- James Mail Server 2.3 . 1
- Remote Manager Service started plain:4555
- POP3 Service started plain:110
- SMTP Service started plain:25
- NNTP Service started plain:119
- FetchMail Disabled
Using PHOENIX_HOME: D:/james-2.3.1 Using PHOENIX_TMPDIR: D:/james-2.3.1/temp Using JAVA_HOME: D:/Java/jdk1.6.0_10 Phoenix 4.2 James Mail Server 2.3.1 Remote Manager Service started plain:4555 POP3 Service started plain:110 SMTP Service started plain:25 NNTP Service started plain:119 FetchMail Disabled
要關閉 James 服務,請使用 Ctrl + C
說明:啟動前請確保您的JDK環境變量如JAVA_HOME等已經設置好;James 啟動時,其SMTP 服務默認在 25 端口啟動,POP3 服務默認在 110 端口啟動, NNTP 服務默認在 119 端口啟動, 請確保這些端口未被占用。比如本系統下就已經安裝了Microsoft的 SMTP 服務,造成 James無法啟動,解決方法為:開始--->設置--->控制面板--->管理工具--->服務,打開后找到“Simple Mail Transport Protocol(SMTP)”服務項,關閉即可。
5。配置服務
服務器啟動后會在本機配置一個默認的服務,這可以用來在本機上本服務器范圍內收發郵件,但是郵件發不到163等外網郵箱。我們這里關注的重點就是配置一個能發到外網如163這樣的郵箱的郵件,這樣才有價值。不過由于本人是在本機上測試,一時不能獲取公司的域名參數,無法測試接收外網郵箱(如163)的郵件,見晾,不過做法都是一樣的,至少我們可以向外網郵箱發送郵件,局域網內可以收、發郵件。
打開D:/james-2.3.1/apps/james/SAR-INF 下的 config.xml 文件,初次啟動James之前,不會有這個文件,只有當James服務啟動一次之后才自動構件該文件。
找到
Java代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%E2%80%A6%E2%80%A6%20%0A%3Cpostmaster%3EPostmaster%40localhost%3C%2Fpostmaster%3E%20%0A%E2%80%A6%E2%80%A6%20%0A%3Cservernames%20autodetect%3D%22true%22%20autodetectIP%3D%22true%22%3E%20%0A%09%3Cservername%3Elocalhost%3C%2Fservername%3E%20%0A%3C%2Fservernames%3E%20%0A%E2%80%A6%E2%80%A6%20" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- ……
- <postmaster>Postmaster@localhost </postmaster>
- ……
- <servernames autodetect="true" autodetectIP= "true" >
- <servername>localhost</servername>
- </servernames>
- ……
…… <postmaster>Postmaster@localhost</postmaster> …… <servernames autodetect="true" autodetectIP="true"> <servername>localhost</servername> </servernames> ……
把localhost該成你自己想要的郵箱域名, 把自動探測IP屬性設置為“false”這里假設改成 zhaipuhong.com 如果開了一個帳號 zph ,那么他的郵件地址就是 [email protected] (^_^)修改結果如下:
Xml代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%E2%80%A6%E2%80%A6%20%0A%3Cpostmaster%3EPostmaster%40zhaipuhong.com%3C%2Fpostmaster%3E%20%0A%E2%80%A6%E2%80%A6%20%0A%3Cservernames%20autodetect%3D%22false%22%20autodetectIP%3D%22false%22%3E%20%0A%09%3Cservername%3Ezhaipuhong.com%3C%2Fservername%3E%20%0A%3C%2Fservernames%3E%20%0A%E2%80%A6%E2%80%A6" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- ……
- < postmaster > [email protected] </ postmaster >
- ……
- < servernames autodetect = "false" autodetectIP = "false" >
- < servername > zhaipuhong.com </ servername >
- </ servernames >
- ……
…… <postmaster>[email protected]</postmaster> …… <servernames autodetect="false" autodetectIP="false"> <servername>zhaipuhong.com</servername> </servernames> ……
找到
Xml代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Cmailet%20match%3D%22RemoteAddrNotInNetwork%3D127.0.0.1%22%20class%3D%22ToProcessor%22%3E%20%0A%09%3Cprocessor%3E%20relay-denied%20%3C%2Fprocessor%3E%20%0A%09%3Cnotice%3E550%20-%20Requested%20action%20not%20taken%3A%20relaying%20denied%3C%2Fnotice%3E%20%0A%3C%2Fmailet%3E" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- < mailet match = "RemoteAddrNotInNetwork=127.0.0.1" class = "ToProcessor" >
- < processor > relay-denied </ processor >
- < notice > 550 - Requested action not taken: relaying denied </ notice >
- </ mailet >
<mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor"> <processor> relay-denied </processor> <notice>550 - Requested action not taken: relaying denied</notice> </mailet>
將其注釋,結果如下:
Xml代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C!--mailet%20match%3D%22RemoteAddrNotInNetwork%3D127.0.0.1%22%20class%3D%22ToProcessor%22%3E%20%0A%09%3Cprocessor%3E%20relay-denied%20%3C%2Fprocessor%3E%20%0A%09%3Cnotice%3E550%20-%20Requested%20action%20not%20taken%3A%20relaying%20denied%3C%2Fnotice%3E%20%0A%3C%2Fmailet--%3E" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- <!--mailet match = "RemoteAddrNotInNetwork=127.0.0.1" class = "ToProcessor" >
- < processor > relay-denied </ processor >
- < notice > 550 - Requested action not taken: relaying denied </ notice >
- </ mailet-- >
<!--mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor"> <processor> relay-denied </processor> <notice>550 - Requested action not taken: relaying denied</notice> </mailet-->
找到下面元素,去掉其注釋
Xml代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3CauthRequired%3Etrue%3C%2FauthRequired%3E" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- < authRequired > true </ authRequired >
<authRequired>true</authRequired>
這樣郵箱訪問需要帳號驗證,你不希望別人用你的帳號收發消息吧……^_^
如此,James服務配置已經完成。
6。創建郵件帳號
創建郵件帳號后,就可以用來收發郵件了。James的賬號管理是通過基于Telnet客戶機的遠程管理器,這點頗為不爽,尤其是我的操作系統下的命令行控制臺是不顯示telnet命令輸入字符的,經常出錯。
現在進入命令行控制臺,在telnet localhsot 4555 進入James管理器,操作如下:
C代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=C%3A%5CDocuments%20and%20Settings%5Cdb2admin%3Etelnet%20localhost%204555%20" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- C:/Documents and Settings/db2admin>telnet localhost 4555
C:/Documents and Settings/db2admin>telnet localhost 4555
將進入
C代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=JAMES%20Remote%20Administration%20Tool%202.3.1%20Please%20enter%20your%20login%20and%20password%20Login%20id%3A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- JAMES Remote Administration Tool 2.3.1 Please enter your login and password Login id:
JAMES Remote Administration Tool 2.3.1 Please enter your login and password Login id:
默認的登陸id 為root 密碼也為 root ,登陸成功后結果如下:
C代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=JAMES%20Remote%20Administration%20Tool%202.3.1%20%0APlease%20enter%20your%20login%20and%20password%20%0ALogin%20id%3A%20%0APassword%3A%20%0AWelcome%20root.%20HELP%20for%20a%20list%20of%20commands%26nbsp%3B%26nbsp%3B" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- JAMES Remote Administration Tool 2.3.1
- Please enter your login and password
- Login id:
- Password:
- Welcome root. HELP for a list of commands
JAMES Remote Administration Tool 2.3.1 Please enter your login and password Login id: Password: Welcome root. HELP for a list of commands
創建新用戶的命令是:adduser username password
這里創建了兩個賬戶來作為演示使用: zhaipuhong/zhangpuhong ; zph/zph
說明:你可以輸入help來獲取命令幫助
C代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=help%20display%20this%20help%20%E6%98%BE%E7%A4%BA%E5%B8%AE%E5%8A%A9%E4%BF%A1%E6%81%AF%20%0Alistusers%20display%20existing%20accounts%20%E6%98%BE%E7%A4%BA%E7%8E%B0%E6%9C%89%E8%B4%A6%E6%88%B7%20%0Acountusers%20display%20the%20number%20of%20existing%20accounts%20%E6%98%BE%E7%A4%BA%E7%8E%B0%E6%9C%89%E8%B4%A6%E6%88%B7%E6%95%B0%E9%87%8F%20%0Aadduser%20%5Busername%5D%20%5Bpassword%5D%20add%20a%20new%20user%20%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA%E6%96%B0%E7%94%A8%E6%88%B7%20%0Averify%20%5Busername%5D%20verify%20if%20specified%20user%20exist%20%E6%A0%B8%E5%AE%9E%E6%8C%87%E5%AE%9A%E7%94%A8%E6%88%B7%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%20%0Adeluser%20%5Busername%5D%20delete%20existing%20user%20%E5%88%A0%E9%99%A4%E5%AD%98%E5%9C%A8%E7%9A%84%E7%94%A8%E6%88%B7%20%0Asetpassword%20%5Busername%5D%20%5Bpassword%5D%20sets%20a%20user's%20password%20%E8%AE%BE%E7%BD%AE%E7%94%A8%E6%88%B7%E5%AF%86%E7%A0%81%20%0Asetalias%20%5Buser%5D%20%5Balias%5D%20locally%20forwards%20all%20email%20for%20'user'%20to%20'alias'%20%E8%AE%BE%E7%BD%AE%E9%82%AE%E7%AE%B1%E5%88%AB%E5%90%8D%20%0Ashowalias%20%5Busername%5D%20shows%20a%20user's%20current%20email%20alias%20%E6%98%BE%E7%A4%BA%E5%88%AB%E5%90%8D%20%0Aunsetalias%20%5Buser%5D%20unsets%20an%20alias%20for%20'user'%20%E5%88%A0%E9%99%A4%E7%94%A8%E6%88%B7%E5%88%AB%E5%90%8D%20%0Asetforwarding%20%5Busername%5D%20%5Bemailaddress%5D%20forwards%20a%20user's%20email%20to%20another%20email%20address%20%E8%BD%AC%E5%8F%91%E7%94%A8%E6%88%B7%E7%9A%84%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6%E5%9C%B0%E5%9D%80%E5%88%B0%E5%8F%A6%E4%B8%80%E4%B8%AA%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6%E5%9C%B0%E5%9D%80%20%0Ashowforwarding%20%5Busername%5D%20shows%20a%20user's%20current%20email%20forwarding%20%E6%98%BE%E7%A4%BA%E7%94%A8%E6%88%B7%E7%9A%84%E5%BD%93%E5%89%8D%E9%82%AE%E4%BB%B6%E8%BD%AC%E5%8F%91%20%0Aunsetforwarding%20%5Busername%5D%20emoves%20a%20forward%20%E5%88%A0%E9%99%A4%E8%BD%AC%E5%8F%91%20%0Auser%20%5Brepositoryname%5D%20change%20to%20another%20user%20repository%20%E6%94%B9%E5%8F%98%E5%8F%A6%E4%B8%80%E4%B8%AA%E7%94%A8%E6%88%B7%E5%BA%93%20%0Ashutdown%20kills%20the%20current%20JVM%20(convenient%20when%20James%20is%20run%20as%20a%20daemon)%E5%85%B3%E9%97%AD%E5%BD%93%E5%89%8D%E7%9A%84%E6%9C%8D%E5%8A%A1%20%0Aquit%20close%20connection%20%E5%85%B3%E9%97%AD%E8%BF%9E%E6%8E%A5" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- help display this help 顯示幫助信息
- listusers display existing accounts 顯示現有賬戶
- countusers display the number of existing accounts 顯示現有賬戶數量
- adduser [username] [password] add a new user 添加一個新用戶
- verify [username] verify if specified user exist 核實指定用戶是否存在
- deluser [username] delete existing user 刪除存在的用戶
- setpassword [username] [password] sets a user's password 設置用戶密碼
- setalias [user] [alias] locally forwards all email for 'user' to 'alias' 設置郵箱別名
- showalias [username] shows a user's current email alias 顯示別名
- unsetalias [user] unsets an alias for 'user' 刪除用戶別名
- setforwarding [username] [emailaddress] forwards a user's email to another email address 轉發用戶的電子郵件地址到另一個電子郵件地址
- showforwarding [username] shows a user's current email forwarding 顯示用戶的當前郵件轉發
- unsetforwarding [username] emoves a forward 刪除轉發
- user [repositoryname] change to another user repository 改變另一個用戶庫
- shutdown kills the current JVM (convenient when James is run as a daemon)關閉當前的服務
- quit close connection 關閉連接
help display this help 顯示幫助信息 listusers display existing accounts 顯示現有賬戶 countusers display the number of existing accounts 顯示現有賬戶數量 adduser [username] [password] add a new user 添加一個新用戶 verify [username] verify if specified user exist 核實指定用戶是否存在 deluser [username] delete existing user 刪除存在的用戶 setpassword [username] [password] sets a user's password 設置用戶密碼 setalias [user] [alias] locally forwards all email for 'user' to 'alias' 設置郵箱別名 showalias [username] shows a user's current email alias 顯示別名 unsetalias [user] unsets an alias for 'user' 刪除用戶別名 setforwarding [username] [emailaddress] forwards a user's email to another email address 轉發用戶的電子郵件地址到另一個電子郵件地址 showforwarding [username] shows a user's current email forwarding 顯示用戶的當前郵件轉發 unsetforwarding [username] emoves a forward 刪除轉發 user [repositoryname] change to another user repository 改變另一個用戶庫 shutdown kills the current JVM (convenient when James is run as a daemon)關閉當前的服務 quit close connection 關閉連接
賬號創建成功后,使用quit退出管理器?,F在我們可以開始部署我們的JMail應用了。
7。用James收發郵件
現在我們來寫兩個簡單的程序來體驗一下如何使用James收發郵件。請注意,示例程序是通用的。關于如何用JMail API收發郵件,這里不詳細介紹,如果還沒有接觸過請參考一下我之前的文章:
http://zhaipuhong.javaeye.com/admin/blogs/213005
關于接受郵件部分,我們在代碼中通過注釋來描述。另外我們來回憶一下上述步驟配置James服務器的參數:
1。smtp 服務器:192.168.1.98或者localhost 192.168.1.98是我的局域網IP,由于我James服務就裝在我的機器上,所以我可以用localhost
2。兩個郵件帳號:zhaipuhong/zhaipuhong 對應郵箱地址: [email protected]
zph/zph 對應郵箱地址: [email protected]
注意:郵箱域名我們配置的是zhaipuhong.com
示例程序:
Java代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=package%20com.zhaipuhong.j2ee.jmail%3B%0A%0Aimport%20java.io.IOException%3B%0Aimport%20java.util.Properties%3B%0A%0Aimport%20javax.mail.Authenticator%3B%0Aimport%20javax.mail.Folder%3B%0Aimport%20javax.mail.Message%3B%0Aimport%20javax.mail.MessagingException%3B%0Aimport%20javax.mail.PasswordAuthentication%3B%0Aimport%20javax.mail.Session%3B%0Aimport%20javax.mail.Store%3B%0Aimport%20javax.mail.Transport%3B%0Aimport%20javax.mail.internet.InternetAddress%3B%0Aimport%20javax.mail.internet.MimeMessage%3B%0Aimport%20javax.mail.internet.MimeUtility%3B%0A%0Apublic%20class%20HelloJMail%20%7B%0A%09%0A%09%2F%2F%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%0A%09public%20static%20void%20sendMail()%20%7B%0A%09%09%2F%2FString%20host%20%3D%20%22192.168.1.98%22%3B%20%2F%2F%20%E6%8C%87%E5%AE%9A%E7%9A%84smtp%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%8C%E6%9C%AC%E6%9C%BA%E7%9A%84%E5%B1%80%E5%9F%9F%E7%BD%91IP%0A%09%09String%20host%20%3D%20%22localhost%22%3B%20%2F%2F%20%E6%9C%AC%E6%9C%BAsmtp%E6%9C%8D%E5%8A%A1%E5%99%A8%0A%09%09%2F%2FString%20host%20%3D%20%22smtp.163.com%22%3B%20%2F%2F%20163%E7%9A%84smtp%E6%9C%8D%E5%8A%A1%E5%99%A8%0A%09%09String%20from%20%3D%20%22zhaipuhong%40zhaipuhong.com%22%3B%20%2F%2F%20%E9%82%AE%E4%BB%B6%E5%8F%91%E9%80%81%E4%BA%BA%E7%9A%84%E9%82%AE%E4%BB%B6%E5%9C%B0%E5%9D%80%0A%09%09String%20to%20%3D%20%22zph%40zhaipuhong.com%22%3B%20%2F%2F%20%E9%82%AE%E4%BB%B6%E6%8E%A5%E6%94%B6%E4%BA%BA%E7%9A%84%E9%82%AE%E4%BB%B6%E5%9C%B0%E5%9D%80%0A%09%09final%20String%20username%20%3D%20%22zhaipuhong%22%3B%20%20%2F%2F%E5%8F%91%E4%BB%B6%E4%BA%BA%E7%9A%84%E9%82%AE%E4%BB%B6%E5%B8%90%E6%88%B7%0A%09%09final%20String%20password%20%3D%20%22zhaipuhong%22%3B%20%20%20%2F%2F%E5%8F%91%E4%BB%B6%E4%BA%BA%E7%9A%84%E9%82%AE%E4%BB%B6%E5%AF%86%E7%A0%81%0A%0A%09%09%2F%2F%20%E5%88%9B%E5%BB%BAProperties%20%E5%AF%B9%E8%B1%A1%0A%09%09Properties%20props%20%3D%20System.getProperties()%3B%0A%0A%09%09%2F%2F%20%E6%B7%BB%E5%8A%A0smtp%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%B1%9E%E6%80%A7%0A%09%09props.put(%22mail.smtp.host%22%2C%20host)%3B%0A%09%09props.put(%22mail.smtp.auth%22%2C%20%22true%22)%3B%0A%0A%09%09%2F%2F%20%E5%88%9B%E5%BB%BA%E9%82%AE%E4%BB%B6%E4%BC%9A%E8%AF%9D%0A%09%09Session%20session%20%3D%20Session.getDefaultInstance(props%2C%20new%20Authenticator()%7B%0A%09%09%09%40Override%0A%09%09%09public%20PasswordAuthentication%20getPasswordAuthentication()%20%7B%0A%09%09%09%09return%20new%20PasswordAuthentication(username%2C%20password)%3B%0A%09%09%09%7D%0A%09%09%09%0A%09%09%7D)%3B%0A%0A%09%09try%20%7B%0A%09%09%09%2F%2F%20%E5%AE%9A%E4%B9%89%E9%82%AE%E4%BB%B6%E4%BF%A1%E6%81%AF%0A%09%09%09MimeMessage%20message%20%3D%20new%20MimeMessage(session)%3B%0A%09%09%09message.setFrom(new%20InternetAddress(from))%3B%0A%09%09%09message.addRecipient(Message.RecipientType.TO%2C%20new%20InternetAddress(%0A%09%09%09%09%09to))%3B%0A%09%09%09%2F%2Fmessage.setSubject(transferChinese(%22%E6%88%91%E6%9C%89%E8%87%AA%E5%B7%B1%E7%9A%84%E9%82%AE%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%BA%86%22))%3B%0A%09%09%09message.setSubject(%22I%20hava%20my%20own%20mail%20server%22)%3B%0A%09%09%09message.setText(%22From%20now%2C%20you%20have%20your%20own%20mail%20server%2C%20congratulation!%22)%3B%0A%0A%09%09%09%2F%2F%20%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF%0A%09%09%09session.getTransport(%22smtp%22).send(message)%3B%20%20%0A%09%09%09%2F%2FTransport.send(message)%3B%20%2F%2F%E4%B9%9F%E5%8F%AF%E4%BB%A5%E8%BF%99%E6%A0%B7%E5%88%9B%E5%BB%BATransport%E5%AF%B9%E8%B1%A1%E5%8F%91%E9%80%81%0A%09%09%09System.out.println(%22SendMail%20Process%20Over!%22)%3B%0A%0A%09%09%7D%20catch%20(MessagingException%20e)%20%7B%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7D%0A%09%7D%0A%09%0A%09%2F%2F%E6%8E%A5%E5%8F%97%E9%82%AE%E4%BB%B6%0A%09public%20static%20void%20getMail()%7B%0A%09%09String%20host%20%3D%20%22localhost%22%3B%0A%09%20%20%20%20final%20String%20username%20%3D%20%22zph%22%3B%0A%09%09final%20String%20password%20%3D%20%22zph%22%3B%0A%0A%09%09%2F%2F%20%E5%88%9B%E5%BB%BAProperties%20%E5%AF%B9%E8%B1%A1%0A%09%20%20%20%20Properties%20props%20%3D%20new%20Properties()%3B%0A%0A%09%09%2F%2F%20%E5%88%9B%E5%BB%BA%E9%82%AE%E4%BB%B6%E4%BC%9A%E8%AF%9D%0A%09%09Session%20session%20%3D%20Session.getDefaultInstance(props%2C%20new%20Authenticator()%7B%0A%09%09%09%40Override%0A%09%09%09public%20PasswordAuthentication%20getPasswordAuthentication()%20%7B%0A%09%09%09%09return%20new%20PasswordAuthentication(username%2C%20password)%3B%0A%09%09%09%7D%0A%09%09%09%0A%09%09%7D)%3B%0A%0A%09%20%20%20%20%0A%09%09try%20%7B%0A%09%09%09%2F%2F%20%E8%8E%B7%E5%8F%96%E9%82%AE%E7%AE%B1%E7%9A%84pop3%E5%AD%98%E5%82%A8%0A%09%09%09Store%20store%20%3D%20session.getStore(%22pop3%22)%3B%0A%09%09%09store.connect(host%2C%20username%2C%20password)%3B%0A%0A%09%20%20%20%20%20%20%20%20%2F%2F%20%E8%8E%B7%E5%8F%96inbox%E6%96%87%E4%BB%B6%0A%09%09%20%20%20%20Folder%20folder%20%3D%20store.getFolder(%22INBOX%22)%3B%0A%09%09%20%20%20%20folder.open(Folder.READ_ONLY)%3B%20%20%2F%2F%E6%89%93%E5%BC%80%EF%BC%8C%E6%89%93%E5%BC%80%E5%90%8E%E6%89%8D%E8%83%BD%E8%AF%BB%E5%8F%96%E9%82%AE%E4%BB%B6%E4%BF%A1%E6%81%AF%0A%0A%09%09%20%20%20%20%2F%2F%20%E8%8E%B7%E5%8F%96%E9%82%AE%E4%BB%B6%E6%B6%88%E6%81%AF%0A%09%09%20%20%20%20Message%20message%5B%5D%20%3D%20folder.getMessages()%3B%0A%0A%09%09%20%20%20%20for%20(int%20i%3D0%2C%20n%3Dmessage.length%3B%20i%3Cn%3B%20i%2B%2B)%20%7B%0A%09%09%20%20%20%20%09System.out.println(i%20%2B%20%22%3A%20%22%20%2B%20message%5Bi%5D.getFrom()%5B0%5D%0A%09%09%20%20%20%20%09%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%5Ct%22%20%2B%20message%5Bi%5D.getSubject())%3B%0A%09%09%20%20%20%20%09try%20%7B%0A%09%09%09%09%09message%5Bi%5D.writeTo(System.out)%3B%0A%09%09%09%09%7D%20catch%20(IOException%20e)%20%7B%0A%09%09%09%09%09e.printStackTrace()%3B%0A%09%09%09%09%7D%0A%0A%09%09%20%20%20%20%7D%0A%0A%09%09%20%20%20%20%2F%2F%20%E5%85%B3%E9%97%AD%E8%B5%84%E6%BA%90%0A%09%09%20%20%20%20folder.close(false)%3B%0A%09%09%20%20%20%20store.close()%3B%0A%09%09%20%20%20%20%0A%09%09%7D%20catch%20(MessagingException%20e)%20%7B%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7D%0A%09%09%0A%09%09System.out.println(%22GetMail%20Process%20Over!%22)%3B%0A%0A%09%7D%0A%09%0A%09%2F%2F%E9%82%AE%E4%BB%B6%E4%B8%BB%E9%A2%98%E4%B8%AD%E6%96%87%E5%AD%97%E7%AC%A6%E8%BD%AC%E6%8D%A2%0A%09public%20static%20String%20transferChinese(String%20strText)%7B%0A%09%09try%7B%0A%09%09%09strText%20%3D%20MimeUtility.encodeText(new%20String(strText.getBytes()%2C%20%22GB2312%22)%2C%20%22GB2312%22%2C%20%22B%22)%3B%0A%09%09%7Dcatch(Exception%20ex)%7B%0A%09%09%09ex.printStackTrace()%3B%0A%09%09%7D%0A%09%09return%20strText%3B%0A%09%7D%0A%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09HelloJMail.sendMail()%3B%0A%09%09%2F%2FHelloJMail.getMail()%3B%0A%09%7D%0A%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- package com.zhaipuhong.j2ee.jmail;
-
- import java.io.IOException;
- import java.util.Properties;
-
- import javax.mail.Authenticator;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Store;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeUtility;
-
- public class HelloJMail {
-
-
- public static void sendMail() {
-
- String host = "localhost" ;
-
- String from = "[email protected]" ;
- String to = "[email protected]" ;
- final String username = "zhaipuhong" ;
- final String password = "zhaipuhong" ;
-
-
- Properties props = System.getProperties();
-
-
- props.put("mail.smtp.host" , host);
- props.put("mail.smtp.auth" , "true" );
-
-
- Session session = Session.getDefaultInstance(props, new Authenticator(){
- @Override
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
-
- });
-
- try {
-
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(from));
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(
- to));
-
- message.setSubject("I hava my own mail server" );
- message.setText("From now, you have your own mail server, congratulation!" );
-
-
- session.getTransport("smtp" ).send(message);
-
- System.out.println("SendMail Process Over!" );
-
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- }
-
-
- public static void getMail(){
- String host = "localhost" ;
- final String username = "zph" ;
- final String password = "zph" ;
-
-
- Properties props = new Properties();
-
-
- Session session = Session.getDefaultInstance(props, new Authenticator(){
- @Override
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
-
- });
-
-
- try {
-
- Store store = session.getStore("pop3" );
- store.connect(host, username, password);
-
-
- Folder folder = store.getFolder("INBOX" );
- folder.open(Folder.READ_ONLY);
-
-
- Message message[] = folder.getMessages();
-
- for ( int i= 0 , n=message.length; i<n; i++) {
- System.out.println(i + ": " + message[i].getFrom()[ 0 ]
- + "/t" + message[i].getSubject());
- try {
- message[i].writeTo(System.out);
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
-
- folder.close(false );
- store.close();
-
- } catch (MessagingException e) {
- e.printStackTrace();
- }
-
- System.out.println("GetMail Process Over!" );
-
- }
-
-
- public static String transferChinese(String strText){
- try {
- strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312" ), "GB2312" , "B" );
- }catch (Exception ex){
- ex.printStackTrace();
- }
- return strText;
- }
-
- public static void main(String[] args) {
- HelloJMail.sendMail();
-
- }
-
- }
package com.zhaipuhong.j2ee.jmail;import java.io.IOException;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Store;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeUtility;public class HelloJMail {//發送郵件public static void sendMail() {//String host = "192.168.1.98"; // 指定的smtp服務器,本機的局域網IPString host = "localhost"; // 本機smtp服務器//String host = "smtp.163.com"; // 163的smtp服務器String from = "[email protected]"; // 郵件發送人的郵件地址String to = "[email protected]"; // 郵件接收人的郵件地址final String username = "zhaipuhong"; //發件人的郵件帳戶final String password = "zhaipuhong"; //發件人的郵件密碼// 創建Properties 對象Properties props = System.getProperties();// 添加smtp服務器屬性props.put("mail.smtp.host", host);props.put("mail.smtp.auth", "true");// 創建郵件會話Session session = Session.getDefaultInstance(props, new Authenticator(){@Overridepublic PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});try {// 定義郵件信息MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress(from));message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));//message.setSubject(transferChinese("我有自己的郵件服務器了"));message.setSubject("I hava my own mail server");message.setText("From now, you have your own mail server, congratulation!");// 發送消息session.getTransport("smtp").send(message); //Transport.send(message); //也可以這樣創建Transport對象發送System.out.println("SendMail Process Over!");} catch (MessagingException e) {e.printStackTrace();}}//接受郵件public static void getMail(){String host = "localhost"; final String username = "zph";final String password = "zph";// 創建Properties 對象 Properties props = new Properties();// 創建郵件會話Session session = Session.getDefaultInstance(props, new Authenticator(){@Overridepublic PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}}); try {// 獲取郵箱的pop3存儲Store store = session.getStore("pop3");store.connect(host, username, password); // 獲取inbox文件 Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); //打開,打開后才能讀取郵件信息 // 獲取郵件消息 Message message[] = folder.getMessages(); for (int i=0, n=message.length; i<n; i++) { System.out.println(i + ": " + message[i].getFrom()[0] + "/t" + message[i].getSubject()); try {message[i].writeTo(System.out);} catch (IOException e) {e.printStackTrace();} } // 關閉資源 folder.close(false); store.close(); } catch (MessagingException e) {e.printStackTrace();}System.out.println("GetMail Process Over!");}//郵件主題中文字符轉換public static String transferChinese(String strText){try{strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");}catch(Exception ex){ex.printStackTrace();}return strText;}public static void main(String[] args) {HelloJMail.sendMail();//HelloJMail.getMail();}}
說明: 執行測試時,先執行發送,在main方法中注釋掉郵件接受方法調用,等待片刻,再注釋掉郵件發送方法調用,執行郵件接受方法調用。因為就算163這樣優秀的企業級郵箱也不可能瞬間就能收到發送的郵件。
在郵件發送方法中:sendMail(), 可以嘗試一下又本郵件服務器向163郵箱發送郵件,如果想通過163郵件服務器想本地發送郵件,你需要配置DSN。
以下為本機接收James郵件服務器提供服務的郵件結果:
C代碼 < type="application/x-shockwave-flash" width="14" height="15" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src=http://www.jz123.cn/text/"http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=0%3A%20zhaipuhong%40zhaipuhong.com%09I%20hava%20my%20own%20mail%20server%0AReturn-Path%3A%20%3Czhaipuhong%40zhaipuhong.com%3E%0ADelivered-To%3A%20zph%40zhaipuhong.com%0AReceived%3A%20from%20127.0.0.1%20(%5B127.0.0.1%5D)%0A%20%20%20%20%20%20%20%20%20%20by%20ZPH%20(JAMES%20SMTP%20Server%202.3.1)%20with%20SMTP%20ID%20693%0A%20%20%20%20%20%20%20%20%20%20for%20%3Czph%40zhaipuhong.com%3E%3B%0A%20%20%20%20%20%20%20%20%20%20Wed%2C%209%20Jul%202008%2019%3A58%3A38%20%2B0800%20(CST)%0ADate%3A%20Wed%2C%209%20Jul%202008%2019%3A58%3A38%20%2B0800%20(CST)%0AFrom%3A%20zhaipuhong%40zhaipuhong.com%0ATo%3A%20zph%40zhaipuhong.com%0AMessage-ID%3A%20%3C11627000.01215604718796.JavaMail.db2admin%40ZPH%3E%0ASubject%3A%20I%20hava%20my%20own%20mail%20server%0AMIME-Version%3A%201.0%0AContent-Type%3A%20text%2Fplain%3B%20charset%3Dus-ascii%0AContent-Transfer-Encoding%3A%207bit%0A%0AFrom%20now%2C%20you%20have%20your%20own%20mail%20server%2C%20congratulation!" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
- 0: [email protected] I hava my own mail server
- Return-Path: <[email protected]>
- Delivered-To: [email protected]
- Received: from 127.0.0.1 ([127.0.0.1])
- by ZPH (JAMES SMTP Server 2.3.1) with SMTP ID 693
- for <[email protected]>;
- Wed, 9 Jul 2008 19:58:38 +0800 (CST)
- Date: Wed, 9 Jul 2008 19:58:38 +0800 (CST)
- From: [email protected]
- To: [email protected]
- Message-ID: <11627000.01215604718796.JavaMail.db2admin@ZPH>
- Subject: I hava my own mail server
- MIME-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
-
- From now, you have your own mail server, congratulation!
0: [email protected] hava my own mail serverReturn-Path: <[email protected]>Delivered-To: [email protected]: from 127.0.0.1 ([127.0.0.1]) by ZPH (JAMES SMTP Server 2.3.1) with SMTP ID 693 for <[email protected]>; Wed, 9 Jul 2008 19:58:38 +0800 (CST)Date: Wed, 9 Jul 2008 19:58:38 +0800 (CST)From: [email protected]: [email protected]: <11627000.01215604718796.JavaMail.db2admin@ZPH>Subject: I hava my own mail serverMIME-Version: 1.0Content-Type: text/plain; charset=us-asciiContent-Transfer-Encoding: 7bitFrom now, you have your own mail server, congratulation!
從上面的結果可以看出,該郵件是由[email protected] 發給 [email protected] 的。
多發幾封電子郵件,就會發現,James 的郵件接收是一個棧結構,最近收到的郵件編號為0,依次類推……