This article introduces Java+Nginx to implement POP, Imap, SMTP mailbox email agency services. The environment we use this time is under CentOS7. The
Environment introduction:
Hostname: java.iternalsoft.com
IP: 192.168.2.163
Roles: Proxy Server OS: CentOS7
We modify the newly installed server information through the following command:
HostnameCTL SETNAME CUSTOMNAME Modify the computer name SystemCtl Stop Firewalld.Serice SystemCtl Disable Firewall.service. Stop the postfix service SystemCTL Disable Postfix disable the postfix service randomly start VIM/ETC/SELINUX/Config modify SELINUX The state is Disabled
First of all, we installed the Nginx service. Before installing Nginx, we need to install the warehouse of Nginx
Yum installhttp: //nginx.org/packages/centos/noarch/rpms/nginx-rease-quetos-7-0.ngx.noarch.rpm
After the repo warehouse was installed, we started to install Nginx
Yum install nginx
Next, check the default path
/etc/nginx/nginx.conf
Find -Name nginx
Next, we have to configure the configuration file of nginx
Vim /etc/nginx/nginx.conf The default configuration file
In order to facilitate the need to clear all the contents in the configuration file, use the following commands to complete
Echo> /etc/nginx/nginx.conf Clear the content in nginx.conf
But in order to configure complete the configuration, it is recommended to backup first
Cp /etc/nginx/nginx.conf /etc/nginx/cginx.conf.bak
Then run again
echo> /etc/nginx/nginx.conf Clear configuration file
Then edit the configuration file again
Vim /etc/nginx/nginx.conf
Add the following code and save the content of the code according to your own environment.
User nginx; worker_processses 4; pid /var/Run/nginx.pid ;events {workr_connections 1024;} error_log /var/ntinx/error.log info; major {server_name ava.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 Pipeline uidl; SMTP_AUTH LOGIN PLAIN CRAM-MD5; SMTP_CAPABILITIES "SIZE 10485760" ENHANCEDSCODES 8bitmime DSN; XClient Offer; {Listen 110; Protocol Pop3; Proxy on; Proxy_pass_error_message on;} Server {Listen 143; Protocol imap; Proxy on;} server {Listen 25; Protocol S mtp; proxy on;}}Paste
Next, set the nginx service
SystemCtl Enable nginx.service Nginx service opens random startup
SystemCtl Start Nginx.service Nginx service startup
SystemCTL Status nginx.service nginx service runs
The next step is to install the Java operating environment (JDK), java operating environment, and install Tomcat here
The first confirmation is that Linux is generally JDK with its own Java environment
The next step is to prepare a new JDK file. First of all, let's run the command
uname -A
Check whether the version and digits of the system are 32 or 64 -bit
If there is X86_64, the 64 -bit operating system, if not, it is 32 -bit
Download and install JDK
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Wett
http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-linux-x64.gz?authparam=1433902875_8682334C723CDBC5C9C9C9C9C9 DCC9
We check the downloaded file through LS, and then we need to decompress
Tar -zxvf jdk1.8.0 ...... ..
Declaim and complete, no need to install
Then we enter this path through the CD command
Create a directory of Java in/usr/, and then move the decompressed JDK to the directory
CD / USR / Enter the USR directory
Mkdir java creates a Java folder
Then we can move the current directory JDK1.8 .... Declaim the file to the java directory
1
MV JDK1.8.XX /USR /Java
VIM /ETC /Profile edit environment variables add 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 quit after saving, and then restart the system and find that the version of the JDK is already the new version of our installation
Next is that we need to install Tomcat, first download it
http://tomcat.apache.org/download-80.cgi
After opening Tomcat's official website to find the right installation package, right -click to copy the download link and download it online through WGET
wett
http://apache.dataguru.cn/tomcat/tomcat-8/v8.0.23/bin/apache-tomcat-8.0.23.tar.gz
Download
After downloading, we can start to decompress
Tar -zxvf apache_tomcat1.8.-0 tar.gz
Decompression complete
We need to move the Apache-Tomcat file to/usr/
MV Apache-Tomcat /USR
Next start the Tomcat service
CD /usr/apache-Tomcat18.0.23/bin
./startup.sh
Then we check the Tomcat related service port
NetStat -NLPT
Under CentOS7, you need to install the Net-Tools service separately
Yum install -y net -times
NetStat -NLPT
Then we use IE browser to access
We can also modify the default service port of Apache-Tomcat
Under Apache-Tomcat/Conf/Server.xml
Hashenv.put (context.security_Authentication, "Simple"); // LDAP access security level (None, Simple, Strong) Hashenv.put (context.security_principal, "CN ="+U u sername+", o = Beyondsoft"); Hashenv. put (context.security_principal, username); // The user name hashenv.put (context.Security_credentials, password); // The password of AD (context.initia L_context_factory, "com.sun.jndi.ldap.ldapctxFactory" ); // Hashenv.put (com.sun.jndi.ldap.connect.timeout, 3000); // The connection timeout is set to 3 seconds of hashenv.put (context.provider_url, "ldap://"+host+":" +port); // The default port 389try {ctx = New Initialdircontext (hashenv); // Initialize the context attributes attrs = CTX.Getattributes ("CN ="+Username+", O = B = B = B eyondsoft "); 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); ss ())) ; Return attcn.split (",", ") [0] .split (" = ") [1]; Then we need to customize the Java running program, in fact, the .war package
After defining the program, we will export the code of the program as a .war package, and then copy it to the webAPP under the installation directory of the Tomcat by WinSCP or Mount.
After the Tomcat service starts, we can visit through the browser to prompt 500 pages. In fact, the 500 page is normal, because we are request transmission in the program, so we cannot open the page.
Next, I have a problem to test the code, so we will test it through the following command:
CURL -H'AUTH -User: IIOSOFT '-H' Auth -Pass: 123 '-H' Auth -PROTOCOL: POP3 '
http: // localhost: 8080/imail/index.jsp
We can test you normally, you can return information such as Auth-SEVER, Auth-Status, Auth-Ports, Auth-Uth-Pass and other information, so there is no problem. We can test and verify through the Telnet MailServer 110, but during the verification process It is found that the code cannot be verified, and the code is indeed no problem, so how can the verification fail?
After finding the data confirmation, Nginx needs to return by LDAP that Auth-Server must be the IP address of the server. If the server name cannot be parsed, then we can only be transformed by the code.
Hashenv.put (context.security_Authentication, "Simple"); // LDAP access security level (None, Simple, Strong) Hashenv.put (context.security_principal, "CN ="+U u sername+", o = Beyondsoft"); Hashenv. put (context.security_principal, username); // The user name hashenv.put (context.Security_credentials, password); // The password of AD (context.initia L_context_factory, "com.sun.jndi.ldap.ldapctxFactory" ); // Hashenv.put (com.sun.jndi.ldap.connect.timeout, 3000); // The connection timeout is set to 3 seconds of hashenv.put (context.provider_url, "ldap://"+host+":" +port); // The default port 389try {ctx = New Initialdircontext (hashenv); // Initialize the context attributes attrs = CTX.Getattributes ("CN ="+Username+", O = B = B = B eyondsoft "); System.out.println (" MailServer: " + Attrs.get (" MailServer "). Get ()); System.out.println (" Mailserver: " + Attrs.get (" Mail "); String attachedomain = 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 // Return attcn.split (",", ") [0] .split (" = ") [1]; Return Inet4address.getByname (Attserver) .Gethostaddress (). Tostring (); Next, we can solve the problem by modifying the code:
How to solve it, before we initialized, we thought of getting the user's MailServer and MAILADDRSS values on LDAP. The content, and then stitching again is a server address. A1.duin.com; Finally
After converting to IP, we test again;
We also tested through Outlook
After the test is successful, we add a parsing to 192.168.2.163 to the DNS, and then we test it again through the domain name. If the nginx agent SMTP, we need to edit nginx.conf to add the configuration file to add configuration files
Server {
Listen 25;
protocol smtp;
smtp_auth login plain;
xclient off;
}
Note: Because we add SMTP proxies under Nginx, we add the SMTP server area
After we add it to re -retract the prompt nginx restart failed
We think that the prompt port has been used, and the postfix that comes with the system is started
SystemCtl Stop Postfix
SystemCtl Disable Postfix
Then we restart nginx
We test through the Outlook configuration
The above is all the contents of this article. I hope everyone can like it.