📄 mimemessagehelper.java
字号:
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mail.javamail;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.FileTypeMap;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePart;
import org.springframework.core.io.InputStreamSource;
/**
* Helper class for easy population of a <code>javax.mail.internet.MimeMessage</code>.
*
* <p>Mirrors the simple setters of SimpleMailMessage, directly applying the values
* to the underlying MimeMessage. Allows to define a character encoding for the
* entire message, automatically applied by all methods of this helper.
*
* <p>Also offers support for typical mail attachments, and for personal names
* that accompany mail addresses. Note that advanced settings can still be applied
* directly to underlying MimeMessage!
*
* <p>Typically used in MimeMessagePreparator implementations or JavaMailSender
* client code: simply instantiating it as a MimeMessage wrapper, invoking
* setters on the wrapper, using the underlying MimeMessage for mail sending.
* Also used internally by JavaMailSenderImpl.
*
* <p>Sample code:
*
* <pre>
* mailSender.send(new MimeMessagePreparator() {
* public void prepare(MimeMessage mimeMessage) throws MessagingException {
* MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
* message.setFrom("me@mail.com");
* message.setTo("you@mail.com");
* message.setSubject("my subject");
* message.setText("my text");
* message.addAttachment("logo.gif", new ClassPathResource("images/mylogo.gif"));
* }
* });</pre>
*
* @author Juergen Hoeller
* @since 19.01.2004
* @see javax.mail.internet.MimeMessage
* @see #getMimeMessage
* @see MimeMessagePreparator
* @see JavaMailSender
* @see JavaMailSenderImpl
* @see org.springframework.mail.SimpleMailMessage
*/
public class MimeMessageHelper {
private final MimeMessage mimeMessage;
private MimeMultipart mimeMultipart;
private String encoding;
/**
* Create new MimeMessageHelper for the given MimeMessage,
* assuming a simple text message (no multipart content).
* @param mimeMessage MimeMessage to work on
* @see #MimeMessageHelper(javax.mail.internet.MimeMessage, boolean)
*/
public MimeMessageHelper(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}
/**
* Create new MimeMessageHelper for the given MimeMessage,
* assuming a simple text message (no multipart content).
* @param mimeMessage MimeMessage to work on
* @param encoding the character encoding to use for the message
* @see #MimeMessageHelper(javax.mail.internet.MimeMessage, boolean)
*/
public MimeMessageHelper(MimeMessage mimeMessage, String encoding) {
this(mimeMessage);
this.encoding = encoding;
}
/**
* Create new MimeMessageHelper for the given MimeMessage,
* in multipart mode (supporting attachments) if requested.
* @param mimeMessage MimeMessage to work on
* @param multipart whether to create a multipart message that
* supports attachments
*/
public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart) throws MessagingException {
this.mimeMessage = mimeMessage;
if (multipart) {
this.mimeMultipart = new MimeMultipart();
this.mimeMessage.setContent(this.mimeMultipart);
}
}
/**
* Create new MimeMessageHelper for the given MimeMessage,
* in multipart mode (supporting attachments) if requested.
* @param mimeMessage MimeMessage to work on
* @param multipart whether to create a multipart message that
* supports attachments
* @param encoding the character encoding to use for the message
*/
public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart, String encoding)
throws MessagingException {
this(mimeMessage, multipart);
this.encoding = encoding;
}
/**
* Return the underlying MimeMessage.
*/
public MimeMessage getMimeMessage() {
return mimeMessage;
}
/**
* Return the character encoding used for this message.
*/
public String getEncoding() {
return encoding;
}
public void setFrom(InternetAddress from) throws MessagingException {
this.mimeMessage.setFrom(from);
}
public void setFrom(String from) throws MessagingException {
this.mimeMessage.setFrom(new InternetAddress(from));
}
public void setFrom(String from, String personal) throws MessagingException, UnsupportedEncodingException {
this.mimeMessage.setFrom(this.encoding != null ?
new InternetAddress(from, personal, this.encoding) :
new InternetAddress(from, personal));
}
public void setTo(InternetAddress to) throws MessagingException {
this.mimeMessage.setRecipient(Message.RecipientType.TO, to);
}
public void setTo(InternetAddress[] to) throws MessagingException {
this.mimeMessage.setRecipients(Message.RecipientType.TO, to);
}
public void setTo(String to) throws MessagingException {
this.mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
public void setTo(String[] to) throws MessagingException {
InternetAddress[] addresses = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
addresses[i] = new InternetAddress(to[i]);
}
this.mimeMessage.setRecipients(Message.RecipientType.TO, addresses);
}
public void addTo(InternetAddress to) throws MessagingException {
this.mimeMessage.addRecipient(Message.RecipientType.TO, to);
}
public void addTo(String to) throws MessagingException {
this.mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
public void addTo(String to, String personal) throws MessagingException, UnsupportedEncodingException {
this.mimeMessage.addRecipient(Message.RecipientType.TO,
this.encoding != null ?
new InternetAddress(to, personal, this.encoding) :
new InternetAddress(to, personal));
}
public void setCc(InternetAddress cc) throws MessagingException {
this.mimeMessage.setRecipient(Message.RecipientType.CC, cc);
}
public void setCc(InternetAddress[] cc) throws MessagingException {
this.mimeMessage.setRecipients(Message.RecipientType.CC, cc);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -