----- Original Message -----
From: Aimn
Sent: Sunday, February 06, 2000 4:58 AM
Subject: emails

Hi everyone,
Does anyone know how to use JSP to send emails.  I might need to write a java bean for that purpose.  Does anyone know of an existing open source bean for that.
Thanks in advance.
 
 
This might work for you - create a library with the following code and get the JavaMail & Java Activation Framework libraries from JavaSoft.
 
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
 
public class EmailManager
{
  public static boolean sendEmail( String from, String to, String subject, String text, String smtpHost )
  {
    boolean           wasSent       = true;
    Session           theSession    = null;
    InternetAddress   theAddress[]  = null;
 
    Properties p = new Properties();
    p.put( "mail.smtp.host", smtpHost );
    theSession = Session.getDefaultInstance( p, null );
 
    theAddress = new InternetAddress[ 1 ];
    try
    {
      theAddress[0] = new InternetAddress( to );
 
      Message msg = new MimeMessage( theSession );
 
      msg.setFrom( new InternetAddress( from ) );
      msg.setSubject( subject );
      msg.setContent( text, "text/plain" );
      msg.setRecipients( Message.RecipientType.TO, theAddress );
 
      Transport.send( msg );
    }
    catch ( Exception ex )
    {
      ex.printStackTrace();
      wasSent = false;
    }
 
    return wasSent;
  }
 
  public static void main( String args[] )
  {
    EmailManager.sendEmail( "[EMAIL PROTECTED]", "[EMAIL PROTECTED]", "Ergle!", "This is a test", "some.smtp.server" );
  }
}
 
---
Richard Vowles, Infrastructure Architect, Inprise New Zealand
home-email: "[EMAIL PROTECTED]", work-email: "[EMAIL PROTECTED]"
"Is it not a foolish man who puts all his chickens down his trousers" - Alexi Sayle, Panic

Reply via email to