org.codemonkey.simplejavamail.Email Maven / Gradle / Ivy
package org.codemonkey.simplejavamail;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.activation.DataSource;
import javax.mail.Message.RecipientType;
import javax.mail.util.ByteArrayDataSource;
/**
* Email message with all necessary data for an effective mailing action, including attachments etc.
*
* @author Benny Bottema
*/
public class Email {
/**
* The sender of the email. Can be used in conjunction with {@link #replyToRecipient}.
*/
private Recipient fromRecipient;
/**
* The reply-to-address, optional. Can be used in conjunction with {@link #fromRecipient}.
*/
private Recipient replyToRecipient;
/**
* The email message body in plain text.
*/
private String text;
/**
* The email message body in html.
*/
private String textHTML;
/**
* The subject of the email message.
*/
private String subject;
/**
* List of {@link Recipient}.
*/
private final List recipients;
/**
* List of {@link AttachmentResource}.
*/
private final List embeddedImages;
/**
* List of {@link AttachmentResource}.
*/
private final List attachments;
/**
* Map of header name and values, such as X-Priority
etc.
*/
private final Map headers;
/**
* Constructor, creates all internal lists.
*/
public Email() {
recipients = new ArrayList();
embeddedImages = new ArrayList();
attachments = new ArrayList();
headers = new HashMap();
}
/**
* Sets the sender address.
*
* @param name The sender's name.
* @param fromAddress The sender's email address.
*/
public void setFromAddress(final String name, final String fromAddress) {
fromRecipient = new Recipient(name, fromAddress, null);
}
/**
* Sets the reply-to address (optional).
*
* @param name The replied-to-receiver name.
* @param replyToAddress The replied-to-receiver email address.
*/
public void setReplyToAddress(final String name, final String replyToAddress) {
replyToRecipient = new Recipient(name, replyToAddress, null);
}
/**
* Bean setters for {@link #subject}.
*/
public void setSubject(final String subject) {
this.subject = subject;
}
/**
* Bean setters for {@link #text}.
*/
public void setText(final String text) {
this.text = text;
}
/**
* Bean setters for {@link #textHTML}.
*/
public void setTextHTML(final String textHTML) {
this.textHTML = textHTML;
}
/**
* Adds a new {@link Recipient} to the list on account of name, address and recipient type (eg. {@link RecipientType#CC}).
*
* @param name The name of the recipient.
* @param address The emailadres of the recipient.
* @param type The type of receiver (eg. {@link RecipientType#CC}).
* @see #recipients
* @see Recipient
* @see RecipientType
*/
public void addRecipient(final String name, final String address, final RecipientType type) {
recipients.add(new Recipient(name, address, type));
}
/**
* Adds an embedded image (attachment type) to the email message and generates the necessary {@link DataSource} with the given byte
* data. Then delegates to {@link #addEmbeddedImage(String, DataSource)}. At this point the datasource is actually a
* {@link ByteArrayDataSource}.
*
* @param name The name of the image as being referred to from the message content body (eg. '<cid:signature>').
* @param data The byte data of the image to be embedded.
* @param mimetype The content type of the given data (eg. "image/gif" or "image/jpeg").
* @see ByteArrayDataSource
* @see #addEmbeddedImage(String, DataSource)
*/
public void addEmbeddedImage(final String name, final byte[] data, final String mimetype) {
final ByteArrayDataSource dataSource = new ByteArrayDataSource(data, mimetype);
dataSource.setName(name);
addEmbeddedImage(name, dataSource);
}
/**
* Overloaded method which sets an embedded image on account of name and {@link DataSource}.
*
* @param name The name of the image as being referred to from the message content body (eg. '<cid:embeddedimage>').
* @param imagedata The image data.
*/
public void addEmbeddedImage(final String name, final DataSource imagedata) {
embeddedImages.add(new AttachmentResource(name, imagedata));
}
/**
* Adds a header to the {@link #headers} list. The value is stored as a String
.
*
* example: email.addHeader("X-Priority", 2)
*
* @param name The name of the header.
* @param value The value of the header, which will be stored using {@link String#valueOf(Object)}.
*/
public void addHeader(final String name, final Object value) {
headers.put(name, String.valueOf(value));
}
/**
* Adds an attachment to the email message and generates the necessary {@link DataSource} with the given byte data. Then delegates to
* {@link #addAttachment(String, DataSource)}. At this point the datasource is actually a {@link ByteArrayDataSource}.
*
* @param name The name of the extension (eg. filename including extension).
* @param data The byte data of the attachment.
* @param mimetype The content type of the given data (eg. "plain/text", "image/gif" or "application/pdf").
* @see ByteArrayDataSource
* @see #addAttachment(String, DataSource)
*/
public void addAttachment(final String name, final byte[] data, final String mimetype) {
final ByteArrayDataSource dataSource = new ByteArrayDataSource(data, mimetype);
dataSource.setName(name);
addAttachment(name, dataSource);
}
/**
* Overloaded method which sets an attachment on account of name and {@link DataSource}.
*
* @param name The name of the attachment (eg. 'filename.ext').
* @param filedata The attachment data.
*/
public void addAttachment(final String name, final DataSource filedata) {
attachments.add(new AttachmentResource(name, filedata));
}
/**
* Bean getter for {@link #fromRecipient}.
*/
public Recipient getFromRecipient() {
return fromRecipient;
}
/**
* Bean getter for {@link #replyToRecipient}.
*/
public Recipient getReplyToRecipient() {
return replyToRecipient;
}
/**
* Bean getter for {@link #subject}.
*/
public String getSubject() {
return subject;
}
/**
* Bean getter for {@link #text}.
*/
public String getText() {
return text;
}
/**
* Bean getter for {@link #textHTML}.
*/
public String getTextHTML() {
return textHTML;
}
/**
* Bean getter for {@link #attachments} as unmodifiable list.
*/
public List getAttachments() {
return Collections.unmodifiableList(attachments);
}
/**
* Bean getter for {@link #embeddedImages} as unmodifiable list.
*/
public List getEmbeddedImages() {
return Collections.unmodifiableList(embeddedImages);
}
/**
* Bean getter for {@link #recipients} as unmodifiable list.
*/
public List getRecipients() {
return Collections.unmodifiableList(recipients);
}
/**
* Bean getter for {@link #headers} as unmodifiable map.
*/
public Map getHeaders() {
return Collections.unmodifiableMap(headers);
}
}