View Javadoc

1   package com.ozacc.mail;
2   
3   import java.io.File;
4   import java.io.InputStream;
5   import java.net.URL;
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import javax.activation.DataSource;
10  import javax.activation.FileDataSource;
11  import javax.activation.FileTypeMap;
12  import javax.activation.URLDataSource;
13  
14  import com.ozacc.mail.impl.ByteArrayDataSource;
15  
16  /***
17   * ¥Þ¥?¥Á¥Ñ¡¼¥È¡¦¥á¡¼¥?¡£¥Õ¥¡¥¤¥?¤òźÉÕ¤·¤¿¤ê¡¢ËÜʸ¤ËHTML¤ò»ÈÍѤǤ­¤Þ¤¹¡£
18   * <p>
19   * <code>setHtmlText(String)</code>¤È<code>setText(String)</code>¥á¥½¥Ã¥É¤ÎξÊ?¤ò»ÈÍѤ·¤ÆËÜʸ¤ò¥»¥Ã¥È¤·¤¿¾?¹ç¤Ï¡¢
20   * ¥×¥?¡¼¥ó¥Æ¥­¥¹¥È¤ÎËÜʸ¤ÈHTML¤ÎËÜʸ¤ÎξÊ?¤¬¥»¥Ã¥È¤µ¤?¤¿¥á¡¼¥?¤Ë¤Ê¤ê¤Þ¤¹¡£
21   * <p>
22   * <strong>Ã?:</strong> <code>addFile(File, String)</code>¥á¥½¥Ã¥É¤ò»ÈÍѤ·¤ÆÇ¤°Õ¤Î¥Õ¥¡¥¤¥?̾¤ò¥»¥Ã¥È¤¹¤?¾?¹ç¡¢¥Õ¥¡¥¤¥?̾¤ËŬÀڤʳÈÄ¥»Ò¤òÉÕ¤±¤Æ¤¯¤À¤µ¤¤¡£
23   * 
24   * @since 1.1
25   * 
26   * @author Tomohiro Otsuka
27   * @version $Id: MultipartMail.java,v 1.5 2004/09/17 20:32:31 otsuka Exp $
28   */
29  public class MultipartMail extends Mail {
30  
31  	private String htmlText;
32  
33  	private List attachmentFiles = new ArrayList();
34  
35  	/***
36  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
37  	 */
38  	public MultipartMail() {
39  		super();
40  	}
41  
42  	/***
43  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
44  	 * °¸Àè¤äº¹½Ð¿Í¤Î̾Á°¤ò¥¨¥ó¥³¡¼¥É¤¹¤?»?¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£
45  	 * ¥Ç¥Õ¥©¥?¥È¤Ï<code>ISO-2022-JP</code>¤Ç¤¹¡£
46  	 * <p>
47  	 * Æ?Ëܸ?´Ä¶­¤ÇÍøÍѤ¹¤?¾?¹ç¤ÏÄ̾?Êѹ¹¤¹¤?ɬÍפϤ¢¤ê¤Þ¤»¤ó¡£
48  	 * 
49  	 * @param charset ¥¨¥ó¥³¡¼¥É¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
50  	 */
51  	public MultipartMail(String charset) {
52  		super(charset);
53  	}
54  
55  	/***
56  	 * ¥³¥Ô¡¼¥³¥ó¥¹¥È¥é¥¯¥¿¡£
57  	 * ¥·¥ã¥ú½¼¥³¥Ô¡¼(shallow copy)¤Ç¤¹¡£
58  	 * 
59  	 * @param original ¥³¥Ô¡¼¸µ¤ÎMail¥¤¥ó¥¹¥¿¥ó¥¹
60  	 */
61  	public MultipartMail(Mail original) {
62  		super(original);
63  	}
64  
65  	/***
66  	 * ¥³¥Ô¡¼¥³¥ó¥¹¥È¥é¥¯¥¿¡£
67  	 * ¥·¥ã¥ú½¼¥³¥Ô¡¼(shallow copy)¤Ç¤¹¡£
68  	 * 
69  	 * @param original ¥³¥Ô¡¼¸µ¤ÎMultipartMail¥¤¥ó¥¹¥¿¥ó¥¹
70  	 */
71  	public MultipartMail(MultipartMail original) {
72  		super(original);
73  		this.htmlText = original.htmlText;
74  		this.attachmentFiles = original.attachmentFiles;
75  	}
76  
77  	/***
78  	 * HTML¤ÎËÜʸ¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
79  	 * 
80  	 * @param htmlText HTML¤ÎËÜʸ
81  	 */
82  	public void setHtmlText(String htmlText) {
83  		this.htmlText = htmlText;
84  	}
85  
86  	/***
87  	 * HTML¤ÎËÜʸ¤òÊÖ¤·¤Þ¤¹¡£
88  	 * 
89  	 * @return HTML¤ÎËÜʸ¡£¤Þ¤¿¤Ïnull¡£
90  	 */
91  	public String getHtmlText() {
92  		return htmlText;
93  	}
94  
95  	/***
96  	 * »ØÄꤵ¤?¤¿¥Õ¥¡¥¤¥?¤òźÉÕ¤·¤Þ¤¹¡£
97  	 * źÉÕ¥Õ¥¡¥¤¥?̾¤Ë¤Ï¡¢»ØÄꤵ¤?¤¿¥Õ¥¡¥¤¥?¤Î̾Á°¤¬»ÈÍѤµ¤?¤Þ¤¹¡£
98  	 * ¤³¤Î¥Õ¥¡¥¤¥?¤Î̾Á°¤ÏŬÀڤʳÈÄ¥»Ò¤¬ÉÕ¤±¤é¤?¤Æ¤¤¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£
99  	 * 
100 	 * @param file źÉÕ¥Õ¥¡¥¤¥?
101 	 */
102 	public void addFile(File file) {
103 		addFile(file, file.getName());
104 	}
105 
106 	/***
107 	 * »ØÄꤵ¤?¤¿¥Õ¥¡¥¤¥?¤òźÉÕ¤·¤Þ¤¹¡£
108 	 * »ØÄꤹ¤?¥Õ¥¡¥¤¥?̾¤Ë¤ÏŬÀڤʳÈÄ¥»Ò¤¬ÉÕ¤±¤é¤?¤Æ¤¤¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£
109 	 * 
110 	 * @param file źÉÕ¥Õ¥¡¥¤¥?
111 	 * @param fileName ¥Õ¥¡¥¤¥?̾
112 	 */
113 	public void addFile(File file, String fileName) {
114 		attachmentFiles.add(new AttachmentFile(fileName, file));
115 	}
116 
117 	/***
118 	 * »ØÄꤵ¤?¤¿URL¤Î¥Õ¥¡¥¤¥?¤òźÉÕ¤·¤Þ¤¹¡£
119 	 * »ØÄꤹ¤?¥Õ¥¡¥¤¥?̾¤Ë¤ÏŬÀڤʳÈÄ¥»Ò¤¬ÉÕ¤±¤é¤?¤Æ¤¤¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£
120 	 * 
121 	 * @param url źÉÕ¥Õ¥¡¥¤¥?
122 	 * @param fileName ¥Õ¥¡¥¤¥?̾
123 	 */
124 	public void addFile(URL url, String fileName) {
125 		attachmentFiles.add(new AttachmentFile(fileName, url));
126 	}
127 
128 	/***
129 	 * »ØÄꤵ¤?¤¿InputStream¤ò¥Õ¥¡¥¤¥?¤È¤·¤ÆÅºÉÕ¤·¤Þ¤¹¡£
130 	 * »ØÄꤹ¤?¥Õ¥¡¥¤¥?̾¤Ë¤ÏŬÀڤʳÈÄ¥»Ò¤¬ÉÕ¤±¤é¤?¤Æ¤¤¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£
131 	 * 
132 	 * @param is źÉÕ¥Õ¥¡¥¤¥?¤òÀ¸À®¤¹¤?InputStream
133 	 * @param fileName ¥Õ¥¡¥¤¥?̾
134 	 */
135 	public void addFile(InputStream is, String fileName) {
136 		attachmentFiles.add(new AttachmentFile(fileName, is));
137 	}
138 
139 	/***
140 	 * źÉÕ¥Õ¥¡¥¤¥?¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
141 	 * źÉÕ¥Õ¥¡¥¤¥?¤¬¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¾?¹ç¤Ï¡¢¶õ¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
142 	 * 
143 	 * @return źÉÕ¥Õ¥¡¥¤¥?¤ÎÇÛÎ󡣤ޤ¿¤Ï¶õ¤ÎÇÛÎó¡£
144 	 */
145 	public AttachmentFile[] getAttachmentFiles() {
146 		if (attachmentFiles == null) {
147 			return new AttachmentFile[0];
148 		}
149 		return (AttachmentFile[])attachmentFiles
150 				.toArray(new AttachmentFile[attachmentFiles.size()]);
151 	}
152 
153 	/***
154 	 * HTML¤ÎËÜʸ¤¬¥»¥Ã¥È¤µ¤?¤Æ¤¤¤?¤«¤É¤¦¤«È½Äꤷ¤Þ¤¹¡£
155 	 * 
156 	 * @return HTML¤ÎËÜʸ¤¬¥»¥Ã¥È¤µ¤?¤Æ¤¤¤?¾?¹? true
157 	 */
158 	public boolean isHtmlMail() {
159 		return (htmlText != null);
160 	}
161 
162 	/***
163 	 * ¥Õ¥¡¥¤¥?¤¬ÅºÉÕ¤µ¤?¤Æ¤¤¤?¤«¤É¤¦¤«È½Äꤷ¤Þ¤¹¡£
164 	 * 
165 	 * @return ¥Õ¥¡¥¤¥?¤¬ÅºÉÕ¤µ¤?¤Æ¤¤¤?¾?¹? true
166 	 */
167 	public boolean hasAttachmentFile() {
168 		return attachmentFiles.size() > 0;
169 	}
170 
171 	/***
172 	 * źÉÕ¥Õ¥¡¥¤¥?¡£
173 	 * 
174 	 * @since 1.1
175 	 * @author Tomohiro Otsuka
176 	 * @version $Id: MultipartMail.java,v 1.5 2004/09/17 20:32:31 otsuka Exp $
177 	 */
178 	public class AttachmentFile {
179 
180 		private String name;
181 
182 		private File file;
183 
184 		private InputStream is;
185 
186 		private URL url;
187 
188 		/***
189 		 * ¥Õ¥¡¥¤¥?̾¤È¥Õ¥¡¥¤¥?¤ò»ØÄꤷ¤Æ¡¢¤³¤Î¥¯¥é¥¹¤Î¥¤¥ó¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
190 		 * ¥Õ¥¡¥¤¥?̾¤Ë¤ÏŬÀڤʳÈÄ¥»Ò¤¬ÉÕ¤±¤é¤?¤Æ¤¤¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£
191 		 * 
192 		 * @param name ¥á¡¼¥?¤Ëɽ¼¨¤¹¤?¥Õ¥¡¥¤¥?̾
193 		 * @param file źÉÕ¥Õ¥¡¥¤¥?
194 		 */
195 		public AttachmentFile(String name, File file) {
196 			this.name = name;
197 			this.file = file;
198 		}
199 
200 		/***
201 		 * ¥Õ¥¡¥¤¥?̾¤ÈInputStream¤ò»ØÄꤷ¤Æ¡¢¤³¤Î¥¯¥é¥¹¤Î¥¤¥ó¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
202 		 * ¥Õ¥¡¥¤¥?̾¤Ë¤ÏŬÀڤʳÈÄ¥»Ò¤¬ÉÕ¤±¤é¤?¤Æ¤¤¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£
203 		 * 
204 		 * @param name ¥á¡¼¥?¤Ëɽ¼¨¤¹¤?¥Õ¥¡¥¤¥?̾
205 		 * @param is źÉÕ¥Õ¥¡¥¤¥?¤òÀ¸À®¤¹¤?InputStream
206 		 */
207 		public AttachmentFile(String name, InputStream is) {
208 			this.name = name;
209 			this.is = is;
210 		}
211 
212 		/***
213 		 * ¥Õ¥¡¥¤¥?̾¤È¥Õ¥¡¥¤¥?¥úÁ±¡¼¥·¥ç¥ó¤ÎURL¤ò»ØÄꤷ¤Æ¡¢¤³¤Î¥¯¥é¥¹¤Î¥¤¥ó¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
214 		 * ¥Õ¥¡¥¤¥?̾¤Ë¤ÏŬÀڤʳÈÄ¥»Ò¤¬ÉÕ¤±¤é¤?¤Æ¤¤¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£
215 		 * 
216 		 * @param name ¥á¡¼¥?¤Ëɽ¼¨¤¹¤?¥Õ¥¡¥¤¥?̾
217 		 * @param is źÉÕ¥Õ¥¡¥¤¥?¤Î¥úÁ±¡¼¥·¥ç¥óURL
218 		 */
219 		public AttachmentFile(String name, URL url) {
220 			this.name = name;
221 			this.url = url;
222 		}
223 
224 		/***
225 		 * źÉÕ¥Õ¥¡¥¤¥?¤ÎDataSource¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤ÆÊÖ¤·¤Þ¤¹¡£
226 		 * 
227 		 * @return źÉÕ¥Õ¥¡¥¤¥?¤ÎDataSource¥¤¥ó¥¹¥¿¥ó¥¹
228 		 */
229 		public DataSource getDataSource() {
230 			if (file != null) {
231 				return new FileDataSource(file);
232 			}
233 
234 			if (url != null) {
235 				return new URLDataSource(url);
236 			}
237 
238 			// InputStream¤«¤éDataSource¤òÀ¸À®
239 			String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(name);
240 			return new ByteArrayDataSource(is, contentType);
241 		}
242 
243 		/***
244 		 * źÉÕ¥Õ¥¡¥¤¥?̾¤òÊÖ¤·¤Þ¤¹¡£
245 		 * 
246 		 * @return źÉÕ¥Õ¥¡¥¤¥?̾
247 		 */
248 		public String getName() {
249 			return name;
250 		}
251 
252 		/***
253 		 * @return ¥»¥Ã¥È¤µ¤?¤¿¥Õ¥¡¥¤¥?¡£¤Þ¤¿¤Ïnull¡£
254 		 */
255 		public File getFile() {
256 			return file;
257 		}
258 
259 		/***
260 		 * @return ¥»¥Ã¥È¤µ¤?¤¿InputStream¡£¤Þ¤¿¤Ïnull¡£
261 		 */
262 		public InputStream getInputStream() {
263 			return is;
264 		}
265 
266 		/***
267 		 * @return ¥»¥Ã¥È¤µ¤?¤¿URL¡£¤Þ¤¿¤Ïnull¡£
268 		 */
269 		public URL getUrl() {
270 			return url;
271 		}
272 	}
273 }