Java Mail Using Swing
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.
File -> New Project -> Categories : General -> Projects : Java Application
Click on Next -> Project Name: MyMailApp -> Select project location -> Click on Finish.
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
package mymailapp;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
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).
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;
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");
If you entered Invalid email id or password following error message will display.
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.