Class Mail

java.lang.Object
com.codename1.backend.mail.Mail

public final class Mail extends Object

One email: who it is for, what it says and what is attached.

mail.send(Mail.to("ada@example.com")
        .subject("Your order shipped")
        .template("mail/shipped", model)
        .attach("invoice.pdf", "application/pdf", pdf)
        .build());

This one class is what Spring spreads over SimpleMailMessage, MimeMessage and MimeMessageHelper. It is immutable; everything is checked in Mail.Builder.build(), so a mail that exists can be sent: an address that is not one, a header with a line break in it or an attachment with no name fails there, with an IllegalArgumentException, never at the server.

A mail with an HTML body and no text is given a text alternative derived from the HTML, which is what keeps it out of spam folders and readable in a client that shows text. The body is always UTF-8.

  • Method Details

    • to

      public static Mail.Builder to(String... addresses)
      Starts a mail to addresses, each user@example.com or Name <user@example.com>.
    • isAddress

      public static boolean isAddress(String address)
      Whether address is a bare user@example.com a mail can be sent to; answers without throwing.
    • builder

      public static Mail.Builder builder()
      Starts a mail with no recipient yet.
    • getFrom

      public String getFrom()
      The sender as it was given, or null when the mail takes the default.
    • getReplyTo

      public String getReplyTo()
      The reply address, or null.
    • getTo

      public List<String> getTo()
      The addresses of the recipients, without their display names.
    • getCc

      public List<String> getCc()
      The addresses copied.
    • getBcc

      public List<String> getBcc()
      The addresses copied without the other recipients being told.
    • getSubject

      public String getSubject()
      The subject, or null.
    • getText

      public String getText()
      The text body. For a mail built from a template this is null until the mail is sent; the mail a test captures has it.
    • getHtml

      public String getHtml()
      The HTML body, or null. For a mail built from a template this is null until the mail is sent; the mail a test captures has it.
    • getAttachmentNames

      public List<String> getAttachmentNames()
      The file names of the attachments, in order.
    • getAttachment

      public byte[] getAttachment(String name)
      The content of the attachment called name, or null.
    • getHeader

      public String getHeader(String name)
      The value of a header added with Mail.Builder.header(String, String), or null. The name is matched without regard to case.