Ver Mensaje Individual
  #3 (permalink)  
Antiguo 19/03/2007, 05:01
Avatar de Zoe79
Zoe79
 
Fecha de Ingreso: diciembre-2001
Mensajes: 187
Antigüedad: 22 años, 5 meses
Puntos: 1
Re: Como Usar JavaMail

A ver si esto te sirve.

Es una clase con una función. Muy simple. Usando el api de javamail.
Eso si necesitas un servidor smtp.

Código:
package tupaquete;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtils {
	
	private String smtpServer="127.0.0.1"; //aqui tu ip de servidor smtp
	
	public void send(String to, String from, String subject, String body){
	  	try
	    {
	      Properties props = System.getProperties();
	      // -- Attaching to default Session, or we could start a new one --
	      props.put("mail.smtp.host", this.smtpServer);
	      Session session = Session.getDefaultInstance(props, null);
	      // -- Create a new message --
	      Message msg = new MimeMessage(session);
	      // -- Set the FROM and TO fields --
	      msg.setFrom(new InternetAddress(from));
	      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
	      // -- We could include CC recipients too --
	      // if (cc != null)
	      // msg.setRecipients(Message.RecipientType.CC
	      // ,InternetAddress.parse(cc, false));
	      // -- Set the subject and body text --
	      msg.setSubject(subject);
	      msg.setContent(body, "text/plain");
	      //msg.setText(body);
	      // -- Set some other header information --
	      msg.setHeader("X-Mailer", "LOTONtechEmail");
	      msg.setSentDate(new Date());
	      // -- Send the message --
	      Transport.send(msg);
	      System.out.println("Message sent OK.");
	    }
	    catch (Exception ex)
	    {
	      ex.printStackTrace();
	    }
}

}
Saludos.
__________________
Diseño integral / Desarrollo
Grupo Ikon
Imagen - Fotografía
Borja Lázaro Herrero