📄 email.java
字号:
package jodd.mail;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
/**
* E-mail bean contains all parts of an eamil and handle attachements.
*
* @2do default values ?
* @2do more attachements handlers
*/
public class Email {
// ---------------------------------------------------------------- address
private String from;
/**
* Sets the FROM address
*
* @param from FROM addess
*/
public void setFrom(String from) {
this.from = from;
}
/**
* Returns FROM address.
*
* @return FROM address
*/
public String getFrom() {
return from;
}
private String[] to;
/**
* Sets single TO address.
*
* @param to single TO address
*/
public void setTo(String to) {
this.to = new String[1];
this.to[0] = to;
}
/**
* Sets multiple TO addresses.
*
* @param to array of TO addresses
*/
public void setTo(String[] to) {
this.to = to;
}
/**
* Returns the array of TO addresses.
*
* @return array of TO addresses
*/
public String[] getTo() {
return to;
}
private String[] cc;
/**
* Sets single CC address
*
* @param cc single CC address
*/
public void setCc(String cc) {
this.cc = new String[1];
this.cc[0] = cc;
}
/**
* Sets multiple CC addresses.
*
* @param cc array of CC address.
*/
public void setCc(String[] cc) {
this.cc = cc;
}
/**
* Returns array of CC addresses.
*
* @return array of CC addresses
*/
public String[] getCc() {
return cc;
}
private String[] bcc;
/**
* Sets single BCC address.
*
* @param bcc single BCC address
*/
public void setBcc(String bcc) {
this.bcc = new String[1];
this.bcc[0] = bcc;
}
/**
* Sets multiple BCC addresses.
*
* @param bcc array of BCC address.
*/
public void setBcc(String[] bcc) {
this.bcc = bcc;
}
/**
* Returns array of BCC addresses.
*
* @return array of BCC addresses
*/
public String[] getBcc() {
return bcc;
}
// ---------------------------------------------------------------- content
private String subject;
/**
* Sets message subject.
*
* @param subject subject of a message
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* Returns message subject.
*
* @return message subject
*/
public String getSubject() {
return this.subject;
}
private boolean htmlMessage = false;
private String message;
/**
* Sets plain message text.
*
* @param message plain message text
*/
public void setMessage(String message) {
this.htmlMessage = false;
this.message = message;
}
/**
* Returns message text, either plain or HTML.
*
* @return message text
*/
public String getMessage() {
return this.message;
}
/**
* Sets HTML message text.
*
* @param htmlMessage
* HTML message text
*/
public void setHtmlMessage(String htmlMessage) {
this.htmlMessage = true;
this.message = htmlMessage;
}
/**
* Returns true if message is HTML, otherwise false.
*
* @return true if message is HTML, otherwise false
*/
public boolean isHtmlMessage() {
return this.htmlMessage;
}
// ---------------------------------------------------------------- attachments
private ArrayList attachments = new ArrayList();
/**
* Returns total number of attachements.
*
* @return total number of attachements
*/
public int getTotalAttachments() {
return attachments.size();
}
/**
* Returns one attachment body part, for javamail usage.
*
* @param i index of attachement
*
* @return attachment body part
*/
public MimeBodyPart getAttachmentBodyPart(int i) {
return (MimeBodyPart) attachments.get(i);
}
/**
* Adds a generic attachement.
*
* @param fileName file name of attachment
* @param dh DataHandler
*
* @exception MessagingException
*/
public void addAttachment(String fileName, DataHandler dh) throws MessagingException {
MimeBodyPart attBodyPart = new MimeBodyPart();
attBodyPart.setFileName(fileName);
attBodyPart.setDataHandler(dh);
attachments.add(attBodyPart);
}
/**
* Adds a HTML attachment.
*
* @param fileName attachment file name
* @param data HTML data
*
* @exception MessagingException
*/
public void addAttachment(String fileName, String data) throws MessagingException {
DataHandler dh = new DataHandler(new ByteArrayDataSource(data, "text/html"));
addAttachment(fileName, dh);
}
/**
* Adds an existing file as attachment.
*
* @param fileName local name of a file to attach
*
* @exception MessagingException
*/
public void addAttachment(String fileName) throws MessagingException {
FileDataSource fds = new FileDataSource(fileName);
addAttachment(fds.getName(), new DataHandler(fds));
}
// ---------------------------------------------------------------- headers
private HashMap headers = new HashMap();
/**
* Return all headers as a HashMap
*
* @return defined headers
*/
HashMap getHeaders() {
return headers;
}
/**
* Add single header.
*
* @param name header name
* @param value header value
*/
public void addHeader(String name, String value) {
headers.put(name, value);
}
/**
* Add map of headers.
*
* @param map headers map
*/
public void addHeaders(Map map) {
headers.putAll(map);
}
// ---------------------------------------------------------------- date
private Date sentDate = null;
/**
* Sets e-mails sent date. If input parameter is <code>null</code> then date
* will be when email is physically sent.
*
* @param date sent date value
*/
public void setSentDate(Date date) {
sentDate = date;
}
/**
* Sets current date as e-mails sent date.
*/
public void setSentDate() {
sentDate = new Date();
}
/**
* Returns e-mails sent date. If return value is <code>null</code> then date
* will be set during the process of sending.
*
* @return emails sent date or null if it will be set later.
*/
public Date getSentDate() {
return sentDate;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -