4

I have created a registration form in JSP with an input field for email address. When user submits the form, then the user must get an auto-reply on his/her email address. How can I achieve this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Smith
  • 413
  • 3
  • 9
  • 24

3 Answers3

23

Auto-reply? Sorry, that term makes no sense in this particular context. Auto-reply is more a setting on a mailserver which should automatically send a reply back on incoming emails, for example "Thank you, your email is been received, your email will be answered within 24 hours." or something. You don't need this here.

You just want to programmatically send a mail. The mail should contain a link which should activate the account so that the user will be able to login. You see this indeed often on other websites. Here's how you can go about this:

  1. Setup/prepare a SMTP server. A SMTP server is a mail server. Like as that a HTTP server is a web server. You can use an existing one from your ISP or a public one like Gmail. You can even setup a completely own one. For example with Apache James. Whatever way you choose, you should end up knowing the hostname, portnumber, username and password of the SMTP server.

  2. Create a Mailer class which can take at least "from", "to", "subject" and "message" arguments and sends a mail using JavaMail. Connect and login the SMTP server by hostname, portnumber, username and password. Create a mail session and send the mail based on the given arguments. Create a dummy test class with a main() method which runs and tests the Mailer class. Once you get it to work, proceed with next steps.

  3. Create another database table user_activation with a PK key and FK user_id referring the PK id of an user table which you should already have. On the existing user table, add a boolean/bit field active which defaults to false/0.

  4. When user registration and insert in the DB succeeds, get the insert id from the user table, generate a long and unique key with java.util.UUID and insert them in user_activation table. Prepare a mail message with an activation link where the unique key is included as URL parameter or path and then send this message using the Mailer class you created.

  5. Create a Servlet which is mapped on an URL pattern matching the activation key links, e.g. /activate/* and extracts the activation key from the URL. Select the associated user from the database and if any exist, then set its active field to true/1 and remove the key from user_activation table.

  6. On login, only select the user when active=true or 1.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    Hi BalusC .. This would be best algorithm for all those who wants this kind of functionality. – Hardik Mishra Mar 25 '11 at 12:03
  • This is great(+1), i am going to do it this way in my application. I have just one doubt, should i erase the key value after the activation, so it cannot be reused or there is not need for that? – javing Apr 18 '11 at 16:58
  • Definitely you need to delete it. See also point 5. Since you're using JSF, you could also just substitute "servlet" with a JSF view/backing bean. – BalusC Apr 18 '11 at 17:01
  • If you don't want to delete the key (which is something I tried avoiding too), one can create an activation key with an expiry date timestamp. If a user activates after the expiry date, it won't be picked up. Once the activation is successful, another timestamp is populated which specify when the activation key was activated. This is a good approach as it can give you an indication on how many users registered but are not yet activated. – Buhake Sindi May 04 '14 at 21:31
  • Is it essential/crucial to store the activation key in the database? Is it not enough to store into the user session? Is it just dependent upon the requirements? – Tiny Dec 29 '14 at 23:42
  • @Tiny: what if the email got delayed, or if enduser clicks the link after the session expires, or if the mailclient opens a new/different browser window, or if the webpage is stateless, etc? – BalusC Dec 29 '14 at 23:44
  • Is this actually the way to go? I mean.. doing all this by hand and programmatically? Aren't there any "ready to go" solutions? Maybe take a look at [my question here](http://stackoverflow.com/questions/37459351/how-to-establish-an-automated-email-service-for-a-website). – Stefan Falk May 26 '16 at 11:22
3

Your question is not very clear. Let me get the requirement right; you need the code to send an email to the user only on successful registration. correct?

In your servlet(which is invoked on submit action),

if(user input is valid){
  Step1: registerUser();
  Step2: send confirmationEmail();
} else {
  Step3: Exception case
}

Send email method would ideally send the request to an JMS(queue) to send the email to the desired user. Below is a snippet to send an email.

//Sample java code to send email

public void sendEmail(){

        try{

            Properties props = null;

                if (props == null) {
                        props = System.getProperties();
                }

                props.put("mail.smtp.host", "<server host name>");

                Session session = Session.getInstance(props, null);

                MimeMessage message = new MimeMessage(session);

                message.setFrom(new InternetAddress("<from email id>"));            

                message.addRecipients(Message.RecipientType.CC, "<Registered user email id>");          

                message.setSubject("<Subject of the email>");

                message.setContent("<Content of the email>", "text/plain");         

                Transport.send(message);    

                    logger.info("Sent Email :" + 
                        "From :" + message.getFrom() +
                        "To:" + message.getAllRecipients() +
                        "Subject:" + message.getSubject() );

            } catch(Exception ex){
                ex.printStackTrace();
            }       
        }
asgs
  • 3,928
  • 6
  • 39
  • 54
Yogi
  • 94
  • 1
  • 3
0

What about adding user's email address to Bcc field (Blind Carbon Copy)?

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
  • Nope i want only to give auto-reply to user as "Registration is got successful"..thats when user got registration completed/when clicked on submit button.How to achieve it?? – Smith Mar 25 '11 at 10:20