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:

package com.viettel.im.database.DAO;
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 www.codejava.net
*
*/
public class SendEmail {
public static void main(String[] args) {
try {
SendEmail.sendEmail(“smtp.viettel.com.vn”, “465”, “username”, “password”, “[email protected]”, “hello”, “hihihihihihih”);
} catch (Exception e) {
e.printStackTrace();
}
}
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(“mail.smtp.host”, 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.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(message);
// 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
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = “D:\\temp\\quaylai.xls”;
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
}

From:
http://www.tutorialspoint.com/java/java_sending_email.htm
http://www.codejava.net/java-ee/jsp/sending-e-mail-with-jsp-servlet-and-javamail
http://www.oracle.com/technetwork/java/javamail145sslnotes-1562622.html
http://www.tutorialspoint.com/javamail_api/javamail_api_smtp_servers.htm

Leave a Reply