How to send emails using JavaMail
javamail
send emails javamail
javamail smtp
This article will present a simple example how to send emails using JavaMail.
Dependency
To get the JavaMail, download the latest version and attach it to the project.
For Maven users you only need to add it as a dependency:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
Example
package com.admfactory.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaMailSender {
private String host;
private String port;
private boolean debug;
private Properties props;
public JavaMailSender(String host, String port) {
this.host = host;
this.port = port;
/** Get a Properties object */
props = System.getProperties();
}
public Properties addProxy(String host, String port) {
props.setProperty("proxySet", "true");
props.setProperty("socksProxyHost", host);
props.setProperty("socksProxyPort", port);
return props;
}
public boolean send(String from, String password, String to, String cc, String title, String text) {
boolean result = false;
try {
props.setProperty("mail.smtp.host", this.host);
props.setProperty("mail.smtp.port", this.port);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] addressTO = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, addressTO);
InternetAddress[] addressCC = { new InternetAddress(cc) };
msg.setRecipients(Message.RecipientType.CC, addressCC);
InternetAddress addressFROM = new InternetAddress(from);
msg.setFrom(addressFROM);
msg.setSentDate(new Date());
msg.setSubject(title);
msg.setText(text);
Transport.send(msg);
result = true;
} catch (Exception ex) {
ex.printStackTrace();
result = false;
}
return result;
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public static void main(String[] args) {
System.out.println("Send emails example using JavaMail");
String host = "smtp.gmail.com";
String port = "587";
JavaMailSender sender = new JavaMailSender(host, port);
/** Activate this line for proxy authentication and change the settings with your details */
// sender.addProxy("10.10.10.10", "8080");
/** Activate this line if you need to see more details */
// sender.setDebug(true);
String from = "example1@gmail.com";
String password = "password";
String to = "example2@gmail.com";
String cc = "example3@yahoo.com";
String subject = "test";
String msg = "Message sent using JavaMail.";
System.out.println();
System.out.println();
boolean b = sender.send(from, password, to, cc, subject, msg);
if (b) {
System.out.println("Message sent successfully.");
} else {
System.out.println("Message failed.");
}
}
}
If the application will be behind a proxy server activate the proxy settings and provide the right details:
sender.addProxy("10.10.10.10", "8080");
Output
If you go with current setup the output should be similar with:
Send emails example using JavaMail
DEBUG: JavaMail version 1.4.7
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
Message sent successfully.
If you activate the debug, by uncommenting the line, sender.setDebug(true);
you will see a bigger output with a lot of useful information.