/**
*
*/
package jp.co.company.util;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import sun.misc.BASE64Encoder;
/**
* @author author
* @date 2009/8/19
*
*/
public class SendMail {
// メールホスト情報
private String host = null;
// 発信アドレス
private String fromAddr = null;
// メールのユーザ
private String username = null;
// メールのパースワード
private String password = null;
// メールのタイトル
private String title = null;
// 添付ファイル定義
private String attachPath = null;
private String attachName = null;
//メールしスト定義
List<String> ccList = null;
List<String> toList = null;
// メール内容
StringBuffer strBufferCon = new StringBuffer();
/**
* @return the ccList
*/
public List<String> getCcList() {
return ccList;
}
/**
* @param ccList the ccList to set
*/
public void setCcList(List<String> ccList) {
this.ccList = ccList;
}
/**
* @return the toList
*/
public List<String> getToList() {
return toList;
}
/**
* @param toList the toList to set
*/
public void setToList(List<String> toList) {
this.toList = toList;
}
/**
* @return the host
*/
public String getHost() {
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return the fromAddr
*/
public String getFromAddr() {
return fromAddr;
}
/**
* @param fromAddr the fromAddr to set
*/
public void setFromAddr(String fromAddr) {
this.fromAddr = fromAddr;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the attachPath
*/
public String getAttachPath() {
return attachPath;
}
/**
* @param attachPath the attachPath to set
*/
public void setAttachPath(String attachPath) {
this.attachPath = attachPath;
}
/**
* @return the attachName
*/
public String getAttachName() {
return attachName;
}
/**
* @param attachName the attachName to set
*/
public void setAttachName(String attachName) {
this.attachName = attachName;
}
/**
* @return the strBufferCon
*/
public StringBuffer getStrBufferCon() {
return strBufferCon;
}
/**
* @param strBufferCon the strBufferCon to set
*/
public void setStrBufferCon(StringBuffer strBufferCon) {
this.strBufferCon = strBufferCon;
}
public void sendMail() throws Exception{
Properties props = new Properties(); //システムプロパーティ
props.put("mail.smtp.host", getHost());
props.put("mail.smtp.auth", "true");
// プロパーティより会話セッションを作る
Session session = Session.getDefaultInstance(props);
session .setDebug(true);
// SessionによりMessage対象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(getFromAddr()));//発信アドレス設定
//受信者アドレス設定
if(this.getToList() != null){
for (int i =0;i < this.getToList().size();i++ ) {
message.addRecipient(Message.RecipientType.TO,new InternetAddress((String)this.getToList().get(i)));// 受信アドレス設定
}
}
// CCアドレス設定
if(this.getCcList() != null) {
for (int i =0;i < this.getCcList().size();i++ ) {
message.addRecipient(Message.RecipientType.CC,new InternetAddress((String)this.getCcList().get(i)));// 受信アドレス設定
}
}
//タイトル設定
message.setSubject(getTitle());
// メール内容設定
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setText(getStrBufferCon().toString());
// 添付ファイル設定
multipart.addBodyPart(contentPart);
if(getAttachPath() != null && getAttachName() != null){
BodyPart attachmentPart= new MimeBodyPart();
DataSource source = new FileDataSource(getAttachPath());
attachmentPart.setDataHandler(new DataHandler(source));
BASE64Encoder enc = new BASE64Encoder();
attachmentPart.setFileName("=?GB2312?B?"+enc.encode(getAttachName().getBytes())+"?=");
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host,getUsername(),getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
/*Test Class*/
/**
*
*/
package jp.co.company.util;
import java.util.ArrayList;
import java.util.List;
/**
* @author se
*
*/
public class MailTest {
public static void main(String[] args){
/*受信者リストとCCリスト定義*/
List<String> toList = new ArrayList<String>();
List<String> ccList = new ArrayList<String>();
//対象実例化
SendMail sender = new SendMail();
//メールホスト設定
sender.setHost("xx.co.jp");
// 発信者リスト
sender.setFromAddr("yy@xx.co.jp");
// 受信者リスト
toList.add("yy@xx.co.jp");
sender.setToList(toList);
// CC者リスト
ccList.add("cc@xx.co.jp");
sender.setCcList(ccList);
// ユーザ名、パースワード、タイトル設定
sender.setUsername("yy");
sender.setPassword("yy123");
sender.setTitle("ファイルアップロードの件について");
//添付ファイルのパスとファイル名を指定する
sender.setAttachPath("C://yy//filelupload.log");
sender.setAttachName("filelupload.log");
// メール内容設定
StringBuffer strBufCon = new StringBuffer();
strBufCon.append("管理者様/n");
strBufCon.append(" /n");
strBufCon.append("お疲れ様です。チームBです。/n");
strBufCon.append(" /n");
strBufCon.append("ファイルアップロード完了いたしましたので、報告いたします。結果は添付ファイルをご参照ください。/n");
strBufCon.append(" /n");
strBufCon.append("以上、よろしくお願いいたします。/n");
sender.setStrBufferCon(strBufCon);
try {
sender.sendMail();
} catch (Exception e){
e.printStackTrace();
}
}
}