Sending mail from a bean..

Discussion in 'Java' started by gbattine, Nov 7, 2006.

  1. gbattine

    gbattine Guest

    Hi guys,
    i need your help.
    I've a jsf application a page in which user can clicks on a button
    "Password lost?".
    I want the bean called by this botton sends an email to user showing
    password from its database.
    How can i do it?
    I use jsf 1.1.0.1 e servlet 2.3
    Can someone post me some working code?
    I've read on internet but i'm bit confused,so simple code can helps me.
    Thanks
     
    gbattine, Nov 7, 2006
    #1
    1. Advertising

  2. gbattine

    Simon Brooke Guest

    in message <>,
    gbattine ('') wrote:

    > i need your help.
    > I've a jsf application a page in which user can clicks on a button
    > "Password lost?".
    > I want the bean called by this botton sends an email to user showing
    > password from its database.
    > How can i do it?
    > I use jsf 1.1.0.1 e servlet 2.3
    > Can someone post me some working code?
    > I've read on internet but i'm bit confused,so simple code can helps me.


    package uk.co.weft.pres.server;

    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;

    import uk.co.weft.alert.Alert;
    import uk.co.weft.alert.AlertingException;
    import uk.co.weft.dbutil.Context;
    import uk.co.weft.dbutil.Contexts;
    import uk.co.weft.dbutil.DataFormatException;
    import uk.co.weft.dbutil.DataStoreException;
    import uk.co.weft.dbutil.TableDescriptor;
    import uk.co.weft.htform.Form;
    import uk.co.weft.htform.InitialisationException;
    import uk.co.weft.htform.TableWrapperForm;


    /**
    * An table wrapper form which is capable of mailing a password to the
    * address registered to it
    */
    public abstract class AbstractMailPass extends TableWrapperForm
    {
    //~ Instance fields -----------------------------------------------

    /** our postman */
    protected Alert pat;

    /** the subject for emails */
    protected String mailSubject =
    "Your password on the Press Release Server";

    //~ Methods -------------------------------------------------------

    /**
    * specialisation: add a MailPassWidget to my authentificationWidgetSet
    */
    public void init( Context config ) throws InitialisationException
    {
    super.init( config );

    config.put( Alert.MAILTEMPLATETOKEN,
    config.get( "mail_pass_template_url" ) );

    try
    {
    pat = new Alert( config );
    }
    catch ( AlertingException e )
    {
    throw new InitialisationException( "Could not create alert agent",
    e );
    }

    String v = config.getValueAsString( Alert.MAILSUBJECTTOKEN );

    if ( v != null )
    {
    mailSubject = v;
    }
    }

    /**
    * mail to the user the password corresponding to the email address
    * in the context
    *
    * @param context the context
    *
    * @return true if mail was despatched
    */
    protected boolean mailPass( Context context ) throws DataStoreException
    {
    boolean result = false;

    String email = context.getValueAsString( Subscriber.EMAILFN );

    if ( email != null )
    {
    Context searchContext = new Context( );
    searchContext.copyDBTokens( context );
    searchContext.put( "email", email );

    Contexts results =
    TableDescriptor.getDescriptor( "subscriber", "subscriber",
    context ).match( searchContext, true );

    if ( results.isEmpty( ) )
    {
    throw new DataFormatException(
    grs( "Could not find a subscription with " +
    "that email address", context ) );
    }
    else
    {
    Context candidate = (Context) results.get( 0 );
    InternetAddress recipient = null;

    try
    {
    recipient = new InternetAddress( email );
    }
    catch ( AddressException e )
    {
    throw new DataFormatException( email +
    grs( "is not a well formed address", context ), e );
    }

    try
    {
    if ( !pat.sendAlert( recipient, mailSubject, candidate ) )
    {
    throw new DataStoreException(
    grs( "Could not send password message",
    context ) );
    }
    }
    catch ( AlertingException e1 )
    {
    throw new DataStoreException(
    grs( "Could not send password message",
    context ), e1 );
    }

    context.put( Form.ACTIONEXECUTEDTOKEN,
    grs( "Password mailed", context ) );

    result = true;
    }
    }

    return result;
    }
    }

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/

    ;; Woz: 'All the best people in life seem to like LINUX.'
    ;; <URL:http://www.woz.org/woz/cresponses/response03.html>
     
    Simon Brooke, Nov 7, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Benjamin Stewart

    To bean or not to bean...

    Benjamin Stewart, Jun 30, 2003, in forum: Java
    Replies:
    0
    Views:
    821
    Benjamin Stewart
    Jun 30, 2003
  2. David Thielen
    Replies:
    2
    Views:
    811
    Marek Lange
    Sep 12, 2003
  3. Markku Salminen
    Replies:
    3
    Views:
    5,871
    Michael Borgwardt
    Jan 21, 2004
  4. Takeshi
    Replies:
    0
    Views:
    526
    Takeshi
    Jan 28, 2004
  5. Replies:
    0
    Views:
    1,418
Loading...

Share This Page