Class GreenMailUtil

java.lang.Object
com.icegreen.greenmail.util.GreenMailUtil

public class GreenMailUtil extends Object
Since:
Jan 29, 2006
Version:
$Id: $
Author:
Wael Chatila
  • Method Details

    • copyStream

      public static void copyStream(InputStream src, OutputStream dest) throws IOException
      Writes the content of an input stream to an output stream
      Throws:
      IOException - on io error.
    • newMimeMessage

      public static jakarta.mail.internet.MimeMessage newMimeMessage(InputStream inputStream)
      Convenience method which creates a new MimeMessage from an input stream.
      Returns:
      the created mime message.
    • newMimeMessage

      public static jakarta.mail.internet.MimeMessage newMimeMessage(String mailString)
      Convenience method which creates a new MimeMessage from a string
    • hasNonTextAttachments

      public static boolean hasNonTextAttachments(jakarta.mail.Part m)
    • getLineCount

      public static int getLineCount(String str)
      Counts the number of lines.
      Parameters:
      str - the input string
      Returns:
      Returns the number of lines terminated by '\n' in string
    • getBody

      public static String getBody(jakarta.mail.Part msg)
      Returns:
      The content part of an email (or a Part)

      Note: You might have to use MimeUtility.decodeText(contentPart) on the result to decode the (possibly) quoted-printable encoded special characters.

    • getHeaders

      public static String getHeaders(jakarta.mail.Part msg)
      Returns:
      The headers of an email (or a Part)
    • getWholeMessage

      public static String getWholeMessage(jakarta.mail.Part msg)
      Returns:
      The both header and body for an email (or a Part)
    • getBodyAsBytes

      public static byte[] getBodyAsBytes(jakarta.mail.Part msg)
    • getHeaderAsBytes

      public static byte[] getHeaderAsBytes(jakarta.mail.Part part)
    • toString

      public static String toString(jakarta.mail.Part msg)
      Returns:
      same as getWholeMessage(jakarta.mail.Part)
    • random

      public static String random()
      Generates a random generated password consisting of letters and digits with a length variable between 5 and 8 characters long. Passwords are further optimized for displays that could potentially display the characters 1,l,I,0,O,Q in a way that a human could easily mix them up.
      Returns:
      the random string.
    • random

      public static String random(int nbrOfLetters)
    • sendTextEmailTest

      public static void sendTextEmailTest(String to, String from, String subject, String msg)
      Sends a text message using the default test setup for SMTP.
      Parameters:
      to - the to address.
      from - the from address.
      subject - the subject.
      msg - the text message.
      See Also:
    • sendTextEmailSecureTest

      public static void sendTextEmailSecureTest(String to, String from, String subject, String msg)
      Sends a text message using the default test setup for SMTPS.
      Parameters:
      to - the to address.
      from - the from address.
      subject - the subject.
      msg - the text message.
      See Also:
    • getAddressList

      public static String getAddressList(jakarta.mail.Address[] addresses)
    • createTextEmail

      public static jakarta.mail.internet.MimeMessage createTextEmail(String to, String from, String subject, String msg, ServerSetup setup)
    • sendTextEmail

      public static void sendTextEmail(String to, String from, String subject, String msg, ServerSetup setup)
      Sends a text message using given server setup for SMTP.
      Parameters:
      to - the to address.
      from - the from address.
      subject - the subject.
      msg - the test message.
      setup - the SMTP setup.
    • sendMimeMessage

      public static void sendMimeMessage(jakarta.mail.internet.MimeMessage mimeMessage)
      Send the message using the JavaMail session defined in the message
      Parameters:
      mimeMessage - Message to send
    • sendMimeMessage

      public static void sendMimeMessage(jakarta.mail.internet.MimeMessage mimeMessage, String username, String password)
      Send the message using the JavaMail session defined in the message
      Parameters:
      mimeMessage - Message to send
      username - Username for authentication.
      password - Password for authentication.
    • sendMessageBody

      public static void sendMessageBody(String to, String from, String subject, Object body, String contentType, ServerSetup serverSetup)
      Send the message with the given attributes and the given body using the specified SMTP settings
      Parameters:
      to - Destination address(es)
      from - Sender address
      subject - Message subject
      body - Message content. May either be a MimeMultipart or another body that java mail recognizes
      contentType - MIME content type of body
      serverSetup - Server settings to use for connecting to the SMTP server
    • sendAttachmentEmail

      public static void sendAttachmentEmail(String to, String from, String subject, String msg, byte[] attachment, String contentType, String filename, String description, ServerSetup setup)
    • createMultipartWithAttachment

      public static jakarta.mail.internet.MimeMultipart createMultipartWithAttachment(String msg, byte[] attachment, String contentType, String filename, String description)
      Create new multipart with a text part and an attachment
      Parameters:
      msg - Message text
      attachment - Attachment data
      contentType - MIME content type of body
      filename - File name of the attachment
      description - Description of the attachment
      Returns:
      New multipart
    • getSession

      public static jakarta.mail.Session getSession(ServerSetup setup)
      Gets a JavaMail Session for given server type such as IMAP and additional props for JavaMail.
      Parameters:
      setup - the setup type, such as ServerSetup.IMAP
      Returns:
      the JavaMail session.
    • getSession

      public static jakarta.mail.Session getSession(ServerSetup setup, Properties mailProps)
      Gets a JavaMail Session for given server type such as IMAP and additional props for JavaMail.
      Parameters:
      setup - the setup type, such as ServerSetup.IMAP
      mailProps - additional mail properties.
      Returns:
      the JavaMail session.
    • getSession

      public static jakarta.mail.Session getSession(ServerSetup setup, Properties mailProps, boolean debug)
      Gets a JavaMail Session for given server type such as IMAP and additional props for JavaMail.
      Parameters:
      setup - the setup type, such as ServerSetup.IMAP
      mailProps - additional mail properties.
      debug - sets JavaMail debug properties.
      Returns:
      the JavaMail session.
    • setQuota

      public static void setQuota(GreenMailUser user, jakarta.mail.Quota quota)
      Sets a quota for a users.
      Parameters:
      user - the user.
      quota - the quota.
    • getQuota

      public static jakarta.mail.Quota[] getQuota(GreenMailUser user, String quotaRoot)
      Gets the quotas for the user.
      Parameters:
      user - the user.
      quotaRoot - the quota root, eg 'INBOX'.
      Returns:
      array of current quotas, or an empty array if not set.