View Javadoc

1   /***
2    * @(#)MailMessageImpl.java
3    * 
4    * JFoxSOAF, Service-Oriented Application Framework
5    * 
6    * Copyright(c) JFoxSOAF Team
7    * 
8    * Licensed under the GNU LGPL, Version 2.1 (the "License"); 
9    * you may not use this file except in compliance with the License. 
10   * You may obtain a copy of the License at  
11   * 
12   * http://www.gnu.org/copyleft/lesser.html
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, 
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17   * See the License for the specific language governing permissions and 
18   * limitations under the License. 
19   * 
20   * For more information, please visit:
21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22   * http://www.huihoo.org/jfox/jfoxsoaf
23   */
24  
25  package org.huihoo.jfox.soaf.services.mail;
26  
27  import java.io.UnsupportedEncodingException;
28  import java.util.Date;
29  import java.util.List;
30  
31  import javax.mail.Message;
32  import javax.mail.MessagingException;
33  import javax.mail.Multipart;
34  import javax.mail.Session;
35  import javax.mail.internet.InternetAddress;
36  import javax.mail.internet.MimeBodyPart;
37  import javax.mail.internet.MimeMessage;
38  import javax.mail.internet.MimeMultipart;
39  
40  import org.huihoo.jfox.soaf.exception.MailException;
41  
42  /***
43   * <p>
44   * Implementation of the MailMessage interface.
45   * </p>
46   * 
47   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
48   * @version $Revision: 1.1 $ $Date: 2005/05/22 06:50:11 $
49   * @version Revision: 1.0
50   */
51  
52  public class MailMessageImpl implements MailMessage {
53  
54      private MimeMessage mimeMessage;
55  
56      private static final String CONTENT_TYPE_HTML = "text/html";
57  
58      private static final String CONTENT_TYPE_CHARSET_SUFFIX = ";charset=";
59  
60      /***
61       * @param session
62       */
63      public MailMessageImpl(Session session) {
64          this.mimeMessage = new MimeMessage(session);
65      }
66  
67      /***
68       * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setFrom(java.lang.String,
69       *      java.lang.String)
70       */
71      public void setFrom(String from, String personal) throws MailException {
72          try {
73              this.mimeMessage.setFrom(new InternetAddress(from, personal));
74          } catch (UnsupportedEncodingException e) {
75              throw new MailException(e);
76          } catch (MessagingException e) {
77              throw new MailException(e);
78          }
79      }
80  
81      /***
82       * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setFrom(java.lang.String)
83       */
84      public void setFrom(String from) throws MailException {
85          try {
86              this.mimeMessage.setFrom(new InternetAddress(from));
87          } catch (MessagingException e) {
88              throw new MailException(e);
89          }
90      }
91  
92      /***
93       * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setReplyTo(java.lang.String)
94       */
95      public void setReplyTo(String replyTo) throws MailException {
96          try {
97              this.mimeMessage
98                      .setReplyTo(new InternetAddress[] { new InternetAddress(
99                              replyTo) });
100         } catch (MessagingException e) {
101             throw new MailException(e);
102         }
103     }
104 
105     /***
106      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setTo(java.lang.String)
107      */
108     public void setTo(String to) throws MailException {
109         try {
110             this.mimeMessage.setRecipient(Message.RecipientType.TO,
111                     new InternetAddress(to));
112         } catch (MessagingException e) {
113             throw new MailException(e);
114         }
115     }
116 
117     /***
118      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setTo(java.util.List)
119      */
120     public void setTo(List toList) throws MailException {
121         try {
122             this.mimeMessage.setRecipients(Message.RecipientType.TO,
123                     toInternetAddressArray(toList));
124         } catch (MessagingException e) {
125             throw new MailException(e);
126         }
127 
128     }
129 
130     /***
131      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setCc(java.lang.String)
132      */
133     public void setCc(String cc) throws MailException {
134         try {
135             this.mimeMessage.setRecipient(Message.RecipientType.CC,
136                     new InternetAddress(cc));
137         } catch (MessagingException e) {
138             throw new MailException(e);
139         }
140     }
141 
142     /***
143      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setCc(java.util.List)
144      */
145     public void setCc(List ccList) throws MailException {
146         try {
147             this.mimeMessage.setRecipients(Message.RecipientType.CC,
148                     toInternetAddressArray(ccList));
149         } catch (MessagingException e) {
150             throw new MailException(e);
151         }
152     }
153 
154     /***
155      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setBcc(java.lang.String)
156      */
157     public void setBcc(String bcc) throws MailException {
158         try {
159             this.mimeMessage.setRecipient(Message.RecipientType.BCC,
160                     new InternetAddress(bcc));
161         } catch (MessagingException e) {
162             throw new MailException(e);
163         }
164     }
165 
166     /***
167      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setBcc(java.util.List)
168      */
169     public void setBcc(List bccList) throws MailException {
170         try {
171             this.mimeMessage.setRecipients(Message.RecipientType.BCC,
172                     toInternetAddressArray(bccList));
173         } catch (MessagingException e) {
174             throw new MailException(e);
175         }
176     }
177 
178     /***
179      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setSentDate(java.util.Date)
180      */
181     public void setSentDate(Date sentDate) throws MailException {
182         try {
183             this.mimeMessage.setSentDate(sentDate);
184         } catch (MessagingException e) {
185             throw new MailException(e);
186         }
187 
188     }
189 
190     /***
191      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setSubject(java.lang.String)
192      */
193     public void setSubject(String subject) throws MailException {
194         try {
195             this.mimeMessage.setSubject(subject);
196         } catch (MessagingException e) {
197             throw new MailException(e);
198         }
199     }
200 
201     /***
202      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setSubject(java.lang.String)
203      */
204     public void setSubject(String subject, String charset) throws MailException {
205         try {
206             this.mimeMessage.setSubject(subject, charset);
207         } catch (MessagingException e) {
208             throw new MailException(e);
209         }
210     }
211 
212     /***
213      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setText(java.lang.String)
214      */
215     public void setText(String text, boolean html) throws MailException {
216         try {
217             Multipart multipt = new MimeMultipart();
218             MimeBodyPart msgbody = new MimeBodyPart();
219             msgbody.setText(text);
220             multipt.addBodyPart(msgbody);
221             if (html) {
222                 this.mimeMessage.setContent(multipt, CONTENT_TYPE_HTML);
223             } else {
224                 this.mimeMessage.setContent(multipt);
225             }
226         } catch (MessagingException e) {
227             throw new MailException(e);
228         }
229     }
230 
231     /***
232      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setText(java.lang.String)
233      */
234     public void setText(String text, String charset, boolean html)
235             throws MailException {
236         try {
237             Multipart multipt = new MimeMultipart();
238             MimeBodyPart msgbody = new MimeBodyPart();
239             msgbody.setText(text, charset);
240             multipt.addBodyPart(msgbody);
241             if (html) {
242                 this.mimeMessage.setContent(multipt, CONTENT_TYPE_HTML
243                         + CONTENT_TYPE_CHARSET_SUFFIX + charset);
244             } else {
245                 this.mimeMessage.setContent(multipt);
246             }
247         } catch (MessagingException e) {
248             throw new MailException(e);
249         }
250     }
251 
252     protected InternetAddress[] toInternetAddressArray(List aList) {
253         InternetAddress[] ia = (InternetAddress[]) aList
254                 .toArray(new InternetAddress[0]);
255         return ia;
256     }
257 
258     /***
259      * @see org.huihoo.jfox.soaf.services.mail.MailMessage#setContentLanguage(java.lang.String[])
260      */
261     public void setContentLanguage(String[] langs) throws MailException {
262         try {
263             this.mimeMessage.setContentLanguage(langs);
264         } catch (MessagingException e) {
265             throw new MailException(e);
266         }
267     }
268 
269     /***
270      * see org.huihoo.jfox.soaf.services.mail.MailMessage#getMimeMessage()
271      */
272     public MimeMessage getMimeMessage() {
273         return this.mimeMessage;
274     }
275 
276 }