首页 > 网站 > Apache > 正文

配置apache james邮件服务器教程

2024-08-27 18:31:01
字体:
来源:转载
供稿:网友

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">
  1. Using PHOENIX_HOME: D:/james- 2.3 . 1    
  2. Using PHOENIX_TMPDIR: D:/james-2.3 . 1 /temp   
  3. Using JAVA_HOME: D:/Java/jdk1.6 .0_10   
  4.   
  5. Phoenix 4.2    
  6.   
  7. James Mail Server 2.3 . 1    
  8. Remote Manager Service started plain:4555    
  9. POP3 Service started plain:110    
  10. SMTP Service started plain:25    
  11. NNTP Service started plain:119    
  12. 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">
  1. ……   
  2. <postmaster>Postmaster@localhost </postmaster>   
  3. ……   
  4. <servernames autodetect="true"  autodetectIP= "true" >   
  5.     <servername>localhost</servername>   
  6. </servernames>   
  7. ……   

        把localhost该成你自己想要的邮箱域名, 把自动探测IP属性设置为“false”这里假设改成 zhaipuhong.com 如果开了一个帐号 zph ,那么他的邮件地址就是 zph@zhaipuhong.com (^_^)修改结果如下:

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">
  1. ……   
  2. < postmaster > Postmaster@zhaipuhong.com </ postmaster >    
  3. ……   
  4. < servernames   autodetect = "false"   autodetectIP = "false" >    
  5.     < servername > zhaipuhong.com </ servername >    
  6. </ servernames >    
  7. ……  

        找到

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">
  1. < mailet   match = "RemoteAddrNotInNetwork=127.0.0.1"   class = "ToProcessor" >    
  2.     < processor >  relay-denied  </ processor >    
  3.     < notice > 550 - Requested action not taken: relaying denied </ notice >    
  4. </ 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">
  1. <!--mailet  match = "RemoteAddrNotInNetwork=127.0.0.1"   class = "ToProcessor" >    
  2.     < processor >  relay-denied  </ processor >    
  3.     < notice > 550 - Requested action not taken: relaying denied </ notice >    
  4. </ 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">
  1. < 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">
  1. 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">
  1. 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">
  1. JAMES Remote Administration Tool 2.3.1   
  2. Please enter your login and password   
  3. Login id:   
  4. Password:   
  5. 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">
  1. help display  this  help 显示帮助信息   
  2. listusers display existing accounts 显示现有账户   
  3. countusers display the number of existing accounts 显示现有账户数量   
  4. adduser [username] [password] add a new  user 添加一个新用户   
  5. verify [username] verify if  specified user exist 核实指定用户是否存在   
  6. deluser [username] delete  existing user 删除存在的用户   
  7. setpassword [username] [password] sets a user's password 设置用户密码   
  8. setalias [user] [alias] locally forwards all email for   'user'  to  'alias'  设置邮箱别名   
  9. showalias [username] shows a user's current email alias 显示别名   
  10. unsetalias [user] unsets an alias for   'user'  删除用户别名   
  11. setforwarding [username] [emailaddress] forwards a user's email to another email address 转发用户的电子邮件地址到另一个电子邮件地址   
  12. showforwarding [username] shows a user's current email forwarding 显示用户的当前邮件转发   
  13. unsetforwarding [username] emoves a forward 删除转发   
  14. user [repositoryname] change to another user repository 改变另一个用户库   
  15. shutdown kills the current JVM (convenient when James is run as a daemon)关闭当前的服务   
  16. quit close connection 关闭连接  

      账号创建成功后,使用quit退出管理器。现在我们可以开始部署我们的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   对应邮箱地址: zhaipuhong@zhaipuhong.com

                                zph/zph                            对应邮箱地址: zph@zhaipuhong.com

      注意:邮箱域名我们配置的是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">
  1. package  com.zhaipuhong.j2ee.jmail;  
  2.   
  3. import  java.io.IOException;  
  4. import  java.util.Properties;  
  5.   
  6. import  javax.mail.Authenticator;  
  7. import  javax.mail.Folder;  
  8. import  javax.mail.Message;  
  9. import  javax.mail.MessagingException;  
  10. import  javax.mail.PasswordAuthentication;  
  11. import  javax.mail.Session;  
  12. import  javax.mail.Store;  
  13. import  javax.mail.Transport;  
  14. import  javax.mail.internet.InternetAddress;  
  15. import  javax.mail.internet.MimeMessage;  
  16. import  javax.mail.internet.MimeUtility;  
  17.   
  18. public   class  HelloJMail {  
  19.       
  20.     //发送邮件   
  21.     public   static   void  sendMail() {  
  22.         //String host = "192.168.1.98"; // 指定的smtp服务器,本机的局域网IP   
  23.         String host = "localhost" // 本机smtp服务器   
  24.         //String host = "smtp.163.com"; // 163的smtp服务器   
  25.         String from = "zhaipuhong@zhaipuhong.com" // 邮件发送人的邮件地址   
  26.         String to = "zph@zhaipuhong.com" // 邮件接收人的邮件地址   
  27.         final  String username =  "zhaipuhong" ;   //发件人的邮件帐户   
  28.         final  String password =  "zhaipuhong" ;    //发件人的邮件密码   
  29.   
  30.         // 创建Properties 对象   
  31.         Properties props = System.getProperties();  
  32.   
  33.         // 添加smtp服务器属性   
  34.         props.put("mail.smtp.host" , host);  
  35.         props.put("mail.smtp.auth" "true" );  
  36.   
  37.         // 创建邮件会话   
  38.         Session session = Session.getDefaultInstance(props, new  Authenticator(){  
  39.             @Override   
  40.             public  PasswordAuthentication getPasswordAuthentication() {  
  41.                 return   new  PasswordAuthentication(username, password);  
  42.             }  
  43.               
  44.         });  
  45.   
  46.         try  {  
  47.             // 定义邮件信息   
  48.             MimeMessage message = new  MimeMessage(session);  
  49.             message.setFrom(new  InternetAddress(from));  
  50.             message.addRecipient(Message.RecipientType.TO, new  InternetAddress(  
  51.                     to));  
  52.             //message.setSubject(transferChinese("我有自己的邮件服务器了"));   
  53.             message.setSubject("I hava my own mail server" );  
  54.             message.setText("From now, you have your own mail server, congratulation!" );  
  55.   
  56.             // 发送消息   
  57.             session.getTransport("smtp" ).send(message);    
  58.             //Transport.send(message); //也可以这样创建Transport对象发送   
  59.             System.out.println("SendMail Process Over!" );  
  60.   
  61.         } catch  (MessagingException e) {  
  62.             e.printStackTrace();  
  63.         }  
  64.     }  
  65.       
  66.     //接受邮件   
  67.     public   static   void  getMail(){  
  68.         String host = "localhost" ;  
  69.         final  String username =  "zph" ;  
  70.         final  String password =  "zph" ;  
  71.   
  72.         // 创建Properties 对象   
  73.         Properties props = new  Properties();  
  74.   
  75.         // 创建邮件会话   
  76.         Session session = Session.getDefaultInstance(props, new  Authenticator(){  
  77.             @Override   
  78.             public  PasswordAuthentication getPasswordAuthentication() {  
  79.                 return   new  PasswordAuthentication(username, password);  
  80.             }  
  81.               
  82.         });  
  83.   
  84.           
  85.         try  {  
  86.             // 获取邮箱的pop3存储   
  87.             Store store = session.getStore("pop3" );  
  88.             store.connect(host, username, password);  
  89.   
  90.             // 获取inbox文件   
  91.             Folder folder = store.getFolder("INBOX" );  
  92.             folder.open(Folder.READ_ONLY);  //打开,打开后才能读取邮件信息   
  93.   
  94.             // 获取邮件消息   
  95.             Message message[] = folder.getMessages();  
  96.   
  97.             for  ( int  i= 0 , n=message.length; i<n; i++) {  
  98.                 System.out.println(i + ": "  + message[i].getFrom()[ 0 ]  
  99.                                                + "/t"  + message[i].getSubject());  
  100.                 try  {  
  101.                     message[i].writeTo(System.out);  
  102.                 } catch  (IOException e) {  
  103.                     e.printStackTrace();  
  104.                 }  
  105.   
  106.             }  
  107.   
  108.             // 关闭资源   
  109.             folder.close(false );  
  110.             store.close();  
  111.               
  112.         } catch  (MessagingException e) {  
  113.             e.printStackTrace();  
  114.         }  
  115.           
  116.         System.out.println("GetMail Process Over!" );  
  117.   
  118.     }  
  119.       
  120.     //邮件主题中文字符转换   
  121.     public   static  String transferChinese(String strText){  
  122.         try {  
  123.             strText = MimeUtility.encodeText(new  String(strText.getBytes(),  "GB2312" ),  "GB2312" "B" );  
  124.         }catch (Exception ex){  
  125.             ex.printStackTrace();  
  126.         }  
  127.         return  strText;  
  128.     }  
  129.   
  130.     public   static   void  main(String[] args) {  
  131.         HelloJMail.sendMail();  
  132.         //HelloJMail.getMail();   
  133.     }  
  134.   
  135. }  

      说明: 执行测试时,先执行发送,在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">
  1. 0: zhaipuhong@zhaipuhong.com    I hava my own mail server  
  2. Return-Path: <zhaipuhong@zhaipuhong.com>  
  3. Delivered-To: zph@zhaipuhong.com  
  4. Received: from 127.0.0.1 ([127.0.0.1])  
  5.           by ZPH (JAMES SMTP Server 2.3.1) with SMTP ID 693  
  6.           for  <zph@zhaipuhong.com>;  
  7.           Wed, 9 Jul 2008 19:58:38 +0800 (CST)  
  8. Date: Wed, 9 Jul 2008 19:58:38 +0800 (CST)  
  9. From: zhaipuhong@zhaipuhong.com  
  10. To: zph@zhaipuhong.com  
  11. Message-ID: <11627000.01215604718796.JavaMail.db2admin@ZPH>  
  12. Subject: I hava my own mail server  
  13. MIME-Version: 1.0  
  14. Content-Type: text/plain; charset=us-ascii  
  15. Content-Transfer-Encoding: 7bit  
  16.   
  17. From now, you have your own mail server, congratulation!  

       从上面的结果可以看出,该邮件是由zhaipuhong@zhaipuhong.com  发给 zph@zhaipuhong.com 的。

       多发几封电子邮件,就会发现,James 的邮件接收是一个栈结构,最近收到的邮件编号为0,依次类推……

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表