Class HtmlEmail
- java.lang.Object
-
- org.apache.commons.mail.Email
-
- org.apache.commons.mail.MultiPartEmail
-
- org.apache.commons.mail.HtmlEmail
-
- Direct Known Subclasses:
ImageHtmlEmail
public class HtmlEmail extends MultiPartEmail
An HTML multipart email.This class is used to send HTML formatted email. A text message can also be set for HTML unaware email clients, such as text-based email clients.
This class also inherits from
MultiPartEmail, so it is easy to add attachments to the email.To send an email in HTML, one should create a
HtmlEmail, then use theEmail.setFrom(String),Email.addTo(String)etc. methods. The HTML content can be set with thesetHtmlMsg(String)method. The alternative text content can be set withsetTextMsg(String).Either the text or HTML can be omitted, in which case the "main" part of the multipart becomes whichever is supplied rather than a
multipart/alternative.Embedding Images and Media
It is also possible to embed URLs, files, or arbitrary
DataSources directly into the body of the mail:HtmlEmail he = new HtmlEmail(); File img = new File("my/image.gif"); PNGDataSource png = new PNGDataSource(decodedPNGOutputStream); // a custom class StringBuffer msg = new StringBuffer(); msg.append("<html><body>"); msg.append("<img src=cid:").append(he.embed(img)).append(">"); msg.append("<img src=cid:").append(he.embed(png)).append(">"); msg.append("</body></html>"); he.setHtmlMsg(msg.toString()); // code to set the other email fields (not shown)Embedded entities are tracked by their name, which for
Files is the filename itself and forURLs is the canonical path. It is an error to bind the same name to more than one entity, and this class will attempt to validate that forFiles andURLs. When embedding aDataSource, the code uses theequals()method defined on theDataSources to make the determination.- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description static intCID_LENGTHDefinition of the length of generated CID's.-
Fields inherited from class org.apache.commons.mail.Email
ATTACHMENTS, CONTENT_TYPE, EMAIL_BODY, EMAIL_SUBJECT, FILE_SERVER, ISO_8859_1, KOI8_R, MAIL_DEBUG, MAIL_HOST, MAIL_PORT, MAIL_SMTP_AUTH, MAIL_SMTP_CONNECTIONTIMEOUT, MAIL_SMTP_FROM, MAIL_SMTP_PASSWORD, MAIL_SMTP_SOCKET_FACTORY_CLASS, MAIL_SMTP_SOCKET_FACTORY_FALLBACK, MAIL_SMTP_SOCKET_FACTORY_PORT, MAIL_SMTP_TIMEOUT, MAIL_SMTP_USER, MAIL_TRANSPORT_PROTOCOL, MAIL_TRANSPORT_TLS, RECEIVER_EMAIL, RECEIVER_NAME, SENDER_EMAIL, SENDER_NAME, SMTP, TEXT_HTML, TEXT_PLAIN, US_ASCII
-
-
Constructor Summary
Constructors Constructor Description HtmlEmail()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbuildMimeMessage()Does the work of actually building the MimeMessage.java.lang.Stringembed(java.io.File file)Embeds a file in the HTML.java.lang.Stringembed(java.io.File file, java.lang.String cid)Embeds a file in the HTML.java.lang.Stringembed(java.lang.String urlString, java.lang.String name)Attempts to parse the specifiedStringas a URL that will then be embedded in the message.java.lang.Stringembed(java.net.URL url, java.lang.String name)Embeds an URL in the HTML.java.lang.Stringembed(javax.activation.DataSource dataSource, java.lang.String name)Embeds the specifiedDataSourcein the HTML using a randomly generated Content-ID.java.lang.Stringembed(javax.activation.DataSource dataSource, java.lang.String name, java.lang.String cid)Embeds the specifiedDataSourcein the HTML using the specified Content-ID.HtmlEmailsetHtmlMsg(java.lang.String aHtml)Set the HTML content.EmailsetMsg(java.lang.String msg)Set the message.HtmlEmailsetTextMsg(java.lang.String aText)Set the text content.-
Methods inherited from class org.apache.commons.mail.MultiPartEmail
addPart, addPart, addPart, attach, attach, attach, attach, attach, attach, getSubType, isBoolHasAttachments, setBoolHasAttachments, setSubType
-
Methods inherited from class org.apache.commons.mail.Email
addBcc, addBcc, addBcc, addBcc, addCc, addCc, addCc, addCc, addHeader, addReplyTo, addReplyTo, addReplyTo, addTo, addTo, addTo, addTo, getBccAddresses, getBounceAddress, getCcAddresses, getFromAddress, getHeader, getHeaders, getHostName, getMailSession, getMimeMessage, getReplyToAddresses, getSentDate, getSmtpPort, getSocketConnectionTimeout, getSocketTimeout, getSslSmtpPort, getSubject, getToAddresses, isSendPartial, isSSL, isSSLCheckServerIdentity, isSSLOnConnect, isStartTLSEnabled, isStartTLSRequired, isTLS, send, sendMimeMessage, setAuthentication, setAuthenticator, setBcc, setBounceAddress, setCc, setCharset, setContent, setContent, setDebug, setFrom, setFrom, setFrom, setHeaders, setHostName, setMailSession, setMailSessionFromJNDI, setPopBeforeSmtp, setReplyTo, setSendPartial, setSentDate, setSmtpPort, setSocketConnectionTimeout, setSocketTimeout, setSSL, setSSLCheckServerIdentity, setSSLOnConnect, setSslSmtpPort, setStartTLSEnabled, setStartTLSRequired, setSubject, setTLS, setTo, updateContentType
-
-
-
-
Field Detail
-
CID_LENGTH
public static final int CID_LENGTH
Definition of the length of generated CID's.- See Also:
- Constant Field Values
-
-
Method Detail
-
setTextMsg
public HtmlEmail setTextMsg(java.lang.String aText) throws EmailException
Set the text content.- Parameters:
aText- A String.- Returns:
- An HtmlEmail.
- Throws:
EmailException- see javax.mail.internet.MimeBodyPart for definitions- Since:
- 1.0
-
setHtmlMsg
public HtmlEmail setHtmlMsg(java.lang.String aHtml) throws EmailException
Set the HTML content.- Parameters:
aHtml- A String.- Returns:
- An HtmlEmail.
- Throws:
EmailException- see javax.mail.internet.MimeBodyPart for definitions- Since:
- 1.0
-
setMsg
public Email setMsg(java.lang.String msg) throws EmailException
Set the message.This method overrides
MultiPartEmail.setMsg(String)in order to send an HTML message instead of a plain text message in the mail body. The message is formatted in HTML for the HTML part of the message; it is left as is in the alternate text part.- Overrides:
setMsgin classMultiPartEmail- Parameters:
msg- the message text to use- Returns:
- this
HtmlEmail - Throws:
EmailException- if msg is null or empty; see javax.mail.internet.MimeBodyPart for definitions- Since:
- 1.0
-
embed
public java.lang.String embed(java.lang.String urlString, java.lang.String name) throws EmailExceptionAttempts to parse the specifiedStringas a URL that will then be embedded in the message.- Parameters:
urlString- String representation of the URL.name- The name that will be set in the filename header field.- Returns:
- A String with the Content-ID of the URL.
- Throws:
EmailException- when URL supplied is invalid or ifnameis null or empty; also seeMimeBodyPartfor definitions- Since:
- 1.1
- See Also:
embed(URL, String)
-
embed
public java.lang.String embed(java.net.URL url, java.lang.String name) throws EmailExceptionEmbeds an URL in the HTML.This method embeds a file located by an URL into the mail body. It allows, for instance, to add inline images to the email. Inline files may be referenced with a
cid:xxxxxxURL, where xxxxxx is the Content-ID returned by the embed function. It is an error to bind the same name to more than one URL; if the same URL is embedded multiple times, the same Content-ID is guaranteed to be returned.While functionally the same as passing
URLDataSourcetoembed(DataSource, String, String), this method attempts to validate the URL before embedding it in the message and will throwEmailExceptionif the validation fails. In this case, theHtmlEmailobject will not be changed.NOTE: Clients should take care to ensure that different URLs are bound to different names. This implementation tries to detect this and throw
EmailException. However, it is not guaranteed to catch all cases, especially when the URL refers to a remote HTTP host that may be part of a virtual host cluster.- Parameters:
url- The URL of the file.name- The name that will be set in the filename header field.- Returns:
- A String with the Content-ID of the file.
- Throws:
EmailException- when URL supplied is invalid or ifnameis null or empty; also seeMimeBodyPartfor definitions- Since:
- 1.0
-
embed
public java.lang.String embed(java.io.File file) throws EmailExceptionEmbeds a file in the HTML. This implementation delegates toembed(File, String).- Parameters:
file- TheFileobject to embed- Returns:
- A String with the Content-ID of the file.
- Throws:
EmailException- when the suppliedFilecannot be used; also seeMimeBodyPartfor definitions- Since:
- 1.1
- See Also:
embed(File, String)
-
embed
public java.lang.String embed(java.io.File file, java.lang.String cid) throws EmailExceptionEmbeds a file in the HTML.This method embeds a file located by an URL into the mail body. It allows, for instance, to add inline images to the email. Inline files may be referenced with a
cid:xxxxxxURL, where xxxxxx is the Content-ID returned by the embed function. Files are bound to their names, which is the value returned byFile.getName(). If the same file is embedded multiple times, the same CID is guaranteed to be returned.While functionally the same as passing
FileDataSourcetoembed(DataSource, String, String), this method attempts to validate the file before embedding it in the message and will throwEmailExceptionif the validation fails. In this case, theHtmlEmailobject will not be changed.- Parameters:
file- TheFileto embedcid- the Content-ID to use for the embeddedFile- Returns:
- A String with the Content-ID of the file.
- Throws:
EmailException- when the suppliedFilecannot be used or if the file has already been embedded; also seeMimeBodyPartfor definitions- Since:
- 1.1
-
embed
public java.lang.String embed(javax.activation.DataSource dataSource, java.lang.String name) throws EmailExceptionEmbeds the specifiedDataSourcein the HTML using a randomly generated Content-ID. Returns the generated Content-ID string.- Parameters:
dataSource- theDataSourceto embedname- the name that will be set in the filename header field- Returns:
- the generated Content-ID for this
DataSource - Throws:
EmailException- if the embedding fails or ifnameis null or empty- Since:
- 1.1
- See Also:
embed(DataSource, String, String)
-
embed
public java.lang.String embed(javax.activation.DataSource dataSource, java.lang.String name, java.lang.String cid) throws EmailExceptionEmbeds the specifiedDataSourcein the HTML using the specified Content-ID. Returns the specified Content-ID string.- Parameters:
dataSource- theDataSourceto embedname- the name that will be set in the filename header fieldcid- the Content-ID to use for thisDataSource- Returns:
- the URL encoded Content-ID for this
DataSource - Throws:
EmailException- if the embedding fails or ifnameis null or empty- Since:
- 1.1
-
buildMimeMessage
public void buildMimeMessage() throws EmailExceptionDoes the work of actually building the MimeMessage. Please note that a user rarely calls this method directly and only if he/she is interested in the sending the underlying MimeMessage without commons-email.- Overrides:
buildMimeMessagein classMultiPartEmail- Throws:
EmailException- if there was an error.- Since:
- 1.0
-
-