How to send email in JSP

There are certain steps to be followed to use Java Mailing System. To send an email using a JSP is simple enough but to start with you should have JavaMail API and Java Activation Framework (JAF) installed on your machine.

Now we have to download latest version of JavaBeans Activation Framework JAF (Version 1.0.2) from Java's standard website.

Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. We need to include mail.jar and activation.jar files in our CLASSPATH.

Send a Simple Email:

Here is an example to send a simple email from our machine. Let us assume that our localhost is connected to the internet and capable enough to send an email. Simultaneously also check that all the jar files from Java Email API package and JAF package are available in CLASSPATH.

Now let us put above code in SendEmail.jsp file and call this JSP using URL http://localhost:9999/SendEmail.jsp which would send an email to given email ID abc@gmail.comand would display following response:

Send Email using JSP

Result: Sent message successfully....

If we want to send an email to multiple recipients then following methods would be used to specify multiple email IDs:

Type: This would be set to TO, CC or BCC. Here CC represents Carbon Copy and BCC represents Black Carbon Copy.

Addresses: This is the array of email ID. We would need to use InternetAddress() method while denoting email IDs

Send an HTML Email:

Here is an example to send an HTML email from your machine. Here it is assumed that our localhost is connected to the internet and capable enough to deliver an email, also make sure all the jar files from Java Email API package and JAF package are available in CLASSPATH.

This example is same as previous one, except here we are using setContent() method to set content whose second argument is "text/html" to specify that the HTML content is included in the message.

Once we have all the data, we can use above mentioned programs to send email.

Conclusion

In this article we studied how we can send an email via JSP.Sending an email is a very important functionality of any application like Bookstores App, registering a new user. All such type where customer communication is necessary.

To have full access to this post (or download the associated files) you must have MrBool Credits.

See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video.Subscribe for this package!