client smtp java avec interface utilisateur
Un client smtp écrit en java avec une interface graphique swing
C'est insuffisant pour un usage courant mais je vous laisse le code pour que vous puissiez faire des miracles avec.
Voici a quoi ça ressemble
> />
import java.util.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Gsmtp extends JFrame
{
class smtpClient extends Thread {
private String server, message, dest, origin;
private int port;
private Socket socket;
public smtpClient (String serv, int por, String org)
{
server = serv;
port = por;
origin = org;
}
public void createMsg(String dst, String msg)
{
message = msg;
dest = dst;
}
public void run()
{
PrintWriter to;
BufferedReader from;
String str;
try {
socket = new Socket(InetAddress.getByName(server),port);
to = new PrintWriter(
new BufferedWriter (
new OutputStreamWriter (
socket.getOutputStream())),true);
from = new BufferedReader(
new InputStreamReader (
socket.getInputStream()));
str = from.readLine();
to.println("helo plus.bas");
status.setText(from.readLine());
to.println("mail from: <"+origin+">");
status.setText(from.readLine());
to.println("rcpt to: <"+dest+">");
status.setText(from.readLine());
to.println("data");
status.setText(from.readLine());
to.println(message + "\r\n.\r\n");
status.setText(from.readLine());
status.setText("Ready ...");
socket.close();
} catch ( Exception e ) {}
}
}
public class SmtpServerDialog extends JDialog
{
JTextField hostTF, portTF;
public SmtpServerDialog(Frame parent, boolean modal)
{
super(parent, modal);
JPanel panel1, panel3;
JButton okButton, cancelButton;
Container contentPane = this.getContentPane();
panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel1, BorderLayout.SOUTH);
okButton = new JButton("OK");
panel1.add(okButton);
cancelButton = new JButton("Cancel");
panel1.add(cancelButton);
panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel3, BorderLayout.CENTER);
hostTF = new JTextField(smtpServer, 20);
portTF = new JTextField("" + port, 5);
panel3.add(hostTF);
panel3.add(portTF);
setTitle("Smtp Server info");
pack();
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
dispose();
setVisible(false);
}
});
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
smtpServer = hostTF.getText();
port = Integer.parseInt(portTF.getText());
dispose();
setVisible(false);
}
});
}
}
private String smtpServer = "fmsmsx18.intel.com";
private int port = 25;
private JLabel status = new JLabel("Ready...");
private JTextField AddrDest = new JTextField(10);
private JTextField AddrOrg = new JTextField(10);
private JTextArea Jbody = new JTextArea(10,30);
public Gsmtp ()
{
JMenuBar menuBar = new JMenuBar();
JButton closeButton = new JButton("Close");
JPanel statusbar = new JPanel();
JPanel Core = new JPanel();
JPanel Result = new JPanel();
JScrollPane SPbody;
Core.setLayout(new BorderLayout());
Result.setLayout(new BorderLayout());
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());
setJMenuBar(menuBar);
JMenu fMenu = new JMenu("File");
menuBar.add(fMenu);
JMenuItem smtpServerItem = new JMenuItem("Set Smtp Server", 2);
fMenu.add(smtpServerItem);
fMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("Exit", 2);
fMenu.add(exitItem);
statusbar.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
statusbar.setBorder(new BevelBorder(BevelBorder.LOWERED));
contentPane.add(statusbar,BorderLayout.SOUTH );
statusbar.add(status);
JPanel url = new JPanel(new FlowLayout());
JButton getButton = new JButton("Send");
url.add( new JLabel("To :") ); url.add( AddrDest );
url.add( new JLabel("From :") ); url.add( AddrOrg );
url.add(getButton);
Core.add(url, BorderLayout.NORTH);
SPbody = new JScrollPane(Jbody,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Result.add(new JSeparator());
Result.add(SPbody, BorderLayout.SOUTH);
Core.add(Result, BorderLayout.SOUTH);
contentPane.add(Core);
// add listeners for the window and the various buttons
this.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
dispose();
System.exit(0);
}});
exitItem.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}});
getButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
send();
}});
smtpServerItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
changeSmtpServer();
}});
pack();
}
protected void changeSmtpServer()
{
new SmtpServerDialog(this, true).show();
}
private void send()
{
smtpClient sc = new smtpClient(smtpServer, port, AddrOrg.getText());
sc.createMsg(AddrDest.getText(), Jbody.getText());
sc.run();
}
public static void main ( String args[] )
{
(new Gsmtp()).setVisible(true);
}
}
|