James服务器配置(2)

 

 

启动 James 服务器

      

 双击\james-2.3.1\bin 目录下的 run.bat文件,即可启动 James 服务器。

 

控制台显示如下:

 

Using PHOENIX_HOME:   F:\项目\James\james-2.3.1rc1   

 

Using PHOENIX_HOME:   F:\项目\James\james-2.3.1rc1
Using PHOENIX_TMPDIR: F:\项目\James\james-2.3.1rc1\temp
Using JAVA_HOME:      C:\Program Files\Java\jdk1.6.0_03

Phoenix 4.2

James Mail Server 2.3.1rc1
Remote Manager Service started plain:4555
POP3 Service started plain:110
SMTP Service started plain:25
NNTP Service started plain:119
FetchMail Disabled

 

 

启动成功。关闭Ctrl + C

 

说明:启动前请确保您的JDK环境变量如JAVA_HOME等已经设置好;James 启动时,其SMTP 服务默认在 25 端口启动,POP3 服务默认在 110 端口启动, NNTP 服务默认在 119 端口启动, 请确保这些端口未被占用。

 

服务配置:

 

打开F:\项目\James\james-2.3.1rc1\apps\james\SAR-INF 下的 config.xml 文件,初次启动James之前,不会有这个文件,只有当James服务启动一次之后才自动构件该文件。

 

需要修改的地方:

 

 
 
…… 
<postmaster>Postmaster@localhost</postmaster> 
…… 
<servernames autodetect="true" autodetectIP="true"> 
	<servername>localhost</servername> 
</servernames> 
…… 

  

  

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

 

Xml代码 复制代码
  1. ……    
  2.  <postmaster>Postmaster@tuping.com</postmaster>
  3. ……    
  4. <servernames autodetect="false" autodetectIP="false">    
  5.     <servername>tuping.com</servername>    
  6. </servernames>    
  7. ……  
Xml代码 复制代码
  1. ……    
  2. <postmaster>Postmaster@lixiaobo.com</postmaster>    
  3. ……    
  4. <servernames autodetect="false" autodetectIP="false">    
  5.     <servername>lixiaobo.com</servername>    
  6. </servernames>    
  7. ……  
…… 
<postmaster>Postmaster@tuping.com</postmaster> 
…… 
<servernames autodetect="false" autodetectIP="false"> 
	<servername>tuping.com</servername> 
</servernames> 
……

   

        找到

<mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor">    
  1.     <processor> relay-denied </processor>    
  2.     <notice>550 - Requested action not taken: relaying denied</notice>    
  3. </mailet>  

        将其更改,结果如下:

 
 <!-- update the configuration to include your own network/addresses.  The matcher can be configured with a comma separated list of IP addresses,wildcarded IP subnets, and wildcarded hostname subnets. -->
         <!-- e.g. "RemoteAddrNotInNetwork=127.0.0.1, abc.de.*, 192.168.0.*"-->
<mailet match="RemoteAddrNotInNetwork=127.0.0.1,192.168.61.135" class="ToProcessor"> 
	<processor> relay-denied </processor> 
	<notice>550 - Requested action not taken: relaying denied</notice> 
</mailet-->

     在原来的127.0.0.1后面加上本机的IP地址

         找到下面元素,去掉其注释

Xml代码 复制代码
  1. <authRequired>true</authRequired>  
Xml代码 复制代码
  1. <authRequired>true</authRequired>  
<authRequired>true</authRequired>

  

 

         这样邮箱访问需要帐号验证,你不希望别人用你的帐号收发消息吧……^_^

        

         如此,James服务配置已经完成。

 

找到:

  <!-- Check for delivery from a known spam server -->
         <!-- This set of matchers/mailets redirect all emails from known -->
         <!-- black holes, open relays, and spam servers to the spam processor -->
         <!-- For this set to function properly, the spam processor must be configured.
        
   该段代码是我注释掉的
   <mailet match="InSpammerBlacklist=dnsbl.njabl.org."
                 class="ToProcessor">
           <processor> spam </processor>
           <notice>550 Requested action not taken: rejected - see http://njabl.org/ </notice>
         </mailet> 

这段代码表示dnsbl.njabl.org组织将会检查本机地址,如果包括在黑名单中,那么该邮件服务器将邮件作为垃圾邮件处理,我就是在这卡了半天。注释掉该段代码就OK了

        6。创建邮件帐号

        创建邮件帐号后,就可以用来收发邮件了。James的账号管理是通过基于Telnet客户机的远程管理器,这点颇为不爽,尤其是我的操作系统下的命令行控制台是不显示telnet命令输入字符的,经常出错。

 

        现在进入命令行控制台,在telnet localhsot 4555 进入James管理器,操作如下:

  复制代码
  1. C:\Documents and Settings>telnet localhost 4555   
C代码 复制代码
  1. C:\Documents and Settings>telnet localhost 4555   
C:\Documents and Settings>telnet localhost 4555 

  

 

        将进入

  复制代码
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:
Password:
Welcome root. HELP for a list of commands  

 

       创建新用户的命令是:adduser username password

    这里创建了两个账户来作为演示使用: leo/123,crb/123

 

代码示例:

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值