Class MimeMessageBuilder


  • public class MimeMessageBuilder
    extends java.lang.Object
    Provides methods to easily create a MimeMessage that can be sent to an SMTP server.
    • Constructor Detail

      • MimeMessageBuilder

        public MimeMessageBuilder​(SmtpServer server)
        Creates a new MimeMessageBuilder by creating a new Session from the given server.
        Parameters:
        server - The server.
        See Also:
        SmtpServer.createSession()
      • MimeMessageBuilder

        public MimeMessageBuilder​(jakarta.mail.Session session)
        Creates a new MimeMessageBuilder with the given session.
        Parameters:
        session - The session.
    • Method Detail

      • from

        public MimeMessageBuilder from​(java.lang.String from)
                                throws jakarta.mail.MessagingException
        Defines the From address.
        Parameters:
        from - The from address.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • from

        public MimeMessageBuilder from​(jakarta.mail.Address from)
                                throws jakarta.mail.MessagingException
        Defines the From address.
        Parameters:
        from - The from address.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • at

        public MimeMessageBuilder at​(java.lang.String sentDate)
                              throws java.text.ParseException,
                                     jakarta.mail.MessagingException
        Defines the sent date.
        Parameters:
        sentDate - The sent date in format 'dd.MM.yyyy HH:mm:ss' (31.12.2020 23:59:59).
        Returns:
        This builder.
        Throws:
        java.text.ParseException
        jakarta.mail.MessagingException
      • at

        public MimeMessageBuilder at​(java.util.Date sentDate)
                              throws jakarta.mail.MessagingException
        Defines the sent date.
        Parameters:
        sentDate - The sent date.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • to

        public MimeMessageBuilder to​(java.lang.String... address)
                              throws jakarta.mail.MessagingException
        Adds the address to the TO recipients.
        Parameters:
        address - The address or a comma-separated list of addresses.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • to

        public MimeMessageBuilder to​(jakarta.mail.Address address)
                              throws jakarta.mail.MessagingException
        Adds the address to the TO recipients.
        Parameters:
        address - The address.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • cc

        public MimeMessageBuilder cc​(java.lang.String... address)
                              throws jakarta.mail.MessagingException
        Adds the address to the CC recipients.
        Parameters:
        address - The address or a comma-separated list of addresses.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • cc

        public MimeMessageBuilder cc​(jakarta.mail.Address address)
                              throws jakarta.mail.MessagingException
        Adds the address to the CC recipients.
        Parameters:
        address - The address.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • bcc

        public MimeMessageBuilder bcc​(java.lang.String... address)
                               throws jakarta.mail.MessagingException
        Adds the address to the BCC recipients.
        Parameters:
        address - The address or a comma-separated list of addresses.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • bcc

        public MimeMessageBuilder bcc​(jakarta.mail.Address address)
                               throws jakarta.mail.MessagingException
        Adds the address to the BCC recipients.
        Parameters:
        address - The address.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • toRecipient

        public MimeMessageBuilder toRecipient​(jakarta.mail.Message.RecipientType type,
                                              java.lang.String... address)
                                       throws jakarta.mail.MessagingException
        Adds the address to the specified recipient type.
        Parameters:
        type - The recipient type.
        address - The address or a comma-separated list of addresses.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • toRecipient

        public MimeMessageBuilder toRecipient​(jakarta.mail.Message.RecipientType type,
                                              jakarta.mail.Address address)
                                       throws jakarta.mail.MessagingException
        Adds the address to the specified recipient type.
        Parameters:
        type - The recipient type.
        address - The address.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • subject

        public MimeMessageBuilder subject​(java.lang.String subject)
                                   throws jakarta.mail.MessagingException
        Defines the message subject.
        Parameters:
        subject - The subject.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • subject

        public MimeMessageBuilder subject​(java.lang.String subject,
                                          java.nio.charset.Charset charset)
                                   throws jakarta.mail.MessagingException
        Defines the message subject.
        Parameters:
        subject - The subject.
        charset - The character set.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • body

        public MimeMessageBuilder body​(java.lang.String body)
                                throws jakarta.mail.MessagingException
        Defines the message body.
        Parameters:
        body - The body.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • body

        public MimeMessageBuilder body​(java.lang.String body,
                                       java.nio.charset.Charset charset)
                                throws jakarta.mail.MessagingException
        Defines the message body.
        Parameters:
        body - The body.
        charset - The character set.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • attachment

        public MimeMessageBuilder attachment​(java.io.File file)
                                      throws jakarta.mail.MessagingException
        Adds the specified file as attachment.
        Parameters:
        file - The file.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • attachment

        public MimeMessageBuilder attachment​(java.lang.String name,
                                             java.io.File file)
                                      throws jakarta.mail.MessagingException
        Adds the specified file as attachment wit the given name.
        Parameters:
        name - The name.
        file - The file.
        Returns:
        This builder.
        Throws:
        jakarta.mail.MessagingException
      • attachment

        public MimeMessageBuilder attachment​(java.lang.String name,
                                             java.lang.String mimeType,
                                             java.io.InputStream is)
                                      throws java.io.IOException,
                                             jakarta.mail.MessagingException
        Adds the specified file as attachment wit the given name and mimeType.
        Parameters:
        name - The name.
        mimeType - The MIME type.
        is - The input stream.
        Returns:
        This builder.
        Throws:
        java.io.IOException
        jakarta.mail.MessagingException
      • build

        public jakarta.mail.internet.MimeMessage build()
                                                throws jakarta.mail.MessagingException
        Builds the MimeMessage. This method can be called only once.
        Returns:
        The create MimeMessage.
        Throws:
        jakarta.mail.MessagingException
      • send

        public jakarta.mail.internet.MimeMessage send()
                                               throws jakarta.mail.MessagingException
        Builds and send the message. This method can be called only once.
        Returns:
        The sent message.
        Throws:
        jakarta.mail.MessagingException