Module bus.extra

Class SMTPMessage

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, saved

    Fields inherited from class jakarta.mail.Message

    expunged, folder, msgnum, session

    Fields inherited from interface jakarta.mail.Part

    ATTACHMENT, INLINE
  • Constructor Summary

    Constructors
    Constructor
    Description
    SMTPMessage(MailAccount mailAccount, jakarta.mail.Session session)
    Constructs a new SMTPMessage with the specified mail account and session.
  • Method Summary

    Modifier and Type
    Method
    Description
    addAttachments(jakarta.activation.DataSource... attachments)
    Adds attachments to the email, represented by DataSource objects.
    addBodyPart(jakarta.mail.BodyPart bodyPart)
    Adds a BodyPart to the email's multipart content.
    addBodyPart(jakarta.mail.BodyPart bodyPart, int index)
    Adds a BodyPart to the email's multipart content at a specific index.
    addFiles(File... files)
    Adds file attachments to the email.
    addImage(String cid, File imageFile)
    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.
    of(MailAccount mailAccount, boolean useGlobalSession, PrintStream debugOutput)
    Creates a new SMTPMessage instance.
    Sends the email.
    setBccs(String... bccs)
    Sets the blind carbon copy (BCC) recipient email addresses.
    setCcs(String... ccs)
    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).
    setReply(String... reply)
    Sets the 'Reply-To' email addresses.
    Sets the email subject.
    setTos(String... tos)
    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, writeTo

    Methods inherited from class jakarta.mail.Message

    addRecipient, getFolder, getMessageNumber, getSession, isExpunged, match, setExpunged, setFlag, setMessageNumber, setRecipient

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SMTPMessage

      public SMTPMessage(MailAccount mailAccount, jakarta.mail.Session session)
      Constructs a new SMTPMessage with the specified mail account and session.
      Parameters:
      mailAccount - The mail account configuration.
      session - The Jakarta Mail Session.
  • Method Details

    • of

      public static SMTPMessage of(MailAccount mailAccount, boolean useGlobalSession, PrintStream debugOutput)
      Creates a new SMTPMessage instance.
      Parameters:
      mailAccount - The mail account configuration.
      useGlobalSession - If true, uses a globally shared session; otherwise, creates a new session.
      debugOutput - The PrintStream for debug output. If null, no debug information is printed.
      Returns:
      A new SMTPMessage instance.
    • setTitle

      public SMTPMessage setTitle(String title)
      Sets the email subject.
      Parameters:
      title - The subject of the email.
      Returns:
      This SMTPMessage instance for method chaining.
    • setTos

      public SMTPMessage setTos(String... tos)
      Sets the recipient email addresses.
      Parameters:
      tos - An array of recipient email addresses.
      Returns:
      This SMTPMessage instance for method chaining.
    • setCcs

      public SMTPMessage setCcs(String... ccs)
      Sets the carbon copy (CC) recipient email addresses.
      Parameters:
      ccs - An array of CC recipient email addresses.
      Returns:
      This SMTPMessage instance for method chaining.
    • setBccs

      public SMTPMessage setBccs(String... bccs)
      Sets the blind carbon copy (BCC) recipient email addresses.
      Parameters:
      bccs - An array of BCC recipient email addresses.
      Returns:
      This SMTPMessage instance for method chaining.
    • setRecipients

      public SMTPMessage setRecipients(jakarta.mail.Message.RecipientType type, String... addresses)
      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 SMTPMessage instance for method chaining.
    • setReply

      public SMTPMessage setReply(String... reply)
      Sets the 'Reply-To' email addresses.
      Parameters:
      reply - An array of 'Reply-To' email addresses.
      Returns:
      This SMTPMessage instance for method chaining.
    • setContent

      public SMTPMessage setContent(String content, boolean isHtml)
      Sets the email body content.
      Parameters:
      content - The email body.
      isHtml - true if the content is HTML, false for plain text.
      Returns:
      This SMTPMessage instance for method chaining.
    • addImage

      public SMTPMessage addImage(String cid, InputStream imageStream)
      Adds an embedded image to the email.
      Parameters:
      cid - The Content-ID for embedding the image.
      imageStream - The InputStream of the image.
      Returns:
      This SMTPMessage instance for method chaining.
    • addImage

      public SMTPMessage addImage(String cid, File imageFile)
      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 SMTPMessage instance for method chaining.
    • addImage

      public SMTPMessage addImage(String cid, InputStream imageStream, String contentType)
      Adds an embedded image to the email with a specified content type.
      Parameters:
      cid - The Content-ID for embedding the image.
      imageStream - The InputStream of the image.
      contentType - The MIME type of the image.
      Returns:
      This SMTPMessage instance for method chaining.
    • addFiles

      public SMTPMessage addFiles(File... files)
      Adds file attachments to the email.
      Parameters:
      files - An array of files to be attached.
      Returns:
      This SMTPMessage instance for method chaining.
    • addAttachments

      public SMTPMessage addAttachments(jakarta.activation.DataSource... attachments)
      Adds attachments to the email, represented by DataSource objects.
      Parameters:
      attachments - An array of DataSource objects to attach.
      Returns:
      This SMTPMessage instance for method chaining.
    • addBodyPart

      public SMTPMessage addBodyPart(jakarta.mail.BodyPart bodyPart)
      Adds a BodyPart to the email's multipart content.
      Parameters:
      bodyPart - The BodyPart to add.
      Returns:
      This SMTPMessage instance for method chaining.
    • addBodyPart

      public SMTPMessage addBodyPart(jakarta.mail.BodyPart bodyPart, int index)
      Adds a BodyPart to the email's multipart content at a specific index.
      Parameters:
      bodyPart - The BodyPart to add.
      index - The index at which to insert the body part.
      Returns:
      This SMTPMessage instance for method chaining.
    • send

      public String send() throws org.miaixz.bus.core.lang.exception.InternalException
      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.