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