java.lang.Object
org.miaixz.bus.extra.mail.Mail
- All Implemented Interfaces:
Serializable,org.miaixz.bus.core.Builder<jakarta.mail.internet.MimeMessage>
public class Mail
extends Object
implements org.miaixz.bus.core.Builder<jakarta.mail.internet.MimeMessage>
Represents a mail client for sending emails, designed with a fluent builder pattern. It simplifies the process of
setting recipients, subject, content, and attachments. Supports both HTML and plain text content, and allows for file
and image attachments.
Usage Example:
// Create a mail client
Mail mail = Mail.of();
// Set basic email information
mail.setTos("recipient@example.com").setTitle("Email Subject").setContent("<h1>Hello!</h1>", true) // true indicates
// HTML format
.addFiles(new File("attachment.pdf"));
// Send the email
String messageId = mail.send();
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMail()Constructs a newMailinstance using the global mail account.Mail(MailAccount mailAccount) Constructs a newMailinstance with the specified mail account. -
Method Summary
Modifier and TypeMethodDescriptionaddAttachments(jakarta.activation.DataSource... attachments) Adds attachments to the email, represented byDataSourceobjects.Adds file attachments to the email.Adds an embedded image from aFileto the email.addImage(String cid, InputStream imageStream) Adds an embedded image to the email, referenced by a CID in the HTML body.addImage(String cid, InputStream imageStream, String contentType) Adds an embedded image to the email with a specified content type.build()Builds theMimeMessageobject from the current configuration.static Mailof()Creates a newMailinstance using the global mail account configuration.static Mailof(MailAccount mailAccount) Creates a newMailinstance with the specified mail account.send()Builds and sends the email.Sets multiple blind carbon copy (BCC) recipient email addresses, overwriting any previously set BCC recipients.Sets multiple carbon copy (CC) recipient email addresses, overwriting any previously set CC recipients.setCharset(Charset charset) Sets the character set encoding for the email.setContent(String content) Sets the body content of the email.setContent(String content, boolean isHtml) Sets both the body content and the content type (HTML or plain text).setDebugOutput(PrintStream debugOutput) Sets the debug output stream for logging mail sending details.setHtml(boolean isHtml) Sets whether the email content is in HTML format.Sets multiple 'Reply-To' email addresses, overwriting any previously set reply-to addresses.Sets the subject of the email.Sets multiple recipient email addresses, overwriting any previously set recipients.setUseGlobalSession(boolean isUseGlobalSession) Sets whether to use a global session for sending mail.Sets the recipient email addresses.
-
Constructor Details
-
Mail
public Mail()Constructs a newMailinstance using the global mail account. -
Mail
Constructs a newMailinstance with the specified mail account. If the provided account is null, the global mail account configuration is used.- Parameters:
mailAccount- The mail account to use. If null, the global account is used.
-
-
Method Details
-
of
Creates a newMailinstance with the specified mail account.- Parameters:
mailAccount- The mail account configuration.- Returns:
- A new
Mailinstance.
-
of
Creates a newMailinstance using the global mail account configuration.- Returns:
- A new
Mailinstance.
-
to
Sets the recipient email addresses.- Parameters:
tos- The recipient email addresses.- Returns:
- This
Mailinstance for method chaining. - See Also:
-
setTos
Sets multiple recipient email addresses, overwriting any previously set recipients.- Parameters:
tos- An array of recipient email addresses.- Returns:
- This
Mailinstance for method chaining.
-
setCcs
Sets multiple carbon copy (CC) recipient email addresses, overwriting any previously set CC recipients.- Parameters:
ccs- An array of CC recipient email addresses.- Returns:
- This
Mailinstance for method chaining.
-
setBccs
Sets multiple blind carbon copy (BCC) recipient email addresses, overwriting any previously set BCC recipients.- Parameters:
bccs- An array of BCC recipient email addresses.- Returns:
- This
Mailinstance for method chaining.
-
setReply
Sets multiple 'Reply-To' email addresses, overwriting any previously set reply-to addresses.- Parameters:
reply- An array of 'Reply-To' email addresses.- Returns:
- This
Mailinstance for method chaining.
-
setTitle
Sets the subject of the email.- Parameters:
title- The subject of the email.- Returns:
- This
Mailinstance for method chaining.
-
setContent
Sets the body content of the email. The content can be plain text or HTML. UsesetHtml(boolean)to specify the content type.- Parameters:
content- The body content of the email.- Returns:
- This
Mailinstance for method chaining.
-
setHtml
Sets whether the email content is in HTML format.- Parameters:
isHtml-trueif the content is HTML,falsefor plain text.- Returns:
- This
Mailinstance for method chaining.
-
setContent
Sets both the body content and the content type (HTML or plain text).- Parameters:
content- The body content of the email.isHtml-trueif the content is HTML,falsefor plain text.- Returns:
- This
Mailinstance for method chaining.
-
addFiles
Adds file attachments to the email. If a file is an image, a CID (Content-ID) is automatically generated for embedding in the email body, using the filename as the default CID.- Parameters:
files- An array ofFileobjects to attach.- Returns:
- This
Mailinstance for method chaining.
-
addImage
Adds an embedded image to the email, referenced by a CID in the HTML body. The image content type defaults to "image/jpeg".- Parameters:
cid- The Content-ID for embedding the image (e.g., "my-image").imageStream- TheInputStreamof the image. This stream is not closed by this method.- Returns:
- This
Mailinstance for method chaining.
-
addImage
Adds an embedded image to the email with a specified content type.- Parameters:
cid- The Content-ID for embedding the image.imageStream- TheInputStreamof the image. This stream is not closed by this method.contentType- The MIME type of the image (e.g., "image/png"). Defaults to "image/jpeg" if null.- Returns:
- This
Mailinstance for method chaining.
-
addImage
Adds an embedded image from aFileto the email.- Parameters:
cid- The Content-ID for embedding the image.imageFile- The imageFile.- Returns:
- This
Mailinstance for method chaining.
-
addAttachments
Adds attachments to the email, represented byDataSourceobjects.- Parameters:
attachments- An array ofDataSourceobjects to attach.- Returns:
- This
Mailinstance for method chaining.
-
setCharset
Sets the character set encoding for the email.- Parameters:
charset- The character set (e.g., UTF-8, GBK).- Returns:
- This
Mailinstance for method chaining. - See Also:
-
setUseGlobalSession
Sets whether to use a global session for sending mail. Defaults to false.- Parameters:
isUseGlobalSession-trueto use a global session,falseto create a new session for each email.- Returns:
- This
Mailinstance for method chaining.
-
setDebugOutput
Sets the debug output stream for logging mail sending details.- Parameters:
debugOutput- ThePrintStreamfor debug output. If null, system default is used.- Returns:
- This
Mailinstance for method chaining.
-
build
Builds theMimeMessageobject from the current configuration. This method consolidates all settings into a complete mail message object ready for sending.- Specified by:
buildin interfaceorg.miaixz.bus.core.Builder<jakarta.mail.internet.MimeMessage>- Returns:
- The constructed
SMTPMessageobject.
-
send
Builds and sends the email.- Returns:
- The unique message-id of the sent email, which can be used for tracking.
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if sending the email fails.
-