..


Sponsored Links

Develop a contact form (email form) in JSP

Article written by Antonio Coschignano
Page 1 of 3

A key feature and that is usually present in almost all the websites, I'm talking about the contact form that allows the user to quickly contact the author or site manager.
What we will see in this article is the implementation of a contact form through Java Server Pages. Doing so is, unfortunately, much less simple than other Web-oriented programming languages: JSP through, in fact, necessary to resort to the use of the Java Mail API through which we can build and send e-mail messages in a purely ad objects.

I propose to proceed by creating a servlet that will send the email through the Java Mail API and then create an HTML form that uses the servlet. We begin then by a general overview of the use of this API set.

The Java Mail API

Javax.mail The package defines a model common to all classes of mail systems, which is conceptually structured by different types of messages (plain text, HTML, attachments, etc. ..) and the transport system through the application protocol and SMTP Receiving POP3 or IMAP.
The protocol is essential for our purpose all'SMPT (Simple Mail Transfer Protocol) which is used in the transmission of email messages. If the library is not present in your Java platform you can download it at this address JavaMail 1.4.3 and then import the jar file in the project.

Let the classes necessary to build and send the message:

  • javax.mail.Session : This class represents a session with the necessary operations are carried out on an e-mail system. As we will see all the objects involved in the transmission of the message refers to an instance of this class.
  • javax.mail.Message : abstracts the concept of message. As we know an e-mail can take different forms. In fact, we can decide to send a message to plain text or HTML, or even a message that ontiene a flooded. So for every message type there is a realization of this class. For our purpose, we will refer to the subclass javax.mail.internet.MimeMessage which corresponds to the simple text message.
  • javax.mail.Transport : It deals simply send the message through the SMTP server using the static send ().

Now let's see how you compose and you start a session for the transmission of an e-mail. First you must set some configuration parameters of the session through an object of type Properties.
The parameters we need in our case are the host address that hosts the SMTP server, and the service port (usually 25):






 ...







 Properties props = new Properties ();







 props.put ("mail.smtp.port", "25");







 props.put ("mail.smtp.host", "smpt.example.com");







 Session session = Session.getDefaultInstance (props);







 ...



We have initiated a session so that interfaces with the server indicated by the attribute mail.smtp.host. We just have to build the message. As I mentioned, there are different types of messages which corresponds to a subclass of javax.mail.Message that realizes the features. We will refer to a simple text message that you can dial using MimeMessage:





 ....







 Message message = new MimeMessage (session);









 InternetAddress from = new InternetAddress ("mittente@example.com");







 InternetAddress to = new InternetAddress ("destinatario@example.com");









 message.setFrom (from);







 message.addRecipient (Message.RecipientType.TO, to);









 message.setSubject ("Message Subject");







 message.setSentDate (new Date ());







 message.setText ("Message Body");



In the same category ...
E-Learning
Excel (Ebook) Excel (Ebook)
Create spreadsheets and calculation. Just 25 €.
Flash MX (Advanced) Flash MX (Advanced)
Become a designer of Web sites from 29 €.
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
Sponsored Links