..
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.
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:
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");
| |
Excel (Ebook)
Create spreadsheets and calculation. Just 25 €. |
| |
Flash MX (Advanced)
Become a designer of Web sites from 29 €. |
| |
Linux (Course)
Complete guide to open-source system. From 49 €. |