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 http://www.w3schools.com/media/media_mimeref.asp
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.setSmtpHost("bcmsg.tsl.telus.com");
email.setFrom("yao.dong@telus.com");
email.setTo(new String[]{"yao.dong@telus.com", "dong_yao@hotmail.com"});
email.setAttachNameToDispaly("status.txt");
email.setContent("attchment content");
email.setContentType("text/plain");
email.setSubject("TEST EMAIL");
email.setText("TEST EMAIL CONTENT");
sendMessage(email);
}
private static void sendMessage(final EmailTemplate email) throws MessagingException, AddressException {
// Create empty properties
Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", 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; itoAddressList.add(new InternetAddress(email.getTo()[i]));
}
message.addRecipients(Message.RecipientType.TO, (Address[])toAddressList.toArray(new Address[]{}));
message.setSubject(email.getSubject());
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(email.getText());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
messageBodyPart.setFileName(email.getAttachNameToDispaly());
messageBodyPart.setContent(email.getContent(), email.getContentType());
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
Transport.send(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) {
this.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