Nelson

Depois de ter procurado bastante pela internet, encontrei uma classe que envia um email..
Estou mandando-a em anexo.

Rodrigo

Nelson wrote:

     Pessoal, Como eu envio um email de dentro de uma aplicacao java???? []'sNelson
import java.net.*; 
import java.io.*;

/**
   * <code>SendMail</code> sends internet smtp email
   *
   * @version 1.4
   * @author Michael Bogan
   */

public class SendMail {
    
    private Socket smtpHost; 
    private BufferedReader In; 
    private DataOutputStream Out;
    private String LineIn;
    private String host, to, from, subject;
    
    /**
     * Constructs a new Instance of SendMail and mails the message
     *
     * @param message The message to send in the email
     * @param host The SMTP host
     * @param to The recipient
     * @param from The sender
     * @param subject The subject of the message
     */

    public SendMail(String message, String host, String to, String from, String 
subject) { 
        
        this.host = host;
        this.to = to;
        this.from = from;
        this.subject = subject;
        
        try  { 
            smtpHost = new Socket(host, 25); 
            Out = new DataOutputStream(smtpHost.getOutputStream()); 
            In = new BufferedReader(new InputStreamReader(smtpHost.getInputStream())); 
            LineIn = In.readLine(); 
            if (LineIn.indexOf("220")==-1) {
                throw new Exception("Bad Server"); 
            }
            // replace your server name here!
            Out.writeBytes("HELO yahoo.com\r\n"); 
            LineIn = In.readLine(); 
            if (LineIn.indexOf("250")==-1) {
                throw new Exception(); 
            } 
        }
        catch(Exception E) {
            smtpHost = null;
        } 
        try {
            Out.writeBytes("MAIL FROM:<" + from + ">\r\n"); 
            LineIn = In.readLine();       
            if (LineIn.indexOf("250")==-1) {
                throw new Exception("Bad MAIL FROM:"); 
            }
            Out.writeBytes("RCPT TO:<" + to + ">\r\n"); 
            LineIn = In.readLine(); 
            if (LineIn.indexOf("250")==-1) {
                throw new Exception("Bad RCPT TO:"); 
            }
            Out.writeBytes("DATA\r\n"); 
            LineIn = In.readLine(); 
            if (LineIn.indexOf("354")==-1) {
                throw new Exception("Bad DATA"); 
            }
            Out.writeBytes("From: " + from + "\r\n"); 
            Out.writeBytes("Subject: " + subject + "\r\n"); 
            Out.writeBytes(message);
            Out.writeBytes("\r\n.\r\n");
            LineIn = In.readLine(); 
            if (LineIn.indexOf("250")==-1) {
                throw new Exception("Bad End of Data"); 
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
        } 
    } 
    
    /**
     * Closes the connection to the smtpHost
     */

    public void close() { 
        try{ 
            if (smtpHost != null) {
                smtpHost.close(); 
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        } 
    } 
    
    /**
     * Finalizes
     *
     * @exception java.lang.Throwable A problem, man
     */

    protected void finalize() throws Throwable { 
        close(); 
    } 

}

  

Responder a