Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How send automatic reply on particular email id when an user registers?

Tags:

java

jsp

servlets

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?

like image 997
Smith Avatar asked Nov 28 '22 22:11

Smith


1 Answers

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.

like image 138
BalusC Avatar answered Dec 04 '22 07:12

BalusC