envoie de mail

envoie de mail - Java - Programmation

Marsh Posté le 10-01-2005 à 11:24:51    

Bonjour, je dois comparer des valeurs d'une base de données et par rapport au resultat, envoyer un mail ou non. Cela se fait au moyen un booléen mais au regard de mes recherches je me perds un peu. Ai-je besoin d'autre? D'un servlet?  
Est ce que ce code me permettrait d'arriver à envoyer des mails sur un serveur local ou autre? Merci par avance.
 
 
 
Code:
import java.net.*;  
import java.io.*;  
 
public class SimpleSendMail  
{  
    String mail = null;  
    String to = null;  
    String from = null;  
    String name = null;  
    boolean verbose = false;  
     
    String smtpServerAddress = "smtp";  
    int smtpServerPort = 25;  
     
    Socket socketSmtpServer = null;  
    DataOutputStream dos = null;  
    BufferedReader br = null;      
     
    /**  
     * @param mail message à envoyer  
     * @param name nom fourni lors de la connexion au serveur stmp  
     * @param from adresse email de l'auteur  
     * @param to adresse email destinataire  
     */  
    public SimpleSendMail(String mail, String name, String from, String to)  
    {  
        this.mail=mail;  
        this.name=name;  
        this.from=from;  
        this.to=to;  
    }  
 
    /**  
     * @param smtpServer adresse du serveur smtp  
     * @param port port du serveur smtp (25 en général)  
     * @param mail message à envoyer  
     * @param name nom fourni lors de la connexion au serveur stmp  
     * @param from adresse email de l'auteur  
     * @param to adresse email destinataire  
     */      
    public SimpleSendMail(String smtpServer, int port, String mail, String name, String from, String to)  
    {  
        this.smtpServerAddress=smtpServer;  
        this.smtpServerPort=port;  
        this.mail=mail;  
        this.name=name;  
        this.from=from;  
        this.to=to;  
    }  
 
    /**  
     * @param smtpServer adresse du serveur smtp  
     * @param port port du serveur smtp (25 en général)  
     * @param mail message à envoyer  
     * @param name nom fourni lors de la connexion au serveur stmp  
     * @param from adresse email de l'auteur  
     * @param to adresse email destinataire  
     * @param verbose : affiche sur la sortie standard la discussion avec le serveur smtp  
     */      
    public SimpleSendMail(String smtpServer, int port, String mail, String name, String from, String to, boolean verbose)  
    {  
        this.smtpServerAddress=smtpServer;  
        this.smtpServerPort=port;  
        this.mail=mail;  
        this.name=name;  
        this.from=from;  
        this.to=to;  
        this.verbose=verbose;  
    }  
 
 
   /**  
    * envoi du message  
    */  
    public void send() throws UnknownHostException, IOException  
    {  
        println("Connexion..." );  
        try  
        {  
            socketSmtpServer = new Socket(smtpServerAddress, smtpServerPort);  
            dos = new DataOutputStream(socketSmtpServer.getOutputStream());  
            br  = new BufferedReader(new InputStreamReader(socketSmtpServer.getInputStream()));  
        }  
        catch (UnknownHostException uhe) {throw (uhe);}  
        catch (IOException ioe) {throw (ioe);}  
     
        String strBuf = null; //reçoit les réponses du serveur  
         
        strBuf = br.readLine(); println(strBuf);  
        //strBuf = br.readLine(); println(strBuf);  
        println("HELO" );  
        dos.writeBytes("HELO " + name + "\n" );  
        strBuf = br.readLine(); println(strBuf);  
        println("RSET" );  
        dos.writeBytes("RSET\n" );  
        strBuf = br.readLine(); println(strBuf);  
         
        println("MAIL FROM" );  
        dos.writeBytes("MAIL FROM:<"+from+">\n" );  
        br.readLine();  
        println("RCPT TO" );  
        dos.writeBytes("RCPT TO:<"+to+">\n" );  
        br.readLine();  
         
        println("DATA" );  
        dos.writeBytes ("DATA\n" );  
        strBuf = br.readLine(); println(strBuf);  
        dos.writeBytes (mail);  
        println("." );  
        dos.writeBytes("\n.\n" ); //signale la fin de DATA  
        strBuf = br.readLine(); println(strBuf);  
        println("QUIT" );  
        dos.writeBytes("QUIT\n" );  
        strBuf = br.readLine(); println(strBuf);  
        dos.close();  
        br.close();  
        socketSmtpServer.close();          
     
    }  
 
    private void println(String s)  
    {  
        if (verbose) System.out.println(s);  
    }  
 
}

Reply

Marsh Posté le 10-01-2005 à 11:24:51   

Reply

Marsh Posté le 10-01-2005 à 17:59:04    

jete un coup d'oueil a ca
http://java.sun.com/products/javamail/

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed