Apache James uses Java programs to create and delete users

Source: Internet
Author: User

Recently in the corporate project management of the mail system, so query a large amount of information, build Apache James 2.3.2 Mail Management system.

After the construction, to code the details of the process of programming, first think of the first problem: Add and delete users.

The Apachejames tool typically uses the Telnet client to perform operations such as adding and removing users and modifying passwords under the CMD Command window, taking into account the need to integrate functionality into the SSH framework, in the context of an eclipse-based user operation.

An article was read on CSDN and its code was modified. Complete the use of the cmd command in the eclipse environment to operate the Telnet client with the following code:

Package com.yyh.StudyMail;

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.PrintStream;
Import java.net.SocketException;

Import org.apache.commons.net.telnet.TelnetClient;

public class Shapassword {

public static void Main (string[] args) {
try {
Telnetclient telnetclient = new Telnetclient ("vt200"); Specifies the Telnet terminal type, otherwise the data returned will be garbled in Chinese
Telnetclient.setdefaulttimeout (5000); Socket delay Time: 5000ms
Telnetclient.connect ("172.168.0.193", 4555); Establish a connection, the default port is 23
InputStream InputStream = Telnetclient.getinputstream (); Read the stream of the command
PrintStream pStream = new PrintStream (Telnetclient.getoutputstream ()); Stream of the Write command
Byte[] B = new byte[1024];
int size;
StringBuffer sbuffer = new StringBuffer (300);
StringBuffer rbuffer = new StringBuffer (300);
StringBuffer tbuffer = new StringBuffer (300);
StringBuffer ubuffer = new StringBuffer (300);

while (true) {//Read the data returned by the server until the login ID is read, this time it is assumed that the user name can be entered
size = Inputstream.read (b);
if ( -1! = size) {
Sbuffer.append (New String (b, 0, size));
if (sbuffer.tostring (). Trim (). EndsWith (":")) {
Break
}
}
}
The first step is to enter the mail system user name
System.out.println (Sbuffer.tostring ());
System.out.println ("1");
Pstream.println ("root"); Write commands
System.out.println ("2");
Pstream.flush (); Send command to Telnet Server
System.out.println ("3");

while (true) {//Read the data returned by the server until the login ID is read, this time it is assumed that the user name can be entered
size = Inputstream.read (b);
if ( -1! = size) {
Rbuffer.append (New String (b, 0, size));
if (rbuffer.tostring (). Trim (). EndsWith ("Password:")) {
Break
}
}
}

Step two Enter the mail system password
System.out.println (Rbuffer.tostring ());
Pstream.println ("root"); Write commands
System.out.println ("1");
Pstream.flush (); Send command to Telnet Server
System.out.println ("2");

while (true) {//Read the data returned by the server until the login ID is read, this time it is assumed that the user name can be entered
size = Inputstream.read (b);
if ( -1! = size) {
Tbuffer.append (New String (b, 0, size));
if (tbuffer.tostring (). Trim (). EndsWith ("commands")) {
Break
}
}
}
System.out.println (Tbuffer.tostring ());

Step three enter add user instruction
Pstream.println ("AddUser test1 test1"); Write commands
System.out.println ("1");
Pstream.flush (); Send command to Telnet Server
System.out.println ("2");

while (true) {//Read the data returned by the server until the login ID is read, this time it is assumed that the user name can be entered
size = Inputstream.read (b);
if ( -1! = size) {
Ubuffer.append (New String (b, 0, size));
if (ubuffer.tostring (). Trim (). EndsWith ("added")) {
Break
}
}
}
System.out.println (Ubuffer.tostring ());

Fourth step to exit the Telnet command interface
Pstream.println ("Quit"); Write commands
System.out.println ("1");
Pstream.flush (); Send command to Telnet Server
System.out.println ("2");

if (null! = PStream) {
Pstream.close ();
}
Telnetclient.disconnect ();
} catch (SocketException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}

There is a null pointer error has not been debug, increase user functionality implementation, the code has not been optimized.

Welcome Message Exchange

Apache James uses Java programs to create and delete users

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.