Le protocole SMTP: classe java
Pour programmer smtp en java, voici une classe toute simple qui permet d'envoyer
un message
sjava.io.*;
import java.net.*;
class smtpClient {
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 sendMsg()
{
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");
str = from.readLine(); System.err.println(str);
to.println("mail from: <"+origin+">");
str = from.readLine(); System.err.println(str);
to.println("rcpt to: <"+dest+">");
str = from.readLine(); System.err.println(str);
to.println("data");
str = from.readLine(); System.err.println(str);
to.println(message + "\r\n.\r\n");
str = from.readLine(); System.err.println(str);
socket.close();
} catch ( Exception e ) {}
}
public static void main ( String args[] )
{
smtpClient c = new smtpClient("SMTP SERVER",25,"org@salemioche.com");
c.createMsg("dest@salemioche.com","Coucou");
c.sendMsg();
|