Using JavaMail API
Introduction:
I'll mention about the JavaMail API today.Before go into the details of the API we'll first look at the well known e-mail protocols SMTP, IMAP and POP3.After that we'll see how to use and configure Apache James server to use in our examples.
SMTP, IMAP and POP3 Protocols:
These are all TCP/IP protocols used for mail delivery.In summary, when you send an email to someone, your email client first connects to your email server.Then your email server uses the receipent email address to determine where the mail will have to go and delivers the mail to the target email server.Up to now the SMTP protocol is used.When the receipent want to see the email that you send she asks to her email server to see the email.At this time IMAP or POP3 has to be used.
SMTP stands for Simple Mail Transfer Protocol and is used when email is delivered from the email client to the server or email is delivered from one email server to another. The default SMPT port is 25.When you are sending an email your email client first contact with the SMTP server in the mail infrastructure.If configured so, SMTP server can authenticate you and then deliver your mail to the target server.After sending the email, the mail client may either keep the sent mail in local sent mail folder as in the POP3 protocol or sent mail folder on the server as in IMAP.
POP3 stands for Post Office Protocol and if you use POP3, when you want to see your emails, the emails will be downloaded from the mail server to your local drive deleting the emails on the server's drive which means you work as offline. POP3 is simpler than IMAP and doesn't offer some of the features of IMAP.The default POP3 port is 110.
IMAP stands for Internet Message Access Protocol and if you use IMAP, when you want to see your emails, besides the ability of downloading the emails to the local drive, they still be kept on the server's drive.Because of that IMAP requires more disk space and more CPU power on the mail server than POP3.The default IMAP port is 143.
Download and Configure the Apache James Email Server:
First go to the page http://james.apache.org/download.cgi and download the latest stable version of Apache James Server.(At the time of this write it is 2.3.2 version).
Next, you have to configure config.xml according to your need.The config.xml should be under ${JAMES_HOME}/apps/james/SAR-INF directory. Some of the configuration values are listed below.You can change the email delivery method either as pop3 or imap, or you can change the port values like below.
*
You can also configure your James server according to the instructions listed in this link.These configuration files should be in ${JAMES_HOME}/apps/james/conf directory.
In the config.xml you may also configure the gateway, gatewayPort,gatewayusername and gatewayPassword properties to be able to send emails to an external SMTP server.Remember however that, if you want to send email to an external SMTP server, you may need to sure that your James server IP address and your domain IP address are same.That's because Email servers do a reverse dns query by comparing the IP of the domain and the IP of the sender email server and doesn't accept the email if they are not the same.You can get more information about reverse dns from this link.
After completing the configuration you can now go to the bin directory and run the run.bat or run.sh file.Be sure to set the JAVA_HOME environment variable at this point. You should see the following command lines now.
Sending Email:
Below java code is an example of sending an email from the "admin" user to the "test" user we have just created.
package tr.com.aligelenler;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
private static final String USER = "admin";
private static final String PASSWORD = "admin";
public static String FROM = "admin@localhost";
public static String TO = "test@localhost";
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "1125");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER, PASSWORD);
}
});
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(FROM));
msg.addRecipients(Message.RecipientType.TO,InternetAddress.parse(TO));
msg.setSubject("You have a new mail");
msg.setText("This is a test message send at " + new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSSZ").format(new Date()));
Transport.send(msg);
} catch (MessagingException e) {
System.out.println(e.getMessage());
}
}
}