java.lang.Object
jakarta.mail.Message
jakarta.mail.internet.MimeMessage
org.miaixz.bus.extra.mail.SMTPMessage
- All Implemented Interfaces:
jakarta.mail.internet.MimePart,jakarta.mail.Part
public class SMTPMessage
extends jakarta.mail.internet.MimeMessage
Represents an SMTP message, extending
MimeMessage to provide a fluent builder pattern for creating and
sending emails. This class simplifies the process of setting headers, content, and attachments.- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested classes/interfaces inherited from class jakarta.mail.internet.MimeMessage
jakarta.mail.internet.MimeMessage.RecipientType -
Field Summary
Fields inherited from class jakarta.mail.internet.MimeMessage
cachedContent, content, contentStream, dh, flags, headers, modified, savedFields inherited from class jakarta.mail.Message
expunged, folder, msgnum, sessionFields inherited from interface jakarta.mail.Part
ATTACHMENT, INLINE -
Constructor Summary
ConstructorsConstructorDescriptionSMTPMessage(MailAccount mailAccount, jakarta.mail.Session session) Constructs a newSMTPMessagewith the specified mail account and session. -
Method Summary
Modifier and TypeMethodDescriptionaddAttachments(jakarta.activation.DataSource... attachments) Adds attachments to the email, represented byDataSourceobjects.addBodyPart(jakarta.mail.BodyPart bodyPart) Adds aBodyPartto the email's multipart content.addBodyPart(jakarta.mail.BodyPart bodyPart, int index) Adds aBodyPartto the email's multipart content at a specific index.Adds file attachments to the email.Adds an embedded image from a file to the email.addImage(String cid, InputStream imageStream) Adds an embedded image to the email.addImage(String cid, InputStream imageStream, String contentType) Adds an embedded image to the email with a specified content type.static SMTPMessageof(MailAccount mailAccount, boolean useGlobalSession, PrintStream debugOutput) Creates a newSMTPMessageinstance.send()Sends the email.Sets the blind carbon copy (BCC) recipient email addresses.Sets the carbon copy (CC) recipient email addresses.setContent(String content, boolean isHtml) Sets the email body content.setRecipients(jakarta.mail.Message.RecipientType type, String... addresses) Sets the recipients for a given recipient type (TO, CC, BCC).Sets the 'Reply-To' email addresses.Sets the email subject.Sets the recipient email addresses.Methods inherited from class jakarta.mail.internet.MimeMessage
addFrom, addHeader, addHeaderLine, addRecipients, addRecipients, createInternetHeaders, createMimeMessage, getAllHeaderLines, getAllHeaders, getAllRecipients, getContent, getContentID, getContentLanguage, getContentMD5, getContentStream, getContentType, getDataHandler, getDescription, getDisposition, getEncoding, getFileName, getFlags, getFrom, getHeader, getHeader, getInputStream, getLineCount, getMatchingHeaderLines, getMatchingHeaders, getMessageID, getNonMatchingHeaderLines, getNonMatchingHeaders, getRawInputStream, getReceivedDate, getRecipients, getReplyTo, getSender, getSentDate, getSize, getSubject, isMimeType, isSet, parse, removeHeader, reply, reply, saveChanges, setContent, setContent, setContentID, setContentLanguage, setContentMD5, setDataHandler, setDescription, setDescription, setDisposition, setFileName, setFlags, setFrom, setFrom, setFrom, setHeader, setRecipients, setRecipients, setReplyTo, setSender, setSentDate, setSubject, setSubject, setText, setText, setText, updateHeaders, updateMessageID, writeTo, writeToMethods inherited from class jakarta.mail.Message
addRecipient, getFolder, getMessageNumber, getSession, isExpunged, match, setExpunged, setFlag, setMessageNumber, setRecipient
-
Constructor Details
-
SMTPMessage
Constructs a newSMTPMessagewith the specified mail account and session.- Parameters:
mailAccount- The mail account configuration.session- The Jakarta MailSession.
-
-
Method Details
-
of
public static SMTPMessage of(MailAccount mailAccount, boolean useGlobalSession, PrintStream debugOutput) Creates a newSMTPMessageinstance.- Parameters:
mailAccount- The mail account configuration.useGlobalSession- Iftrue, uses a globally shared session; otherwise, creates a new session.debugOutput- ThePrintStreamfor debug output. If null, no debug information is printed.- Returns:
- A new
SMTPMessageinstance.
-
setTitle
Sets the email subject.- Parameters:
title- The subject of the email.- Returns:
- This
SMTPMessageinstance for method chaining.
-
setTos
Sets the recipient email addresses.- Parameters:
tos- An array of recipient email addresses.- Returns:
- This
SMTPMessageinstance for method chaining.
-
setCcs
Sets the carbon copy (CC) recipient email addresses.- Parameters:
ccs- An array of CC recipient email addresses.- Returns:
- This
SMTPMessageinstance for method chaining.
-
setBccs
Sets the blind carbon copy (BCC) recipient email addresses.- Parameters:
bccs- An array of BCC recipient email addresses.- Returns:
- This
SMTPMessageinstance for method chaining.
-
setRecipients
Sets the recipients for a given recipient type (TO, CC, BCC).- Parameters:
type- The recipient type.addresses- An array of recipient email addresses.- Returns:
- This
SMTPMessageinstance for method chaining.
-
setReply
Sets the 'Reply-To' email addresses.- Parameters:
reply- An array of 'Reply-To' email addresses.- Returns:
- This
SMTPMessageinstance for method chaining.
-
setContent
Sets the email body content.- Parameters:
content- The email body.isHtml-trueif the content is HTML,falsefor plain text.- Returns:
- This
SMTPMessageinstance for method chaining.
-
addImage
Adds an embedded image to the email.- Parameters:
cid- The Content-ID for embedding the image.imageStream- TheInputStreamof the image.- Returns:
- This
SMTPMessageinstance for method chaining.
-
addImage
Adds an embedded image from a file to the email.- Parameters:
cid- The Content-ID for embedding the image.imageFile- The image file.- Returns:
- This
SMTPMessageinstance 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.contentType- The MIME type of the image.- Returns:
- This
SMTPMessageinstance for method chaining.
-
addFiles
Adds file attachments to the email.- Parameters:
files- An array of files to be attached.- Returns:
- This
SMTPMessageinstance for method chaining.
-
addAttachments
Adds attachments to the email, represented byDataSourceobjects.- Parameters:
attachments- An array ofDataSourceobjects to attach.- Returns:
- This
SMTPMessageinstance for method chaining.
-
addBodyPart
Adds aBodyPartto the email's multipart content.- Parameters:
bodyPart- TheBodyPartto add.- Returns:
- This
SMTPMessageinstance for method chaining.
-
addBodyPart
Adds aBodyPartto the email's multipart content at a specific index.- Parameters:
bodyPart- TheBodyPartto add.index- The index at which to insert the body part.- Returns:
- This
SMTPMessageinstance for method chaining.
-
send
Sends the email.- Returns:
- The unique message-id of the sent email.
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if sending the email fails.
-