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?
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:
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.
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.
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
.
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.
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.
On login, only select the user when active=true
or 1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With