Sending email in Java with SSL

I want use Pentaho to send email to 900 person, but I am not a expert of Pentaho, so I fail. Then, I have to code it by Java. I never have been code anything about email. Luckily, I can use Google to search something, after copy and paste, I have it:


import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

* A utility class for sending e-mail messages
* @author
public class SendEmail {

public static void main(String[] args) {
try {
SendEmail.sendEmail(“”, “465”, “username”, “password”, “[email protected]”, “hello”, “hihihihihihih”);
} catch (Exception e) {

public static void sendEmail(String host, String port,
final String userName, final String password, String toAddress,
String subject, String message) throws AddressException,
MessagingException {

// sets SMTP server properties
Properties properties = new Properties();

properties.put(“”, host);

properties.put(“mail.smtp.port”, port);

properties.put(“mail.smtp.auth”, “true”);

properties.put(“mail.smtp.starttls.enable”, “true”);
properties.put(“mail.smtp.ssl.enable”, “true”);

// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(userName, password);


Session session = Session.getInstance(properties, auth);

// creates a new e-mail message
Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(userName));

InternetAddress[] toAddresses = {new InternetAddress(toAddress)};

msg.setRecipients(Message.RecipientType.TO, toAddresses);


msg.setSentDate(new Date());


// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText(“This is message body”);

// Create a multipar message
Multipart multipart = new MimeMultipart();

// Set text message part
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = “D:\\temp\\quaylai.xls”;
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));

// Send the complete message parts

// sends the e-mail



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s