How do I receive mail using POP3?

In this example you will learn how to receive email using POP3. We are going to connect to Gmail server and read the messages in the INBOX folder. There are some steps that you need to do to download this email. Here are the steps:

  1. Setup properties for the mail session.
  2. Creates a javax.mail.Authenticator object.
  3. Creating mail session.
  4. Get the POP3 store provider and connect to the store.
  5. Get folder and open the INBOX folder in the store.
  6. Retrieve the messages from the folder.
  7. Close folder and close store.

These steps can be written as the following code snippet:

 

How do I create a simple mail client program in Swing?

The code snippet below show you how to create a simple swing application that can be used to send an e-mail. The program allows user to supply the from email address, to email address, the subject and the message of the email. User need to select the available smtp server to connect to and provide the username and password for authentication the the mail server.

Here is the user interface of this simple email client:

Simple E-mail Client

Simple E-mail Client

The main routine for sending the email is in the SendEmailActionListener class, which is an implementation of an ActionListener interface that will handle the emailing process when the Send E-mail button is pressed.

What are the system properties used for sending email?

Here are a list of system properties that can be use to send an e-mail using the JavaMail API.

PropertyDefault ValueDescription
mail.hostDefine the host name of the mail server.
mail.smtp.hostDefine the host name of the SMTP server; this will overrides the mail.host for SMTP connections only.
mail.protocol.hostDefine the host name of the specified protocol (POP, IMAP); this will overrides the mail.host.
mail.userDefine the default username sent to all mail servers.
mail.protocol.userDefine the default username for the specified protocol; this will overrides the mail.user for the specified protocol.
mail.smtp.port25Define the SMTP port on which the SMTP server is listening.
mail.protocol.portDefault port for the corresponding protocolDefine the port on which the servers for the specified protocol is listening.
mail.smtp.starttls.enableUpgrade the regular SMTP connection on the usual port to an encrypted (TLS or SSL) connection.
mail.smtp.connectiontimeoutInfiniteDefine the number of milliseconds to wait for a connection timeout.
mail.debugfalseDefine parameter for disabling or enabling information debugging.
mail.fromDefine the e-mail address to use in the From header.
mail.mime.charsetfile.encodingDefine the default character set used to send messages.
mail.alternatesDefine other email address for the current that will not to be included when replying to a message.
mail.protocol.classDefine the fully package qualified class name of the provider for the specified protocol.
mail.transport.protocolFirst transport provider in the configuration fileDefine default protocol with which to send messages.
mail.transport.protocol.address-typeDefine the message transport protocol such as SMTP for the specified address type, for example mail.transport.protocol.rfc822.
mail.replayallccfalsePut all recipients in the CC list of the reply message instead of the TO field when replying to all.