1 package hayashi.yuu.tools.mail;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.UnsupportedEncodingException;
7 import java.util.Properties;
8 import java.util.Vector;
9 import javax.mail.Address;
10 import javax.mail.Message;
11 import javax.mail.MessagingException;
12 import javax.mail.Session;
13 import javax.mail.Transport;
14 import javax.mail.Store;
15 import javax.mail.internet.InternetAddress;
16 import javax.mail.internet.MimeBodyPart;
17 import javax.mail.internet.MimeMessage;
18 import javax.mail.internet.MimeMultipart;
19 import javax.mail.internet.MimeUtility;
21 import hayashi.yuu.tools.mail.SiteData;
24 * <p>Eメールを送信するためのクラス<br/>
26 * @see <a href="http://java.sun.com/products/javamail/javadocs/index.html">JavaMail API</a>
29 * @version 2010/02/05 SMTPサーバーのポート番号指定を追加
33 public static void main(java.lang.String[] args) {
35 SiteData siteData = new SiteData("sendmail.properties");
36 SendMail mail = new SendMail(siteData);
37 mail.setSubject("[SendMail] SendMail テスト");
38 mail.setContent("本文\n");
39 mail.setAttachment(new File("activation-1.1.jar"));
47 protected Vector<String> toVector = new Vector<String>(); // プロパティ 送信先(to) の値
48 protected Vector<String> ccVector = new Vector<String>(); // プロパティ 送信先(Cc) の値
49 protected Vector<String> bccVector = new Vector<String>(); // プロパティ 送信先(Bcc) の値
50 protected String from = null; // プロパティ 発信元(from) の値
51 protected String subject; // プロパティ 題名(subject) の値
52 protected String content; // プロパティ 本文(content) の値
53 protected String mailHost; // プロパティ SMTP(mail.smtp.host) の値
54 protected String mailHostPort; // プロパティ SMTP_PORT(mail.smtp.port) の値
56 protected Vector<File> attachments = new Vector<File>(); // 添付ファイル
57 protected Vector<String> attachNames = new Vector<String>(); // 添付ファイル名
59 protected boolean POP_before_SMTP = false;
60 protected boolean STARTTLS = false;
61 protected boolean USER_AUTH = false;
62 protected String popServer = "";
63 protected String userId = "";
64 protected String password ="";
65 protected Session session = null;
68 * 新しい YuuMail インスタンスを作成する
69 * @param mailProp メール設定ファイルのパス名称
71 public SendMail(String mailProp) {
72 this(new SiteData(mailProp));
77 * @param siteData メール設定値を包含したオブジェクト
79 public SendMail(SiteData siteData) {
82 if (checkItem(siteData.MAIL_SMTP)) {
83 this.setMailHost(siteData.MAIL_SMTP);
85 if (checkItem(siteData.MAIL_SMTP_PORT)) {
86 this.setMailPort(siteData.MAIL_SMTP_PORT);
88 if (checkItem(siteData.MAIL_TO)) {
89 this.toVector.add(siteData.MAIL_TO);
91 if (checkItem(siteData.MAIL_CC)) {
92 this.ccVector.add(siteData.MAIL_CC);
94 if (checkItem(siteData.MAIL_BCC)) {
95 this.bccVector.add(siteData.MAIL_BCC);
97 if (checkItem(siteData.MAIL_FROM)) {
98 this.from = siteData.MAIL_FROM;
101 this.POP_before_SMTP = siteData.POP_before_SMTP;
102 this.STARTTLS = siteData.STARTTLS;
103 this.USER_AUTH = siteData.USER_AUTH;
104 this.popServer = siteData.MAIL_POP;
105 this.userId = siteData.USER_ID;
106 this.password = siteData.PASSWORD;
108 // メール(MimeMessageオブジェクト)を生成する
109 Properties props = System.getProperties();
110 props.put("mail.smtp.host", getMailHost());
111 props.put("mail.smtp.port", getMailPort());
112 if (this.USER_AUTH) {
113 props.put("mail.smtp.auth", "true");
116 props.put("mail.smtp.starttls.enable", "true");
118 this.session = Session.getDefaultInstance(props, null);
120 private boolean checkItem(String itemStr) {
121 if (itemStr == null) {
124 if (itemStr.trim().equals("")) {
131 * プロパティ mailHost の取得メソッド。
132 * @return プロパティ mailHost の値。
134 public String getMailHost() {
135 return this.mailHost;
140 * @param mailHost SMTPサーバー名またはSMTPサーバーのIPアドレス
142 public void setMailHost(String mailHost) {
143 this.mailHost = mailHost;
147 * プロパティ SMTPポート番号 の取得メソッド。
148 * @return SMTPポート番号を表す文字列(デシマル)
150 public String getMailPort() {
151 return this.mailHostPort;
156 * @param portStr SMTPポート番号を表す文字列(デシマル)
158 public void setMailPort(String portStr) {
159 this.mailHostPort = portStr;
166 public void setTo(String to) {
167 if (!to.equals("")) {
168 toVector.addElement(to);
176 public void setCc(String cc) {
177 if (!cc.equals("")) {
178 ccVector.addElement(cc);
184 * @param bcc 送信先(Bcc)
186 public void setBcc(String bcc) {
187 if (!bcc.equals("")) {
188 bccVector.addElement(bcc);
193 * プロパティ from の取得メソッド。
194 * @return プロパティ from の値。
196 public String getFrom() {
201 * プロパティ 送信元(from) の設定メソッド。
202 * @param from 送信元メールアドレス
204 public void setFrom(String from) {
209 * プロパティ subject の取得メソッド。
210 * @return プロパティ subject の値。
212 public String getSubject() {
217 * プロパティ 題名(subject) の設定メソッド。
219 * @param subject メールタイトル(題名)
221 public void setSubject(String subject) {
222 this.subject = subject;
226 * 本文(content)の取得メソッド。
229 public String getContent() {
234 * 本文(content)の設定メソッド。
236 * @param content 本文(content)
238 public void setContent(String content) {
239 this.content = content;
244 * 添付するファイル(attachment)を設定.添付ファイルは複数設定可。
245 * @param attachment 添付するファイル(attachment)
246 * @throws FileNotFoundException 添付するファイルが見つからなかった。
248 public void setAttachment(File attachment) throws FileNotFoundException {
249 new FileInputStream(attachment); // ファイルが実存することをチェックする。
250 attachments.addElement(attachment);
251 attachNames.addElement(attachment.getName());
256 * 添付するファイル(attachment)を設定.添付ファイルは複数設定可。
257 * @param attachment 添付するファイル(attachment)
258 * @param name 添付するファイルの表示名
259 * @throws FileNotFoundException 添付するファイルが見つからなかった。
261 public void setAttachment(File attachment, String name) throws FileNotFoundException {
262 new FileInputStream(attachment); // ファイルが実存することをチェックする。
263 attachments.addElement(attachment);
264 attachNames.addElement(name);
269 * @throws MessagingException エラー:メールの送信に失敗しました。
270 * @throws UnsupportedEncodingException エラー:メールの文字エンコードに失敗しました。
272 public void send() throws MessagingException, UnsupportedEncodingException {
273 //-------------------------
274 // POP before SMTP 対応
277 if (this.POP_before_SMTP == true) {
278 System.out.println("store.connect('"+ popServer +"', '"+ userId +"', '"+ password +"')");
279 store = this.session.getStore("pop3");
280 store.connect(popServer, userId, password);
283 System.out.println("no POP_before_SMTP!");
286 MimeMessage message = createMessage();
288 if (this.USER_AUTH) {
289 System.out.println("transport.connect(null, '"+ userId +"', '"+ password+"')");
290 Transport transport = this.session.getTransport("smtp");
291 transport.connect(null, userId, password);
292 Address[] addrs = message.getAllRecipients();
293 for (Address addr : addrs) {
294 System.out.println("[Addr] "+ addr.toString());
296 transport.sendMessage(message, message.getAllRecipients());
299 System.out.println("no SMTP_AUTH!");
300 Transport.send(message);
303 if (this.POP_before_SMTP == true) {
311 * @throws MessagingException メール送信に失敗しました。
312 * @throws UnsupportedEncodingException 文字エンコーディングに失敗した。
314 protected MimeMessage createMessage() throws MessagingException, UnsupportedEncodingException {
315 MimeMessage msg = new MimeMessage(this.session);
318 InternetAddress[] toList = new InternetAddress[toVector.size()];
319 for (int i=0; i < toVector.size(); i++) {
320 toList[i] = new InternetAddress(toVector.elementAt(i));
322 msg.setRecipients(Message.RecipientType.TO, toList);
325 InternetAddress[] ccList = new InternetAddress[ccVector.size()];
326 for (int i=0; i < ccVector.size(); i++) {
327 ccList[i] = new InternetAddress(ccVector.elementAt(i));
329 msg.setRecipients(Message.RecipientType.CC, ccList);
332 InternetAddress[] bccList = new InternetAddress[bccVector.size()];
333 for (int i=0; i < bccVector.size(); i++) {
334 bccList[i] = new InternetAddress(bccVector.elementAt(i));
336 msg.setRecipients(Message.RecipientType.BCC, bccList);
339 msg.setFrom(new InternetAddress(getFrom()));
342 msg.setSubject(MimeUtility.encodeText(getSubject(), "iso-2022-jp", "B"));
345 MimeMultipart naiyou = new MimeMultipart();
346 msg.setContent(naiyou);
348 MimeBodyPart honbun = new MimeBodyPart();
349 honbun.setContent(getContent(), "text/plain; charset=\"iso-2022-jp\"");
350 naiyou.addBodyPart(honbun);
353 for (int i=0; i < attachments.size(); i++) {
354 File attachFile = attachments.elementAt(i);
355 MimeBodyPart tenpu = new MimeBodyPart();
356 javax.activation.FileDataSource dfs = new javax.activation.FileDataSource(attachFile);
357 javax.activation.DataHandler dh = new javax.activation.DataHandler(dfs);
358 tenpu.setDataHandler(dh);
359 tenpu.setFileName(attachNames.elementAt(i));
360 naiyou.addBodyPart(tenpu);