Friday, September 17, 2010

Java mail sample

Here is a sample to send mail by java mail. It also attach some data in memory as an attach file.

In this sample, the mime type is “text/plain”, it could be jpg, pdf… to get the full list of mime type, see

package com.telus.itms;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;

public class MailSender {

public static void main(String[] args) throws MessagingException {
EmailTemplate email = new EmailTemplate();
email.setTo(new String[]{"", ""});
email.setContent("attchment content");
email.setSubject("TEST EMAIL");
email.setText("TEST EMAIL CONTENT");



private static void sendMessage(final EmailTemplate email) throws MessagingException, AddressException {
// Create empty properties
Properties props = new Properties();

// Setup mail server
props.put("", email.getSmtpHost());

// Get session
Session session = Session.getDefaultInstance(props, null);

// Define message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(email.getFrom()));

List toAddressList = new ArrayList();
for (int i=0; i toAddressList.add(new InternetAddress(email.getTo()[i]));
message.addRecipients(Message.RecipientType.TO, (Address[])toAddressList.toArray(new Address[]{}));

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

// Fill the message

Multipart multipart = new MimeMultipart();

// Part two is attachment
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(email.getContent(), email.getContentType());

// Put parts in message

// Send the message

public static class EmailTemplate {
private String smtpHost;
private String from;
private String[] to;
private String subject;
private String text;
private String attachNameToDispaly;
private Object content;
private String contentType;
public String getFrom() {
return from;
public void setFrom(String from) {
this.from = from;
public String[] getTo() {
return to;
public void setTo(String[] to) { = to;
public String getSubject() {
return subject;
public void setSubject(String subject) {
this.subject = subject;
public String getText() {
return text;
public void setText(String text) {
this.text = text;
public String getAttachNameToDispaly() {
return attachNameToDispaly;
public void setAttachNameToDispaly(String attachNameToDispaly) {
this.attachNameToDispaly = attachNameToDispaly;
public Object getContent() {
return content;
public void setContent(Object content) {
this.content = content;
public String getContentType() {
return contentType;
public void setContentType(String contentType) {
this.contentType = contentType;
public String getSmtpHost() {
return smtpHost;
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;

No comments:

Post a Comment