View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.mail.javamail;
18  
19  import java.util.Date;
20  import javax.mail.MessagingException;
21  import javax.mail.internet.MimeMessage;
22  
23  import org.springframework.mail.MailMessage;
24  import org.springframework.mail.MailParseException;
25  
26  /**
27   * Implementation of the MailMessage interface for a JavaMail MIME message,
28   * to let message population code interact with a simple message or a MIME
29   * message through a common interface.
30   *
31   * <p>Uses a MimeMessageHelper underneath. Can either be created with a
32   * MimeMessageHelper instance or with a JavaMail MimeMessage instance.
33   *
34   * @author Juergen Hoeller
35   * @since 1.1.5
36   * @see MimeMessageHelper
37   * @see javax.mail.internet.MimeMessage
38   */
39  public class MimeMailMessage implements MailMessage {
40  
41  	private final MimeMessageHelper helper;
42  
43  
44  	/**
45  	 * Create a new MimeMailMessage based on the given MimeMessageHelper.
46  	 * @param mimeMessageHelper the MimeMessageHelper
47  	 */
48  	public MimeMailMessage(MimeMessageHelper mimeMessageHelper) {
49  		this.helper = mimeMessageHelper;
50  	}
51  
52  	/**
53  	 * Create a new MimeMailMessage based on the given JavaMail MimeMessage.
54  	 * @param mimeMessage the JavaMail MimeMessage
55  	 */
56  	public MimeMailMessage(MimeMessage mimeMessage) {
57  		this.helper = new MimeMessageHelper(mimeMessage);
58  	}
59  
60  	/**
61  	 * Return the MimeMessageHelper that this MimeMailMessage is based on.
62  	 */
63  	public final MimeMessageHelper getMimeMessageHelper() {
64  		return this.helper;
65  	}
66  
67  	/**
68  	 * Return the JavaMail MimeMessage that this MimeMailMessage is based on.
69  	 */
70  	public final MimeMessage getMimeMessage() {
71  		return this.helper.getMimeMessage();
72  	}
73  
74  
75  	@Override
76  	public void setFrom(String from) throws MailParseException {
77  		try {
78  			this.helper.setFrom(from);
79  		}
80  		catch (MessagingException ex) {
81  			throw new MailParseException(ex);
82  		}
83  	}
84  
85  	@Override
86  	public void setReplyTo(String replyTo) throws MailParseException {
87  		try {
88  			this.helper.setReplyTo(replyTo);
89  		}
90  		catch (MessagingException ex) {
91  			throw new MailParseException(ex);
92  		}
93  	}
94  
95  	@Override
96  	public void setTo(String to) throws MailParseException {
97  		try {
98  			this.helper.setTo(to);
99  		}
100 		catch (MessagingException ex) {
101 			throw new MailParseException(ex);
102 		}
103 	}
104 
105 	@Override
106 	public void setTo(String[] to) throws MailParseException {
107 		try {
108 			this.helper.setTo(to);
109 		}
110 		catch (MessagingException ex) {
111 			throw new MailParseException(ex);
112 		}
113 	}
114 
115 	@Override
116 	public void setCc(String cc) throws MailParseException {
117 		try {
118 			this.helper.setCc(cc);
119 		}
120 		catch (MessagingException ex) {
121 			throw new MailParseException(ex);
122 		}
123 	}
124 
125 	@Override
126 	public void setCc(String[] cc) throws MailParseException {
127 		try {
128 			this.helper.setCc(cc);
129 		}
130 		catch (MessagingException ex) {
131 			throw new MailParseException(ex);
132 		}
133 	}
134 
135 	@Override
136 	public void setBcc(String bcc) throws MailParseException {
137 		try {
138 			this.helper.setBcc(bcc);
139 		}
140 		catch (MessagingException ex) {
141 			throw new MailParseException(ex);
142 		}
143 	}
144 
145 	@Override
146 	public void setBcc(String[] bcc) throws MailParseException {
147 		try {
148 			this.helper.setBcc(bcc);
149 		}
150 		catch (MessagingException ex) {
151 			throw new MailParseException(ex);
152 		}
153 	}
154 
155 	@Override
156 	public void setSentDate(Date sentDate) throws MailParseException {
157 		try {
158 			this.helper.setSentDate(sentDate);
159 		}
160 		catch (MessagingException ex) {
161 			throw new MailParseException(ex);
162 		}
163 	}
164 
165 	@Override
166 	public void setSubject(String subject) throws MailParseException {
167 		try {
168 			this.helper.setSubject(subject);
169 		}
170 		catch (MessagingException ex) {
171 			throw new MailParseException(ex);
172 		}
173 	}
174 
175 	@Override
176 	public void setText(String text) throws MailParseException {
177 		try {
178 			this.helper.setText(text);
179 		}
180 		catch (MessagingException ex) {
181 			throw new MailParseException(ex);
182 		}
183 	}
184 
185 }