Apache James簡單郵件服務器配置

#編者注 以前的老系統使用的是windows上的hmail服務器,因爲界面感官好,配置迅速,則爲了上線使用。但度過了3年左右,原始配置hmail的人員離職。而且因爲Windows的問題,並不可以和Linux系統上的軟件打包。而且,因爲近期斷電事故致使風險加重。因此,須要搭建簡單易用的Mail服務器。html

#Apache James Apache James項目提供了一組豐富的開放源代碼的模塊和庫,由Java編寫,互聯網郵件通訊建設成爲先進的企業郵件服務器相關。java

#下載Jamesmysql

#JAVA_HOME [20161102]因爲本人第二次的是使用的是mac系統,安裝的JDK8,James找不到JAVA_HOME。須要在James當中添加。 ##JAVA_HOME添加到run.sh - 失敗 因爲James經過bin/run.sh運行,首先嚐試的是在run.sh中添加JAVA_HOME,以下內容是在run.sh頭部進行添加sql

# -- zhanpeng for mac config JAVA_HOME--
JAVA_HOME=`/usr/libexec/java_home`
echo $JAVA_HOME

運行結果數據庫

vfxdeiMac:bin zhanpeng$ ./run.sh 
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
ERROR: JAVA_HOME not found in your environment.

Please, set the JAVA_HOME variable in your environment to match the
location of the Java Virtual Machine you want to use.

確認,實際的James的執行是使用phoenix.sh進行執行 ##JAVA_HOME添加到phoenix.sh 在bin/phoenix.sh添加JAVA_HOMEapache

# -- zhanpeng for mac config JAVA_HOME--
JAVA_HOME=`/usr/libexec/java_home`

運行結果windows

vfxdeiMac:bin zhanpeng$ ./run.sh 
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
Using PHOENIX_HOME:   /Users/zhanpeng/build/james/james-2.3.2.1
Using PHOENIX_TMPDIR: /Users/zhanpeng/build/james/james-2.3.2.1/temp
Using JAVA_HOME:      /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
Running Phoenix: 

Phoenix 4.2

James Mail Server 2.3.2.1
Remote Manager Service started plain:4555

#Component named "pop3server" failed to pass through the Starting stage Mac和Ubuntu系統限制普通用戶使用1024如下的端口,使用管理員權限運行,就不會發生問題服務器

sudo ./run.sh

#修改James User配置爲JDBC **注意:**以下內容在第一次運行james以後纔會出現,James使用的是DBCP鏈接池,默認配置支持Apache Derby、MySQL、MSSQL、HypersonicSQL,查看conf/sqlResources.xml還支持其餘數據庫。請自行查看 ##修改repository 修改配置文件james-2.3.2.1/apps/james/SAR-INF/config.xml,這裏咱們須要注意,把文件類型的repository註釋掉,替換成數據庫repositoryapp

<!--
      <repository name="LocalUsers" class="org.apache.james.userrepository.UsersFileRepository">
         <destination URL="jdbc:sqlite://D:/users.sqlite"/>
      </repository>
	  -->

      <!-- Database backed user repositories -->
      <!-- -->
      <!-- Use these configurations to store user info in a database. -->
      <!-- Note: The <data-source> element must refer to a connection configured -->
      <!--       in the <database-connections> configuration section. -->

      <!-- The LocalUsers repository, for storing James' User info. -->
      <repository name="LocalUsers" class="org.apache.james.userrepository.JamesUsersJdbcRepository" destinationURL="db://maildb/users">
         <sqlFile>file://conf/sqlResources.xml</sqlFile>
      </repository>

##JDBC配置 取消註釋,並按照jdbc url的格式配置ide

<!-- JDBC driver .jar libraries for other RDBMS can be placed in ~james/lib/  -->

      <!-- You can download latest Connector/J from   -->
      <!-- http://dev.mysql.com/downloads/connector/j/3.1.html -->
      <!-- -->
      <!-- Example, connecting to a MySQL database called "mail" on localhost-->
      <!-- -->
      <!-- The max value is the maximum number of concurrent connections James will -->
      <!-- open to this database-->
      <!-- If you see "SQLException: Giving up... no connections available." in your -->
      <!-- log files or bounced mail you should increase this value -->

      <data-source name="maildb" class="org.apache.james.util.dbcp.JdbcDataSource">
         <driver>com.mysql.jdbc.Driver</driver>
         <dburl>jdbc:mysql://192.168.1.200/james_user?autoReconnect=true</dburl>
         <user>username</user>
         <password>password</password>
         <max>20</max>
      </data-source>

##添加JDBC驅動到lib 因爲編者連接的MySQL兼容mysql-connector-java-5.1.40.jar
添加mysql-connector-java-5.1.40.jarjames-2.3.2.1/lib目錄,jdbc驅動程序能夠去mvnrepo網站下載。

##輸出

vfxdeiMac:bin zhanpeng$ sudo ./run.sh 
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
Using PHOENIX_HOME:   /Users/zhanpeng/build/james/james-2.3.2.1
Using PHOENIX_TMPDIR: /Users/zhanpeng/build/james/james-2.3.2.1/temp
Using JAVA_HOME:      /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
Running Phoenix: 

Phoenix 4.2

James Mail Server 2.3.2.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的用戶內容,在數據庫當中建立了

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `username` varchar(64) NOT NULL,
  `pwdHash` varchar(50) DEFAULT NULL,
  `pwdAlgorithm` varchar(20) DEFAULT NULL,
  `useForwarding` smallint(6) DEFAULT NULL,
  `forwardDestination` varchar(255) DEFAULT NULL,
  `useAlias` smallint(6) DEFAULT NULL,
  `alias` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

#修改James Inbox配置爲JDBC

<!-- The inbox repository is the location for users inboxes -->
      <!-- Default setting: file based repository - enter path ( use  "file:///" for absolute) -->
<!--
      <inboxRepository>
         <repository destinationURL="file://var/mail/inboxes/" type="MAIL"/>
      </inboxRepository>
-->

      <!-- Alternative inbox repository definition for DB use. -->
      <!-- The format for the destinationURL is "db://<data-source>/<table>" -->
      <!-- <data-source> is the datasource name set up in the database-connections block, below -->
      <!-- <table> is the name of the table to store user inboxes in -->
      <!-- The user name is used as <repositoryName> for this repository config. -->
      
      <inboxRepository>
         <repository destinationURL="db://maildb/inbox/" type="MAIL"/>
      </inboxRepository>
      

      <!-- Alternative inbox repository definition for DB use. -->
      <!-- Stores message body in file system, rest in database -->
      <!--
      <inboxRepository>
         <repository destinationURL="dbfile://maildb/inbox/" type="MAIL"/>
      </inboxRepository>
      -->

      <!-- Alternative inbox repository definition for mbox use. -->
      <!-- This method uses UNIX standard mbox files and is meant for people using mbox files -->
      <!-- with systems such as mail list archive displayers -->
      <!-- Note that dot-locking is not currently supported -->
      <!-- so network (write) accesses may cause mbox corruption -->
      <!-- the sample mbox URL is an absolute URL; mbox:///var/mail will put the users mbox files in /var/mail/-->
      <!--
      <inboxRepository>
         <repository destinationURL="mbox:///var/mail/" type="MAIL"/>
      </inboxRepository>
      -->

inbox表結構

DROP TABLE IF EXISTS `inbox`;
CREATE TABLE `inbox` (
  `message_name` varchar(200) NOT NULL,
  `repository_name` varchar(100) NOT NULL,
  `message_state` varchar(30) NOT NULL,
  `error_message` varchar(200) DEFAULT NULL,
  `sender` varchar(255) DEFAULT NULL,
  `recipients` text NOT NULL,
  `remote_host` varchar(255) NOT NULL,
  `remote_addr` varchar(20) NOT NULL,
  `message_body` longblob NOT NULL,
  `message_attributes` longblob,
  `last_updated` datetime NOT NULL,
  PRIMARY KEY (`repository_name`,`message_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

#修改James Spool配置爲JDBC Spool repository是收到的郵件臨時存儲的本地位置

<!-- Spool repository configuration -->
   <!-- The spool repository is the location where incoming mails are temporarily stored -->
   <!-- before being processed. -->
   <!--
   <spoolrepository destinationURL="file://var/mail/spool/" type="SPOOL"/>
   -->

   <!-- Alternative spool repository definition for JDBC use -->
   
   <spoolrepository destinationURL="db://maildb/spool/spool" type="SPOOL"/>
   

   <!-- Alternative spool repository definition for JDBC use -->
   <!-- Stores message body in file system, rest in database -->
   <!--
   <spoolrepository destinationURL="dbfile://maildb/spool/spool" type="SPOOL"/>
   -->

數據庫表結構

DROP TABLE IF EXISTS `spool`;
CREATE TABLE `spool` (
  `message_name` varchar(200) NOT NULL,
  `repository_name` varchar(100) NOT NULL,
  `message_state` varchar(30) NOT NULL,
  `error_message` varchar(200) DEFAULT NULL,
  `sender` varchar(255) DEFAULT NULL,
  `recipients` text NOT NULL,
  `remote_host` varchar(255) NOT NULL,
  `remote_addr` varchar(20) NOT NULL,
  `message_body` longblob NOT NULL,
  `message_attributes` longblob,
  `last_updated` datetime NOT NULL,
  PRIMARY KEY (`repository_name`,`message_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

#查看並修改James遠程管理的用戶名密碼 查看配置文件,能夠看到以下內容,請修改管理密碼

<!-- The RemoteManager server is enabled by default -->
   <!-- Disabling blocks will stop them from listening, -->
   <!-- but does not free as many resources as removing them would -->
   <remotemanager enabled="true">
      <port>4555</port>
      <!--  Uncomment this if you want to bind to a specific inetaddress -->
      <!--
      <bind> </bind>
      -->
      <!--  Uncomment this if you want to use TLS (SSL) on this port -->
      <!--
      <useTLS>true</useTLS>
      -->
      <handler>
         <!-- This is the name used by the server to identify itself in the RemoteManager -->
         <!-- protocol.  If autodetect is TRUE, the server will discover its -->
         <!-- own host name and use that in the protocol.  If discovery fails, -->
         <!-- the value of 'localhost' is used.  If autodetect is FALSE, James -->
         <!-- will use the specified value. -->
         <helloName autodetect="true">myMailServer</helloName>
         <administrator_accounts>
<!-- CHECKME! -->
            <!-- Change the default login/password. -->
            <account login="root" password="!changeme!"/>
         </administrator_accounts>
         <connectiontimeout> 60000 </connectiontimeout>
         <!-- The prompt directive adds a prompt to every output from RemoteManager -->
         <!-- 
         <prompt>james&gt;</prompt>
         -->
      </handler>
   </remotemanager>

#telnet登陸並添加用戶

vfxdeiMac:SAR-INF zhanpeng$ telnet localhost 4555
Trying ::1...
Connected to localhost.
Escape character is '^]'.
JAMES Remote Administration Tool 2.3.2.1
Please enter your login and password
Login id:
root
Password:
!changeme!
Welcome root. HELP for a list of commands
help
Currently implemented commands:
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]              removes 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

adduser wangyinghan 123
User wangyinghan added

#使用thunderbird連接 異常提示

接收:POP3,xxx,未加密
發送:SMTP,xxx,未加密
用戶名:xxx

查詢James文檔,得知須要修改useTLS值爲ssl POP3

<pop3server enabled="true">
      <!-- port 995 is the well-known/IANA registered port for POP3S  ie over SSL/TLS -->
      <!-- port 110 is the well-known/IANA registered port for Standard POP3 -->
      <port>110</port>

      <!-- Uncomment this if you want to bind to a specific inetaddress -->
      <!--
      <bind> </bind>
      -->
      <!--  Uncomment this if you want to use TLS (SSL) on this port -->
      
      <useTLS>true</useTLS>
      

      <handler>
         <!-- This is the name used by the server to identify itself in the POP3 -->
         <!-- protocol.  If autodetect is TRUE, the server will discover its -->
         <!-- own host name and use that in the protocol.  If discovery fails, -->
         <!-- the value of 'localhost' is used.  If autodetect is FALSE, James -->
         <!-- will use the specified value. -->
         <helloName autodetect="true">myMailServer</helloName>
         <connectiontimeout>120000</connectiontimeout>
      </handler>
   </pop3server>

SMTP

<smtpserver enabled="true">
      <!-- port 25 is the well-known/IANA registered port for SMTP -->
      <port>25</port>

      <!-- Uncomment this if you want to bind to a specific inetaddress -->
      <!-- Please NOTE: you should add this IP also to your RemoteAddrNotInNetwork -->
      <!-- in order to avoid relay check for locallly generated bounces -->
      <!--
      <bind> </bind>
      -->
      <!-- Uncomment this if you want to use TLS (SSL) on this port -->
      
      <useTLS>true</useTLS>

RemoteManager

<remotemanager enabled="true">
      <port>4555</port>
      <!--  Uncomment this if you want to bind to a specific inetaddress -->
      <!--
      <bind> </bind>
      -->
      <!--  Uncomment this if you want to use TLS (SSL) on this port -->
      
      <useTLS>true</useTLS>

##問題 - 沒法啓動 查找文檔須要安裝ssl,並和http服務器去連接,超出範圍。暫時不連接

#關於郵件收發的測試 ##未修改 - localhost A在本機登陸A@localhost 登陸正常
A@localhost 發送給 A@localhost 收發正常
B在外機登陸B@locahost 不能登陸
B在外機登陸B@A_IP 登陸征程
B@A_IP 發送給 A@localhost 收發正常 - 可是沒法修改爲B@localhost

##localhost修改成IP A在本機登陸A@localhost 登陸正常 A在本機登陸A@IP 登錄正常 B在外機登陸B@IP 登錄正常 A收發正常 B收發正常

相關文章
相關標籤/搜索