001/*
002 * Copyright 2002-2014 the original author or authors.
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 *      https://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, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.mail.javamail;
018
019import java.util.Date;
020import javax.mail.MessagingException;
021import javax.mail.internet.MimeMessage;
022
023import org.springframework.mail.MailMessage;
024import org.springframework.mail.MailParseException;
025
026/**
027 * Implementation of the MailMessage interface for a JavaMail MIME message,
028 * to let message population code interact with a simple message or a MIME
029 * message through a common interface.
030 *
031 * <p>Uses a MimeMessageHelper underneath. Can either be created with a
032 * MimeMessageHelper instance or with a JavaMail MimeMessage instance.
033 *
034 * @author Juergen Hoeller
035 * @since 1.1.5
036 * @see MimeMessageHelper
037 * @see javax.mail.internet.MimeMessage
038 */
039public class MimeMailMessage implements MailMessage {
040
041        private final MimeMessageHelper helper;
042
043
044        /**
045         * Create a new MimeMailMessage based on the given MimeMessageHelper.
046         * @param mimeMessageHelper the MimeMessageHelper
047         */
048        public MimeMailMessage(MimeMessageHelper mimeMessageHelper) {
049                this.helper = mimeMessageHelper;
050        }
051
052        /**
053         * Create a new MimeMailMessage based on the given JavaMail MimeMessage.
054         * @param mimeMessage the JavaMail MimeMessage
055         */
056        public MimeMailMessage(MimeMessage mimeMessage) {
057                this.helper = new MimeMessageHelper(mimeMessage);
058        }
059
060        /**
061         * Return the MimeMessageHelper that this MimeMailMessage is based on.
062         */
063        public final MimeMessageHelper getMimeMessageHelper() {
064                return this.helper;
065        }
066
067        /**
068         * Return the JavaMail MimeMessage that this MimeMailMessage is based on.
069         */
070        public final MimeMessage getMimeMessage() {
071                return this.helper.getMimeMessage();
072        }
073
074
075        @Override
076        public void setFrom(String from) throws MailParseException {
077                try {
078                        this.helper.setFrom(from);
079                }
080                catch (MessagingException ex) {
081                        throw new MailParseException(ex);
082                }
083        }
084
085        @Override
086        public void setReplyTo(String replyTo) throws MailParseException {
087                try {
088                        this.helper.setReplyTo(replyTo);
089                }
090                catch (MessagingException ex) {
091                        throw new MailParseException(ex);
092                }
093        }
094
095        @Override
096        public void setTo(String to) throws MailParseException {
097                try {
098                        this.helper.setTo(to);
099                }
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}