001/* 002 * Copyright 2002-2017 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; 020 021import javax.mail.MessagingException; 022import javax.mail.internet.MimeMessage; 023 024import org.springframework.mail.MailMessage; 025import org.springframework.mail.MailParseException; 026 027/** 028 * Implementation of the MailMessage interface for a JavaMail MIME message, 029 * to let message population code interact with a simple message or a MIME 030 * message through a common interface. 031 * 032 * <p>Uses a MimeMessageHelper underneath. Can either be created with a 033 * MimeMessageHelper instance or with a JavaMail MimeMessage instance. 034 * 035 * @author Juergen Hoeller 036 * @since 1.1.5 037 * @see MimeMessageHelper 038 * @see javax.mail.internet.MimeMessage 039 */ 040public class MimeMailMessage implements MailMessage { 041 042 private final MimeMessageHelper helper; 043 044 045 /** 046 * Create a new MimeMailMessage based on the given MimeMessageHelper. 047 * @param mimeMessageHelper the MimeMessageHelper 048 */ 049 public MimeMailMessage(MimeMessageHelper mimeMessageHelper) { 050 this.helper = mimeMessageHelper; 051 } 052 053 /** 054 * Create a new MimeMailMessage based on the given JavaMail MimeMessage. 055 * @param mimeMessage the JavaMail MimeMessage 056 */ 057 public MimeMailMessage(MimeMessage mimeMessage) { 058 this.helper = new MimeMessageHelper(mimeMessage); 059 } 060 061 /** 062 * Return the MimeMessageHelper that this MimeMailMessage is based on. 063 */ 064 public final MimeMessageHelper getMimeMessageHelper() { 065 return this.helper; 066 } 067 068 /** 069 * Return the JavaMail MimeMessage that this MimeMailMessage is based on. 070 */ 071 public final MimeMessage getMimeMessage() { 072 return this.helper.getMimeMessage(); 073 } 074 075 076 @Override 077 public void setFrom(String from) throws MailParseException { 078 try { 079 this.helper.setFrom(from); 080 } 081 catch (MessagingException ex) { 082 throw new MailParseException(ex); 083 } 084 } 085 086 @Override 087 public void setReplyTo(String replyTo) throws MailParseException { 088 try { 089 this.helper.setReplyTo(replyTo); 090 } 091 catch (MessagingException ex) { 092 throw new MailParseException(ex); 093 } 094 } 095 096 @Override 097 public void setTo(String to) throws MailParseException { 098 try { 099 this.helper.setTo(to); 100 } 101 catch (MessagingException ex) { 102 throw new MailParseException(ex); 103 } 104 } 105 106 @Override 107 public void setTo(String... to) throws MailParseException { 108 try { 109 this.helper.setTo(to); 110 } 111 catch (MessagingException ex) { 112 throw new MailParseException(ex); 113 } 114 } 115 116 @Override 117 public void setCc(String cc) throws MailParseException { 118 try { 119 this.helper.setCc(cc); 120 } 121 catch (MessagingException ex) { 122 throw new MailParseException(ex); 123 } 124 } 125 126 @Override 127 public void setCc(String... cc) throws MailParseException { 128 try { 129 this.helper.setCc(cc); 130 } 131 catch (MessagingException ex) { 132 throw new MailParseException(ex); 133 } 134 } 135 136 @Override 137 public void setBcc(String bcc) throws MailParseException { 138 try { 139 this.helper.setBcc(bcc); 140 } 141 catch (MessagingException ex) { 142 throw new MailParseException(ex); 143 } 144 } 145 146 @Override 147 public void setBcc(String... bcc) throws MailParseException { 148 try { 149 this.helper.setBcc(bcc); 150 } 151 catch (MessagingException ex) { 152 throw new MailParseException(ex); 153 } 154 } 155 156 @Override 157 public void setSentDate(Date sentDate) throws MailParseException { 158 try { 159 this.helper.setSentDate(sentDate); 160 } 161 catch (MessagingException ex) { 162 throw new MailParseException(ex); 163 } 164 } 165 166 @Override 167 public void setSubject(String subject) throws MailParseException { 168 try { 169 this.helper.setSubject(subject); 170 } 171 catch (MessagingException ex) { 172 throw new MailParseException(ex); 173 } 174 } 175 176 @Override 177 public void setText(String text) throws MailParseException { 178 try { 179 this.helper.setText(text); 180 } 181 catch (MessagingException ex) { 182 throw new MailParseException(ex); 183 } 184 } 185 186}