This post shows how to send Email using Java.....this means you can write a simple Java Program that sends a mail using your mail account details or you can embed this functionality in your Web Application. Instead of explaining details of how it works, i will first give you the code that i used and then direct you to some useful links/resources that help you to understand better.
Resources Required:
JavaMail
JavaBeans Activation Framework
JAF(JavaBeans Activation Framework) is already shipped with Java SE 6, so Java SE 6 users need not download it separately.
NetBeans 6.1 IDE
Needless to say, NetBeans 6.1 IDE has helped me and other students of my University in completing assignments and projects on time and with ease. So, if you don't have it, download it right away here. You can also order NetBeans Starter Kit DVD here.
Why you need to download them ?
JavaMail consists of various jar files such as mail.jar, mailapi.jar, smtp.jar, pop3.jar whereas JAF mainly consists of activation.jar file. You need to download them and include these jar files in your CLASSPATH so that your JVM(Java Virtual Machine) can find the JAF classes.
Getting Started
Open the NetBeans 6.1 IDE.

Pretty fast than previous releases. Took about only 14.35 seconds on my machine.
Go to File --> New Project
Under Categories, choose "Web" and then select to create a simple Web Application.

Click Next and Type in Project Name : JavaMail. Be sure to select Glassfish as your server.
Click Finish and allow NetBeans to create files for your project.
Now, index.jsp opens up in the editor by default. Copy and paste the following code in your index.jsp file. It simply creates a HTML form that asks for the receiver's email address, subject and message body of the email.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Java Mail </title>
</head>
<body>
<form action="sendMail.jsp" method="POST">
<table border="0" align="center" cellpadding="5">
<tbody>
<thead><tr> <td colspan="3" align="center">
<b> Send Mail </b> </td> </tr> </thead>
<tr>
<td> To </td> <td> : </td>
<td> <input type="text" name="to" value="" /> </td>
</tr>
<tr>
<td> Subject </td> <td> : </td>
<td> <input type="text" name="subject" value="" /> </td>
</tr>
<tr>
<td> Message </td> <td> : </td>
<td> <textarea name="message" rows="8" cols="30">
</textarea></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="Send Mail" />
<input type="reset" value="Reset" />
<td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Save your index.jsp and it looks like

Now, we create a bean in which we store all the mail related information : sender's email id and password and receiver's email id, subject and message body of the email and the name of the smtp server that we use to send email.
Right-click the project node and select New --> Java Class
A wizard like the following appears, fill in the appropriate details as
Class Name : Mail
Package Name : jMail

Mail.java class open up in the editor.
Add the following instance variables to the bean:
String to;
String from;
String message;
String subject;
String smtpServ;
Generate the getter, setter methods for them by right-clicking in the editor and select Refactor --> Encapsulate Fields. Then check all the setter and getter methods and click Refactor so that NetBeans 6.1 generates code for them.

It is here, we make use of the JavaMail API and send mail using the following code. But before doing that, import the required packages.
import javax.mail.*;
import javax.mail.internet*;
import java.util.*;
Now, as you has already noticed, NetBeans is showing errors showing that it doesn't know of javax.mail package even if you have correctly downloaded and installed the JAF and JavaMail API. To make NetBeans aware of the existence of these packages, perform the following steps:
Right click the project node (JavaMail), click to Properties to change properties of the project
Now go to Libraries Tab. (since we need to add jar files to our project)

Click on Add JAR/Folder Button. A window opens up.
Browse to the location where you have installed/unzipped your JavaMail API and then include
- activation.jar
- pop3.jar
- mail.jar
- mailapi.jar
- smtp.jar
If you do not perform this step, then NetBeans will not recognize JAF and JavaMail classes and hence will not compile the program given below which makes use of classes such as Message, Session, Transport etc.
Compile your program to check whether you have been able to successfully include these jar files or not.
Now, the main function sendMail( ), which is actually responsible for sending mail:
public int sendMail(){
try
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.transport.protocol", "smtp" );
props.put("mail.smtp.starttls.enable","true" );
props.put("mail.smtp.host",smtpServ);
props.put("mail.smtp.auth", "true" );
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText(message);
// -- Set some other header information --
msg.setHeader("MyMail", "Mr. XYZ" );
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent to"+to+" OK." );
return 0;
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Exception "+ex);
return -1;
}
}
// Also include an inner class that is used for authentication purposes
private class SMTPAuthenticator extends javax.mail.Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "Java.Mail.CA@gmail.com"; // specify your email id here (sender's email id)
String password = "javamail"; // specify your password here
return new PasswordAuthentication(username, password);
}
}
Save the file and compile it.
Now add another JSP, name it sendMail.jsp
Add the following code to it that instantiates JavaBean Object and set properties of JavaBean using the parameters passed to it using index.jsp
<jsp:useBean id="mail" scope="session" class="jMail.Mail" />
<jsp:setProperty name="mail" property="to" value="to" />
<jsp:setProperty name="mail" property="from" value="Java.Mail.CA@gmail.com" />
<jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" />
<jsp:setProperty name="mail" property="subject" param="subject" />
<jsp:setProperty name="mail" property="message" param="message" />
The main point to note here is the value of smtpServ that we have passed. Here, we are using a gmail id, so its smtp server name should be used, which is "smtp.gmail.com". If you want to use your own email id, then you have to make few changes as specified above and also change the value of smtpServ property to your smtp server name.
Add the following code to sendMail.jsp that calls the sendMail() method of the bean. Yes, a bean can have other methods except getter and setter methods. Its perfectly legal. 
<%
String to = mail.getTo();
int result;
result = mail.sendMail();
if(result == 0){
out.println(" Mail Successfully Sent to "+to);
}
else{
out.println(" Mail NOT Sent to "+to);
}
%>
Save your project and Run it to see that it actually works 
The whole project is available here for download.
Notes:
There is much to JavaMail than what is mentioned here. For more read on
JavaMail & Glassfish
Read the following links to understand what's actually happening in the code given above
JavaMail Quick Start
jGuru : Fundamentals of JavaMail API
Related & Advanced :
Creating JavaMail Client On NB
Please do not modify the password for the account Java.Mail.CA@gmail.com I created it only for demo purposes and have given out the account details as on all tutorials/demos about JavaMail that i have seen online leave these fields empty and also leave smtp server name fields empty, which only adds to confusion for novices.
Any comments, suggestions and corrections are welcome.

I forgot to add that, i used the new and improved NetBeans 6.1 for developing this small and handy tutorial about JavaMail and deployed it on Glassfish application server with ease.
Posted by Agraj on May 17, 2008 at 08:21 PM IST #
nice work Agraj
I was looking for something similar that can provide me a simple web app to send mail with javamail.And that also with the netbeans6.1 is like chocs on ice-cream.
This example provides step by step way to get started with simple javamail.Good for novice.
But once this is done
i think the application should be featured with the real purpose of email and that here means the attachment facility and html rich text format and html support.
It would be very helpful if you could add that (^_^)
Posted by fallenstar on May 21, 2008 at 05:41 PM IST #
Hi
How do I call the class I have created in code without having to use the beans interface.
Thanks.
Mark
Posted by Mark Nash on May 22, 2008 at 08:31 AM IST #
Hi Mark,
Well if you do not want to create a separate class for writing Java Code, you can simply put the Java code to send Email within <% (put your code here) %> these JSP Tags and call that function normally.
However, using a Bean is more useful as your code is not visible to users. Also, your code is more reusable as a Bean.
Posted by Agraj on May 22, 2008 at 07:34 PM IST #
Excellent tutorial... Should b very useful for people starting with Javamail. Nice little screenshots should help people not familiar with netbeans! Keep up the good work!
Posted by Saptarshi on June 16, 2008 at 02:13 PM IST #
Hi,
My name is Varun Nischal and I'm the NetBeans Community Docs Contribution Coordinator. Your blog entry would make a fantastic tutorial for our Community Docs wiki (http://wiki.netbeans.org/CommunityDocs).
Would you be willing to contribute it? If you need any help or have any questions, please contact me at nvarun@netbeans.org
I look forward to hearing from you.
Thanks,
Varun Nischal
http://nb-community-docs.blogspot.com/
--
"You must do the things you think you cannot do."
Posted by Varun on June 17, 2008 at 06:31 PM IST #
@fallenstar
Well, to send data other than normal text by email, one should use the DataHandler Class provided with JAF. You can read the API documentation for details.
Also you can try the demos shipped with JavaMail API. That would definitely help you in sending Multipart Messages.
Posted by Agraj on June 20, 2008 at 06:30 PM IST #
This blog entry has been re-posted as a tutorial at
http://wiki.netbeans.org/SendMailUsingJavaFromNetBeans
Thanks to Varun !!
Posted by Agraj on June 29, 2008 at 07:44 PM IST #
This Blog Entry won "second prize" in Sun's Student Reviews Contest on NetBeans and OpenSolaris.
You can see the complete list here..http://blogs.sun.com/students/date/20080701
Cheers to all the winners !!
Posted by Agraj on July 01, 2008 at 10:34 PM IST #
Greetings Agraj, from Argentina. This article was very clear for all students.
Thank you!
Posted by Dusk on March 02, 2009 at 08:00 PM IST #
hi, this does not allow me to send mail
while running the project file index.jsp is working but while sending it says
encrypted email connection has been detected.
can you help me please to solve this problem
Posted by chagdharam on March 23, 2009 at 02:31 AM IST #
this does not allow me send mail, while running this gives message
encrypted email has been detected
Posted by chagdharam on March 23, 2009 at 02:37 AM IST #
Hi,
Is possibility to change body of e-mail as HTML and encoded in UTF-8 ?
Best regards
Slawek
Posted by Slawek on November 01, 2009 at 08:00 PM IST #