Java+nginx implement Pop, IMAP, SMTP mailbox Agent service _java

Source: Internet
Author: User
Tags auth imap ldap md5 unpack tomcat tomcat server

This article describes the Java+nginx implementation of Pop, IMAP, SMTP mailbox Agent service, we use the environment for CENTOS7, Java program we run through the Eclipse exported war package under the Linux under Tomcat execution, see below:

Environment Introduction:

Hostname:java.iternalsoft.com

ip:192.168.2.163

Roles:proxy Server Os:centos7

We modify the newly installed server information by using the following command:

Hostnamectl set-hostname customname Modify computer name

Systemctl Stop firewalld.serice shutdown Firewall service

Systemctl Firewall.service prohibit Firewall service from randomly starting

systemctl stop postfix stop postfix service systemctl disable postfix

disable postfix service randomly start

Vim/etc/selinux/config Modify the SELinux state to Disabled

We first install the Nginx service and we need to install the Nginx warehouse before we install Nginx.

Yum Install

http://nginx.org/packages/centos/7/noarch/RPMS/

nginx-release-centos-7-0.el7.ngx.noarch.rpm

After the repo warehouse was installed, we started to install Nginx.

Yum Install Nginx

Next, look at the installation default path

/etc/nginx/nginx.conf

Find-name Nginx

Next we're going to configure the Nginx configuration file

vim/etc/nginx/nginx.conf Default configuration file

In order to facilitate the need to clear the contents of the configuration file, use the following command to complete

Echo >/etc/nginx/nginx.conf Clears the contents of the nginx.conf

But in order to configure it completely, we recommend that you first back up

Cp/etc/nginx/nginx.conf/etc/nginx/cginx.conf.bak

And then run it again.

echo >/etc/nginx/nginx.conf Empty configuration file

Then edit the configuration file again

Vim/etc/nginx/nginx.conf

Add the following code, and then modify the contents of the code according to your environment to save it

User Nginx;

Worker_processes 4;

Pid/var/run/nginx.pid;

events{

worker_connections 1024;

} error_log/var/log/nginx/error.log info;

Mail {

server_name java.abc.com;

Auth_http http://localhost:8080/imail/index.jsp;

Imap_capabilities IMAP4rev1 uidplus IDLE literal+ QUOTA;

Pop3_auth plain apop cram-md5;

Pop3_capabilities last top USER pipelining UIDL;

Smtp_auth login plain cram-md5;

Smtp_capabilities "SIZE 10485760" Enhancedstatuscodes 8BITMIME DSN;

Xclient off;

server {

listen;

Protocol POP3;

Proxy on;

Proxy_pass_error_message on;

}

server {

Listen 143;

protocol IMAP;

Proxy on;

}

server {

listen;

protocol SMTP;

Proxy on;

}



Paste Save

Next set up the Nginx service

Systemctl Enable Nginx.service nginx service open random Boot

Systemctl Start Nginx.service nginx service startup

Systemctl status Nginx.service nginx service running status

The next step is to install the Java Runtime Environment (JDK), the Java Runtime Environment, where Tomcat is installed

The first thing to be sure is that Linux is typically a JDK with a Java environment

The next step is to install a new JDK file, and first we run the command

Uname-a

View the system version and the number of digits is 32 or 64 bits

If there is a x86_64 64-bit operating system, if not, that's 32-bit.

Download the installation JDK

Http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Wget

Http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.tar.gz? Authparam=1433902875_8682334a55c7231fd0cb3cdbc5c9dcc9

We view the downloaded file through LS, and then we need to unpack

TAR-ZXVF jdk1.8.0 .....

Decompression complete, no installation required

Then we go to the path through the CD command

Create Java directories in/usr/, and then move the extracted JDK to that directory

cd/usr/into usr directory.

Mkdir Java Create Java folder

Then we move the jdk1.8 of the current directory to the Java directory.
1
Mv Jdk1.8.xx/usr/java

Vim/etc/profile Editing Environment variables
Adding environment variables
Java_home=/usr/java/jdk1.8.0_45
Jre_home=/usr/java/jdk1.8.0_45/jre
Path= $PATH: $JAVA _home/bin: $JRE _home/bin
Classpath=.: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar: $JRE _home/lib
Export Java_home jre_home PATH CLASSPATH

We save and exit, then reboot the system and find that the JDK version is already our newly installed version.

Next, we need to install Tomcat.
The first is to download
http://tomcat.apache.org/download-80.cgi
After we open the Tomcat website to find the right installation package, right click on the copy download link, via wget online download
Wget
Http://apache.dataguru.cn/tomcat/tomcat-8/v8.0.23/bin/apache-tomcat-8.0.23.tar.gz

Download complete


After downloading, we can begin to unpack the
TAR-ZXVF apache_tomcat1.8.-0 tar.gz

Decompression complete

We need to move the Apache-tomcat file to the/usr/.

Mv APACHE-TOMCAT/USR

Next, start the Tomcat service

Cd/usr/apache-tomcat18.0.23/bin

./startup.sh

Then we look at the Tomcat related service ports

Netstat-anlpt

CENTOS7 requires separate installation of Net-tools services

Yum install-y Net-tools

Netstat-anlpt

Then we use IE browser to access

We can also modify the default service port by Apache-tomcat

Under the Apache-tomcat/conf/server.xml

 Hashenv.put (context.security_authentication, "simple");//LDAP Access security level (None,simple,strong
) Hashenv.put (Context.security_principal, "cn=" +username+ ", O=beyondsoft"); Hashenv.put (Context.security_principal, username); The user name of Ad hashenv.put (context.security_credentials, password);
Ad password Hashenv.put (context.initial_context_factory, "com.sun.jndi.ldap.LdapCtxFactory"); Hashenv.put (Com.sun.jndi.ldap.connect.timeout, 3000),//connection timeout set to 3 seconds Hashenv.put (Context.provider_url, "ldap://" + host+ ":" +port)//default port 389 try {ctx = new InitialDirContext (hashenv);//initialization context Attributes Attrs = ctx.getattributes ("cn=" +
Username+ ", O=beyondsoft");
System.out.println ("mailserver:" + attrs.get ("MailServer"). get ());
String Attdomain=attrs.get ("Mail"). Get (). toString ();
String attserver=attcn.split (",") [0].split ("=") [1]+ "." + attdomain.substring (Attdomain.indexof ("@") +1);
System.out.println (Inet4address.getbyname (attserver). gethostaddress ()); Return Attcn.split (",") [0].split ("=") [1]; 

And then we need to customize Java to run the program, which is actually. War Package

After we have defined the program, we export the code of the program as a. War package, and then copy it to the WebApp under the Tomcat installation directory by WINSCP or mount, and start the Tomcat server system to automatically decompress the war package

After the Tomcat service started, we accessed through the browser, prompted 500 pages, in fact, 500 of the page is normal, because we are in the program is a POST request transmission, so it is unable to open the page,

Next we have a convenient test code whether there is a problem, we have to test the following command:
Curl-i-H ' auth-user:iiosoft '-h ' auth-pass:123 '-h ' auth-protocol:pop3 '
http://localhost:8080/imail/Index.jsp

We test that you can return normal information such as auth-sever,auth-status,auth-ports,auth-user,auth-pass, so no problem, we can do this through Telnet mailserver 110 of the way to test validation, but found in the verification process can not be validated, the code is really no problem, then how can verify failure?

We find data to confirm that Nginx need to return through LDAP is auth-server must be the IP address of the server, if the name of the server can not be resolved, then we can only through the conversion in the code, the default return is the server name

Hashenv.put (Context.security_authentication, "simple");
LDAP access security level (None,simple,strong) hashenv.put (Context.security_principal, "cn=" +username+ ", O=beyondsoft"); Hashenv.put (Context.security_principal, username); The user name of Ad hashenv.put (context.security_credentials, password);
Ad password Hashenv.put (context.initial_context_factory, "com.sun.jndi.ldap.LdapCtxFactory"); Hashenv.put (Com.sun.jndi.ldap.connect.timeout, 3000),//connection timeout set to 3 seconds Hashenv.put (Context.provider_url, "ldap://" + host+ ":" +port)//default port 389 try {ctx = new InitialDirContext (hashenv);//initialization context Attributes Attrs = ctx.getattributes ("cn=" +
Username+ ", O=beyondsoft");
System.out.println ("mailserver:" + attrs.get ("MailServer"). get ());
System.out.println ("mailserver:" + Attrs.get ("Mail"). get ());
String Attdomain=attrs.get ("Mail"). Get (). toString ();
String attcn=attrs.get ("MailServer"). Get (). toString ();
String attserver=attcn.split (",") [0].split ("=") [1]+ "." + attdomain.substring (Attdomain.indexof ("@") +1); System.out.println (INET4ADDress.getbyname (Attserver). gethostaddress ());
Return Attcn.split (",") [0].split ("=") [1];


 Return Inet4address.getbyname (Attserver). Gethostaddress (). toString ();

Next we fix the problem by modifying the code:
How to solve, before we initialize, think of LDAP to get the user's mailserver and mailaddress value, the format is a1/server,user@domain.com, then we can take A1 on the left, the right to take the content after @ And then stitching again is a server address. A1.domain.com Finally, we can use Inetaddress.getbyname to convert to IP, OK

After the conversion to IP, we test again;

We also test through Outlook

After the test was successful, we pointed to 192.168.2.163 by adding a resolution in DNS, and then we tested it again through the domain name
If you want Nginx to proxy SMTP, we need to edit nginx.conf add config file
server {
Listen 25;
protocol SMTP;
Smtp_auth Login Plain;
Xclient off;
}
Note: We add the SMTP proxy to the Nginx, so we added the SMTP server zone

We added a reboot to the discovery prompt Nginx reboot failed

We think that the port has been used, think of the system from the Postfix is started
Systemctl stop Postfix
Systemctl Disable Postfix
And then we'll reboot and start Nginx.

We are testing through Outlook configuration

The above is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.