001 /* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016 package org.opengion.fukurou.mail; 017 018 import org.opengion.fukurou.util.HybsEntry ; 019 import org.opengion.fukurou.util.Argument ; 020 import org.opengion.fukurou.util.LogWriter; 021 022 import java.util.Map; 023 import java.util.LinkedHashMap; 024 025 import java.util.logging.Logger; 026 import java.util.logging.Level; 027 import javax.mail.MessagingException ; 028 029 /** 030 * MailReceiver は、POP3プロトコルによるメール受信プログラ?す? 031 * 032 * 引数には、host,user,passwd,headerList,mailSaveDir,fileSaveDir,filter,help が指定できます? 033 * メールは、メ?ージID名をファイル名として、セーブします? 034 * 添付ファイルは、?のファイル名で、指定??レクトリに出力される為、?のメール? 035 * 同時に抜く場合?、uniq 属?を付与してください。その場合?、メ?ージID??番??のファイル? 036 * で、ファイルをセーブします?添付ファイルには、同名?ファイルを?添付すること? 037 * できるため、保存時には、添付ファイルの番号を?番としてファイル名に、付与します? 038 * 039 * 引数のプロパテイのキー部は、大??小文字が厳格に適用されます?で、正確に記述願います? 040 * 041 * java org.opengion.fukurou.fukurou.mail.MailReceiver 042 * -host=メールサーバ?(??) 043 * -user=メールを取得するログインユーザー(??) 044 * -passwd=メールを取得するログインパスワー???) 045 * -protocol=受信サーバ?のプロトコル[imap/pop3]を指?初期値:pop3) 046 * -port=受信サーバ?のポ?トを??初期値:-1) 047 * -mailSaveDir=受信メールをセーブするディレクトリ。指定がな??合?、標準?力へ出力する? 048 * -fileSaveDir=添付ファイルをセーブするディレクトリ。指定がな??合?抜き出さな?? 049 * -useMsgId=添付ファイルをセーブするディレクトリに、MesssageIdフォル?個別に割り当てるかど?(初期値:false) 050 * -maxRowCount=受信メールの?取り込み件数(初期値:100)(0:[無制限]) 051 * -match_Subject=受信メールのSubjectを選択する条件 052 * -match_Body=受信メールのBodyを選択する条件 053 * -match_From=受信メールのFromを選択する条件 054 * -match_XXXX=受信メールのヘッ??部のキーXXXXを選択する条件 055 * -delete=検索後?メールをサーバ?から削除するかど?を?true/falseで??初期値:false) 056 * -help=使用方法を出力して、終?ます? 057 * 058 * @version 0.9.0 2000/11/13 059 * @author Kazuhiko Hasegawa 060 * @since JDK5.0, 061 */ 062 public class MailReceiver { 063 private static Logger logger = Logger.getLogger( "org.opengion.fukurou.fukurou.mail.MailReceiver" ); // 4.3.3.5 (2008/11/08) 064 065 private static final String CR = System.getProperty("line.separator"); 066 067 /** 受信メールの?取り込み件数を指定します? {@value} */ 068 public static final int MAX_ROW_COUNT = 100 ; 069 070 /** 検索後?メールをサーバ?から削除するかど?を?true/falseで?します? {@value} */ 071 public static final boolean DELETE_MESSAGE = false ; 072 073 /** メールサーバ?の?ォルト?ロトコル {@value} */ 074 public static final String PROTOCOL = "pop3" ; 075 076 /** メールサーバ?の?ォルト?ート番号 {@value} */ 077 public static final int PORT = -1 ; 078 079 private Argument argment = null; 080 081 private static final Map<String,String> mustProparty ; 082 private static final Map<String,String> usableProparty ; 083 084 static { 085 mustProparty = new LinkedHashMap<String,String>(); 086 mustProparty.put( "host", "メールサーバ?(??)" ); 087 mustProparty.put( "user", "メールを取得するログインユーザー(??)" ); 088 mustProparty.put( "passwd", "メールを取得するログインパスワー???)" ); 089 090 usableProparty = new LinkedHashMap<String,String>(); 091 usableProparty.put( "protocol", "受信サーバ?のプロトコル(imap,pop3)を指?初期値:pop3)" ); 092 usableProparty.put( "port", "受信サーバ?のポ?トを??初期値:-1)" ); 093 usableProparty.put( "mailSaveDir", "受信メールをセーブするディレクトリ? + 094 CR + "?がな??合?、標準?力へ出力する?" ); 095 usableProparty.put( "fileSaveDir", "添付ファイルをセーブするディレクトリ? + 096 CR + "?がな??合?抜き出さな??" ); 097 usableProparty.put( "useMsgId", "添付ファイルをセーブするディレクトリに? + 098 CR + "MesssageIdフォル?個別に割り当てるかど?? ); 099 usableProparty.put( "maxRowCount", "受信メールの?取り込み件数(初期値:100)(0:[無制限])" ); 100 usableProparty.put( "match_Subject", "受信メールのSubjectを選択する条件" ); 101 usableProparty.put( "match_Body", "受信メールのBodyを選択する条件" ); 102 usableProparty.put( "match_From", "受信メールのFromを選択する条件" ); 103 usableProparty.put( "match_", "受信メールのヘッ??部のキーXXXXを選択する条件" ); 104 usableProparty.put( "delete", "検索後?メールをサーバ?から削除するかど?を?" + 105 CR + "true/falseで??初期値:false)" ); 106 usableProparty.put( "help", "使用方法を出力して、終?ます?" ); 107 } 108 109 /** 110 * レシーバ?を開始します? 111 * 112 * @og.rev 4.3.3.5 (2008/11/08) Argument オブジェクトへの引数?util ?mail に訂正します? 113 * 114 * @param args 引数配? 115 */ 116 public void start( final String[] args ) throws MessagingException { 117 118 // パラメータの解析?取? 119 logger.fine( "パラメータの解析?取? ); 120 argment = new Argument( "org.opengion.fukurou.fukurou.mail.MailReceiver" ); // 4.3.3.5 (2008/11/08) 121 argment.setMustProparty( mustProparty ); 122 argment.setUsableProparty( usableProparty ); 123 124 argment.setArgument( args ); 125 126 // help パラメータ?true に?された場合?処?? 127 if( argment.getProparty( "help",false ) ) { 128 System.out.println( argment.toString() ); 129 return; 130 } 131 132 // 処??な?パラメータを取得しておきます? 133 logger.fine( "処??な?パラメータを取得します?" ); 134 MailRX recive = new MailRX(); 135 136 recive.setHost( argment.getProparty( "host" ) ) ; 137 recive.setUser( argment.getProparty( "user" ) ) ; 138 recive.setPasswd( argment.getProparty( "passwd" ) ) ; 139 recive.setProtocol( argment.getProparty( "protocol",PROTOCOL ) ) ; 140 recive.setPort( argment.getProparty( "port",PORT ) ) ; 141 recive.setDelete( argment.getProparty( "delete",DELETE_MESSAGE ) ) ; 142 recive.setMaxRowCount( argment.getProparty( "maxRowCount",MAX_ROW_COUNT ) ) ; 143 144 // ??条件にマッチしたメ?ージのみ抜き出す為の、SearchTerm オブジェクト?作? 145 logger.fine( "??条件にマッチしたメ?ージのみ抜き出す条件を設定します?" ); 146 HybsEntry[] matchs = argment.getEntrys( "match_" ); 147 for( int i=0; i<matchs.length; i++ ) { 148 recive.addMatchTerm( matchs[i] ) ; 149 } 150 151 // リスナ?を設定して、受信メールを?ずつ処?ます? 152 logger.fine( "リスナ?を設定して、受信メールを?ずつ処?ます?" ); 153 String mailSaveDir = argment.getProparty( "mailSaveDir" ); 154 String fileSaveDir = argment.getProparty( "fileSaveDir" ); 155 boolean useMsgId = argment.getProparty( "useMsgId",false ); 156 157 MailReceiveListener listener = new ReceiveListener( mailSaveDir,fileSaveDir,useMsgId ) ; 158 recive.setMailReceiveListener( listener ); 159 160 recive.start(); 161 } 162 163 /** 164 * メール受信プログラ?使用する MailReceiveListener の実??部クラスです? 165 * 166 * @version 0.9.0 2000/11/13 167 * @author Kazuhiko Hasegawa 168 * @since JDK5.0, 169 */ 170 private static class ReceiveListener implements MailReceiveListener { 171 private final String mailSaveDir ; 172 private final String fileSaveDir ; 173 private final boolean useMsgId ; 174 private int counter = 0; 175 176 /** 177 * コンストラクター 178 * 179 * @param mailSaveDir メールをセーブする?合?保存フォル? 180 * @param fileSaveDir メールの添付ファイルをセーブする?合?保存フォル? 181 * @param useMsgId 添付ファイルをセーブする?合に、メ?ージIDを使用するかど? 182 */ 183 public ReceiveListener( final String mailSaveDir,final String fileSaveDir,final boolean useMsgId ) { 184 this.mailSaveDir = mailSaveDir; 185 this.fileSaveDir = fileSaveDir; 186 this.useMsgId = useMsgId; 187 } 188 189 /** 190 * 受信処?行います? 191 * 192 * @param message MailMessage 193 * @return 結果(true:正常/false:異常) 194 */ 195 public boolean receive( final MailMessage message ) { 196 String msg = "[" + counter++ + "]" + message.getMessageID() + " 受信中" ; 197 System.out.println( msg ); 198 199 if( mailSaveDir != null ) { 200 message.saveMessage( mailSaveDir ); 201 } 202 else { 203 System.out.println( message.getSubject() ); 204 System.out.println( message.getContent() ); 205 } 206 207 if( fileSaveDir != null ) { 208 message.saveAttachFiles( fileSaveDir,useMsgId ); 209 } 210 return true ; 211 } 212 } 213 214 /** 215 * main メソ?です? 216 * 217 * @param args コマンド引数配? 218 */ 219 public static void main ( final String[] args ) { 220 MailReceiver receiver = new MailReceiver(); 221 try { 222 logger.info( "メール受信処?開始しま? ---------------------------------------------" ); 223 receiver.start( args ); 224 logger.info( "正常に終?ました? ); 225 } 226 catch( Throwable th ) { 227 String errMsg = "メール受信中に例外が発生しました?" 228 + CR + receiver.argment 229 + CR + th.getMessage() ; 230 LogWriter.log( errMsg ); 231 logger.log( Level.SEVERE,errMsg, th ); 232 throw new RuntimeException( errMsg,th ); // 4.0.0 (2005/01/31) 233 } 234 } 235 }