Apache James 系列1 --- 安装

一、环境

1. 版本

(1) CentOS 7.9 2009;

(2) Apache James 3.7.3;

(3) 域名:假设我们使用test.com作为我们的邮箱域名。

2. 准备

(1) 下载

可前往https://james.apache.org/download.cgi查看最新的版本:

wget https://dlcdn.apache.org/james/server/3.7.3/james-server-spring-app-3.7.3-app.zip

(2) 创建安装目录

sudo mkdir -p /opt/apache-james

(3) 解压

sudo unzip james-server-spring-app-3.7.3-app.zip -d /opt/apache-james
sudo mv /opt/apache-james/james-server-spring-app-3.7.3/* /opt/apache-james
sudo rm -rf /opt/apache-james/james-server-spring-app-3.7.3

二、安装与配置

Apache James是使用Java编写的程序,解压包中核心是lib目录下的james-server-spring-app-3.7.3.jar文件,无需使用yum install命令安装,我们需要做的是修改其相关配置。

1. 域名配置

(1) 编辑配置

sudo vi /opt/apache-james/conf/domainlist.xml

(2) 配置域名

<!-- JPA implementation for DomainList -->
<domainlist class="org.apache.james.domainlist.jpa.JPADomainList">
   <autodetect>false</autodetect>
   <autodetectIP>false</autodetectIP>
   <defaultDomain>test.com</defaultDomain>
</domainlist>

需要将配置中的defaultDomain改为自己的域名。

如果该James实例需要同时支持多个不同的域名,则需要配置domainnames节:

<domainlist class="org.apache.james.domainlist.xml.XMLDomainList">
   <domainnames>
       <domainname>mail1.com</domainname>
       <domainname>mail2.com</domainname>
   </domainnames>
   <autodetect>true</autodetect>
   <autodetectIP>true</autodetectIP>
   <defaultDomain>test.com</defaultDomain>
</domainlist>

2. MySQL配置

(1) 创建数据库

mysql-> create database if not exists james default character set UTF8MB4;

(2) 编辑配置

sudo vi /opt/apache-james/conf/james-database.properties

(3) 配置MySQL

Apache James默认使用DERBY数据库来存储邮件数据,我们需要将其默认数据库链接注释,并添加新的MySQL配置;

a. 注释以下配置

#database.driverClassName=org.apache.derby.jdbc.EmbeddedDriver
#database.url=jdbc:derby:../var/store/derby;create=true
#database.username=app
#database.password=app

b. 修改以下配置

# Supported adapters are:
# DB2, DERBY, H2, HSQL, INFORMIX, MYSQL, ORACLE, POSTGRESQL, SQL_SERVER, SYBASE 
vendorAdapter.database=MYSQL

c. 添加MySQL配置

database.driverClassName=com.mysql.cj.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/james?useUnicode=true&useSSL=false&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=UTC
database.username=[UserName]
database.password=[Password]

注意修改MySQL的服务器IP地址、账号和密码。

(4) 下载MySQL的Java驱动

wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.30/mysql-connector-java-8.0.30.jar
sudo mv mysql-connector-java-8.0.30.jar /opt/apache-james/lib

3. Mailet配置

(1) 编辑配置

sudo vi /opt/apache-james/conf/mailetcontainer.xml

mailetcontainer.xml由若干处理器(processor)组成,每个处理器都负责实现相应的功能。

(2) 管理员邮箱

    <!-- MailAddress used for PostMaster -->
    <context>
        <!-- When the domain part of the postmaster mailAddress is missing, the default domain is appended.
        You can configure it to (for example) <postmaster>postmaster@myDomain.com</postmaster> -->
        <postmaster>postmaster@test.com</postmaster>
    </context>

这里是配置邮箱管理员账号的地方,注释告诉我们,如果我们不填写@后的域名部分,我们在domainlist.xml中配置的defaultDomain将被作为默认的管理员邮箱域名,合起来就是:

postmaster@localhost

(3) 注释值为file://*的repositoryPath节点,释放值为db://*的repositoryPath节点

如果配置节仅包含值为file://*的repositoryPath节点,则不做处理。

我们以“错误处理器”为例,当出现非预期的错误时,将会调用该错误处理器:

    <processor state="error" enableJmx="true">
       <!-- If you want to notify the sender their message generated an error, uncomment this       -->
       <!--
       <mailet match="All" class="Bounce"/>
         -->
       <!-- If you want to notify the postmaster that a message generated an error, uncomment this  -->
       <!--
       <mailet match="All" class="NotifyPostmaster"/>
         -->

       <!-- Logs any messages to the repository specified -->
       <mailet match="All" class="ToRepository">
          <!-- <repositoryPath>file://var/mail/error/</repositoryPath> -->
          <!-- An alternative database repository example follows. -->
          <repositoryPath>db://maildb/deadletter/error</repositoryPath>
       </mailet>
    </processor>

(4) 注销RemoteAddrNotInNetwork所在行

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

4. SMTP配置

(1) 编辑配置

sudo vi /opt/apache-james/conf/smtpserver.xml

(2) helloname

在"smtpservers->smtpserver"下找到已经被注释的"helloName"节点,解开其注释并配置它的值为邮箱服务域名

        <!-- This is the name used by the server to identify itself in the SMTP -->
        <!-- 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">test.com</helloName>

(3) SMTP认证

在"smtpservers->smtpserver"下找到已经被注释的"authRequired"节点,解开其注释:

        <authRequired>true</authRequired>

(4) 发件人地址校验

在"smtpservers->smtpserver"下找到已经被注释的"verifyIdentity"节点,解开其注释:

<verifyIdentity>true</verifyIdentity>

该配置用于校验己方发件人的地址信息。

5. LDAP 配置

我们使用LDAP的用户作为我们收发邮件的用户,也就是Apache James的用户。

(1) 编辑配置

sudo vi /opt/apache-james/conf/usersrepository.xml

(2) 注释掉原有的usersrepository块:

注意,在添加注释之前,usersrepository块内部也有注释,所以这里可以把整个块删除,也可以分别添加两段注释。

(3) 在usersrepository块下方添加以下配置:

<!-- Read-Only LDAP based UsersRepository -->
<repository name="LocalUsers" 
    class="org.apache.james.user.ldap.ReadOnlyUsersLDAPRepository" 
    ldapHost="ldap://127.0.0.1:389" 
    principal="cn=admin,dc=example,dc=com" credentials="password"
    userBase="ou=People,dc=example,dc=com" userIdAttribute="uid"
    userObjectClass="inetOrgPerson" maxRetries="20" retryStartInterval="0" retryMaxInterval="30" retryIntervalScale="1000">
        <UsersDomain>example.com</UsersDomain>  
        <LDAPRoot>dc=example,dc=com</LDAPRoot> 
        <MailAddressAttribute>mail</MailAddressAttribute> 
        <IdentityAttribute>uid</IdentityAttribute> 
        <AuthenticationType>simple</AuthenticationType>
        <ManagePasswordAttribute>TRUE</ManagePasswordAttribute> 
        <PasswordAttribute>userPassword</PasswordAttribute> 
</repository>

6. 运行

sudo chmod +x /opt/apache-james/bin/run.sh
cd /opt/apache-james/bin
sudo ./run.sh

(1) 启动成功后:

7. 运行服务

(1) 添加用户:

sudo useradd james

(2) 赋权:

sudo chown -R james:james /opt/apache-james

(3) 编辑运行者:

sudo vi /opt/apache-james/conf/wrapper.conf

更新RUN_AS_USER:

RUN_AS_USER=james

(4) 创建软连接:

sudo ln -s /opt/apache-james/bin/james /etc/init.d

(5) 开机启动:

sudo chkconfig --add james
sudo chkconfig james on

(6) 启动服务:

sudo systemctl start james

三、SSL

1. 准备工作

(1) 首先需要申请公网认可的证书

Letsencrypt

(2) 创建Keystore

OpenSSL 系列2 --- 应用

(3) 移动keystore:

sudo mv -f keystore.p12 /opt/apache-james/conf

2. smtpserver.xml配置

(1) 编辑配置

sudo vi /opt/apache-james/conf/smtpserver.xml

(2) 更新tls部分如下:

<tls socketTLS="false" startTLS="true">
    <keystore>file://conf/keystore</keystore>
    <keystoreType>PKSC12</keystoreType>
    <secret>密码</secret>
    <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider>
    <algorithm>SunX509</algorithm>
</tls>

注意:keystoreType是可选配置,默认为JKS,下同。

3. imapserver.xml配置

(1) 编辑配置:

sudo vi /opt/apache-james/conf/imapserver.xml

(2) 更新tls部分如下:

<tls socketTLS="false" startTLS="true">
  <keystore>file://conf/keystore.jks</keystore>
  <keystoreType>PKSC12</keystoreType>
  <secret>密码</secret>
  <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider>
</tls>

4. pop3server.xml配置(可选)

(1) 编辑配置

sudo vi /opt/apache-james/conf/pop3server.xml

(2) 更新tls部分如下:

<tls socketTLS="false" startTLS="true">
    <keystore>file://conf/keystore.jks</keystore>
    <keystoreType>PKSC12</keystoreType>
    <secret>密码</secret>
    <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider>
</tls>

5. 驱动

(1) 下载

wget http://www.docjar.com/jar/sunjce_provider.jar
wget https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/8.2.0/mysql-connector-j-8.2.0.jar

(2) 复制到Apache James目录下

sudo mv sunjce_provider.jar /opt/apache-james/lib
sudo mv mysql-connector-j-8.2.0.jar /opt/apache-james/lib

6. 开放端口

(1) 查看端口开放

firewall-cmd --list-all

(2) 开放端口

sudo firewall-cmd --permanent --add-port=25/tcp
sudo firewall-cmd --permanent --add-port=465/tcp
sudo firewall-cmd --permanent --add-port=143/tcp
sudo firewall-cmd --permanent --add-port=993/tcp
sudo firewall-cmd --reload

如果部署在云环境,需要同步设置安全策略。

(3) 关闭postfix

systemctl status postfix
sudo systemctl stop postfix
sudo systemctl disable postfix

(4) 重启James

systemctl status james
sudo systemctl restart james

7. 关于25端口

当你在云服务商(Ali, Huawei, Azure, Amazon)购买到一个服务器并按照以上配置部署完Apache James之后,你会发现你可以收外网邮箱(163, QQ, Hotmail, Gmail)的邮件,你也可以通过James给内网的其他账户发送邮件,但是你无法给外网邮箱(163, QQ, Hotmail, Gmail)发送邮件。

原因是这些云服务平台限制了25端口的出站访问请求,大部分云平台直接建议你购买专业邮箱服务平台的商业化产品(SendGrid、163、阿里云企业邮箱);部分云平台允许你提交解封申请(比如阿里云、七牛云),可百度“阿里云 25”;部分云平台根据你登录账户的角色自动进行判断是否可以给解封25端口(Azure允许Enterprise的账户解封)。

8. 日志

(1) 授权

sudo chmod -R 755 /opt/apache-james/log

(2) Service日志,用于查看启动过程中是否有报错:

tail -n 500 /opt/apache-james/log/wrapper.log

(3) 手动运行日志:

tail -n 500 /opt/apache-james/log/james.log

四、参考

1. 官方

https://james.apache.org/server/config.html

https://james.apache.org/server/config-ssl-tls.html

2. 其他

https://blog.csdn.net/qq_29427355/article/details/127083676

https://robertmunn.com/categories/apache-james/

https://nozaki.me/roller/kyle/entry/installing-james-as-a-service

https://serverfault.com/questions/408758/using-apache-james-mail-server-with-ldap

https://www.cyberithub.com/chkconfig-command-examples/

https://nozaki.me/roller/kyle/entry/installing-james-as-a-service

posted @ 2023-02-19 19:08  白马黑衣  阅读(1362)  评论(0编辑  收藏  举报