Class MimeMessageBuilder

java.lang.Object
ch.astorm.smtp4j.util.MimeMessageBuilder

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

    • MimeMessageBuilder

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

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

    • from

      public MimeMessageBuilder from(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(String sentDate) throws 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:
      ParseException
      jakarta.mail.MessagingException
    • at

      public MimeMessageBuilder at(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(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(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(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, 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(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(String subject, 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(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(String body, 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(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(String name, 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(String name, String mimeType, InputStream is) throws 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:
      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