You are on page 1of 7

Java Mail using Swing

Introduction:

In this I will discuss to develop Desktop Application to send email using Java. This application
can be used for send email to any email id for e.g. gmail.com, hotmail.com, yahoo.com and
many more.

Jdk1.5.0
Net Beans 5.0/5.5.1 / Sun Java Studio Enterprise 8
Javamail-1.3.3
Jaf-1.1

Definition:

Java Mail It is a messaging service, which used to send and receive messages over the
network and the Internet. It handles several message formats, such as plain text, message with
attachments and others. It uses Post Office Protocol (POP) / Internet Message Access Protocol
(IMAP) to receive message and Simple Mail Transfer Protocol (SMTP) to send message.

Java Mail API: - It is set of classes and interface, which provides a platform and protocol
independent framework to build mail and messaging applications. The JavaMail API is available
as an optional package for use with Java SE platform and is also included in the Java EE
platform.

Following are the important class and interface which is used to send and receive mail.

javax.mail.internet.MimeMessage
- This class is used to create email message in MIME style

javax.mail.Flags.Flag
- This class represents system flag such as message is deleted, answered, drafted,
recent, flagged, and seen.

javax.mail.internet.InternetAddress
- This class represents valid internet email address for e.g. abc@gmail.com

javax.mail.Session
- This is final class and represents a mail session to send/receive email

javax.mail.Store
- An abstract class which represents a message store and its access protocol, for storing
and retrieving messages.

javax.mail.Folder
- An abstract class that represents a folder for mail messages for e.g. INBOX, DRAFT.

javax.mail.Transport
- An abstract class that models a message transport, and used to send message.

Java Mail using Swing By Rahul Kumar


Article Code: ACE-08025 Page 1 of 7
Developing Java Mail Desktop Application in NetBeans using Swing:

File -> New Project -> Categories : General -> Projects : Java Application
Click on Next -> Project Name: MyMailApp -> Select project location -> Click on Finish.

Download the javamail-1.3.3 from http://java.sun.com/products/javamail/javamail-1_3_3.html


, and jaf-1.1 from
http://java.sun.com/javase/technologies/desktop/javabeans/jaf/downloads/index.html

Unzip this file in local drive.

Click on Tools Menu -> Click on Library Manager


Click on New Library -> Library Name: JavaMail -> Click on Ok.

Java Mail using Swing By Rahul Kumar


Article Code: ACE-08025 Page 2 of 7
Click on Add JAR/Folder -> Select mail.jar file from javamail-1.3.3 folder
Click on Add JAR/Folder Button
Same way create one more library named JAF and add activiation.jar file into it.

Right click on Libraries of MyMailApp -> Add Library -> Select JavaMail
Click on Add Library -> Same way add JAF library to the project.

Right click on Source Package of Project -> New -> Click on Java Class
Class Name: MyAuth -> Package Name: mymailapp -> Click on Finish Button

Replace MyAuth.java with following code

package mymailapp;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MyAuth extends Authenticator


{
private String emailId, password;

public MyAuth(String emailId, String password)


{
this.emailId = emailId;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(emailId,password);
}
}

Right click on Project -> New -> Click on JFrame Form.


Class Name: JavaMailFrame -> package : mymailapp -> Click on Finish Button

Drag the following components from Palate and change the label and change the variable
name (By right click on Component and Click on Change Variable Name).

Java Mail using Swing By Rahul Kumar


Article Code: ACE-08025 Page 3 of 7
Drag jOptionPane1 in outside of Frame, it will visible in Inspector Window

Components Text Property Variable Name


jLabel1 My Mail Program lblMessage
jLabel2 Email ID: [From] lblEmailId
jLabel3 Password lblPassword
jLabel4 To: lblTo
jLabel5 Subject lblSubject
jTextField1 txtEmailId
jPasswordField1 txtPassword
jTextField2 txtTo
jTextField3 txtSubject
jTextArea1 txaMsg
jButton1 Send Mail btnSend
jOptionPane1 dlgEmail

Now your Frame should look below.

Import following package in Source Code of Frame

import java.util.Date;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

Type the below code in Action Event of btnSend

Java Mail using Swing By Rahul Kumar


Article Code: ACE-08025 Page 4 of 7
String to = txtTo.getText();
String from = txtEmailId.getText();

// Smtp Address of Mail Server here I am using Gmail


String host = "smtp.gmail.com";

//Properties file contains host details


java.util.Properties props = System.getProperties();

props.put("mail.smtp.user", from);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");

String pwd = new String(txtPassword.getPassword());


Session session = Session.getInstance(props, new
mymailapp.MyAuth(from,pwd));
try
{
// Instantiatee a message
Message msg = new MimeMessage(session);

//Set message attributes


msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(txtSubject.getText());
msg.setSentDate(new Date());

// Set message content


msg.setText(txaMsg.getText());

//Send the message


Transport.send(msg);

dlgEmail.showMessageDialog(this,"Message has been successfully


send to " + to,"Mail",javax.swing.JOptionPane.INFORMATION_MESSAGE);

System.out.println("Message has been successfully send to " +


to);
}
catch(Exception ee)
{
System.out.println("Error : " + ee.toString());
dlgEmail.showMessageDialog(this,"Error : " +
ee.toString(),"Mail",javax.swing.JOptionPane.ERROR_MESSAGE);
}

Java Mail using Swing By Rahul Kumar


Article Code: ACE-08025 Page 5 of 7
Right Click on Project and Click on Build Project
Right Click on JavaMailFrame and Click on Run File
Enter the Email Id [From]: your gmail id (Because this application is configured using gmail)
Type the Valid password of your gmail account -> Type Email id in To Textbox, to whom
you want to send email (It can be any email id for eg. abc@hotmail.com, abc@yahoo.com ).
Enter the Subject and Message in text Area-> Click on Send Mail Button

Following Message will display if Email id and Password are valid.

If you entered Invalid email id or password following error message will display.

Java Mail using Swing By Rahul Kumar


Article Code: ACE-08025 Page 6 of 7
Now this email is received by user rahulk252.

Conclusion:

In this I explained to JavaMail API for sending email. In this application I have not developed own
email server, but I used SMTP server of Gmail (Because it s Free!!!). Using same SMTP we can
develop application which will send email with attachment and in different format.

Java Mail using Swing By Rahul Kumar


Article Code: ACE-08025 Page 7 of 7

You might also like