Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Sending mail from a bean..

Thread view: 
gbattine - 07 Nov 2006 11:37 GMT
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
Simon Brooke - 07 Nov 2006 13:41 GMT
> i need your help.
> I've a jsf application a page in which user can clicks on a button
[quoted text clipped - 5 lines]
> 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;
 }
}

Signature

simon@jasmine.org.uk (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>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.