Package com.icegreen.greenmail.util
Class GreenMail
- java.lang.Object
-
- com.icegreen.greenmail.configuration.ConfiguredGreenMail
-
- com.icegreen.greenmail.util.GreenMail
-
- All Implemented Interfaces:
GreenMailOperations
public class GreenMail extends ConfiguredGreenMail
Utility class that manages a greenmail server with support for multiple protocols
-
-
Field Summary
Fields Modifier and Type Field Description protected ServerSetup[]configprotected org.slf4j.Loggerlogprotected Managersmanagersprotected Map<String,AbstractServer>services
-
Constructor Summary
Constructors Constructor Description GreenMail()Creates a SMTP, SMTPS, POP3, POP3S, IMAP, and IMAPS server binding onto non-default ports.GreenMail(ServerSetup config)Call this constructor if you want to run one of the email servers onlyGreenMail(ServerSetup[] config)Call this constructor if you want to run more than one of the email servers
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Map<String,AbstractServer>createServices(ServerSetup[] config, Managers mgr)Create the required services according to the server setupImapServergetImap()ImapServergetImaps()ManagersgetManagers()Pop3ServergetPop3()Pop3ServergetPop3s()jakarta.mail.internet.MimeMessage[]getReceivedMessages()jakarta.mail.internet.MimeMessage[]getReceivedMessagesForDomain(String domain)Gets all messages containing given domain.SmtpServergetSmtp()SmtpServergetSmtps()UserManagergetUserManager()booleanisRunning()Checks if GreenMail is up and running.voidpurgeEmailFromAllMailboxes()Remove/purge all data from all mail stores (POP3/IMAP)voidreset()Restart the GreenMail server.voidsetQuotaSupported(boolean isEnabled)Toggles the IMAP quota support.GreenMailUsersetUser(String login, String password)Sets the password for the account linked to email.GreenMailUsersetUser(String email, String login, String password)Sets the password for the account linked to email.voidsetUsers(Properties users)Sets up accounts with password based on a properties map where the key is the email/login and the value the passwordvoidstart()Start the GreenMail servervoidstop()Stop the GreenMail server.booleanwaitForIncomingEmail(int emailCount)Does the same thing asGreenMailOperations.waitForIncomingEmail(long, int)but with a default timeout of 5000msbooleanwaitForIncomingEmail(long timeout, int emailCount)Use this method if you are sending email in a different thread from the one you're testing from.GreenMailwithConfiguration(GreenMailConfiguration config)Configure GreenMail instance using the given configuration-
Methods inherited from class com.icegreen.greenmail.configuration.ConfiguredGreenMail
doConfigure
-
-
-
-
Field Detail
-
log
protected final org.slf4j.Logger log
-
managers
protected Managers managers
-
services
protected final Map<String,AbstractServer> services
-
config
protected ServerSetup[] config
-
-
Constructor Detail
-
GreenMail
public GreenMail()
Creates a SMTP, SMTPS, POP3, POP3S, IMAP, and IMAPS server binding onto non-default ports. The ports numbers are defined inServerSetupTest
-
GreenMail
public GreenMail(ServerSetup config)
Call this constructor if you want to run one of the email servers only- Parameters:
config- Server setup to use
-
GreenMail
public GreenMail(ServerSetup[] config)
Call this constructor if you want to run more than one of the email servers- Parameters:
config- Server setup to use
-
-
Method Detail
-
start
public void start()
Description copied from interface:GreenMailOperationsStart the GreenMail server
-
stop
public void stop()
Description copied from interface:GreenMailOperationsStop the GreenMail server. Clear all data (send messages, users, ...).
-
reset
public void reset()
Description copied from interface:GreenMailOperationsRestart the GreenMail server. Clear all data (send messages, users, ...)
-
createServices
protected Map<String,AbstractServer> createServices(ServerSetup[] config, Managers mgr)
Create the required services according to the server setup- Parameters:
config- Service configuration- Returns:
- Services map
-
getSmtp
public SmtpServer getSmtp()
- Returns:
- SMTP server operated by this GreenMail instance or null if there is none
-
getImap
public ImapServer getImap()
- Returns:
- SMTP server operated by this GreenMail instance or null if there is none
-
getPop3
public Pop3Server getPop3()
- Returns:
- SMTP server operated by this GreenMail instance or null if there is none
-
getSmtps
public SmtpServer getSmtps()
- Returns:
- SMTP server operated by this GreenMail instance or null if there is none
-
getImaps
public ImapServer getImaps()
- Returns:
- SMTP server operated by this GreenMail instance or null if there is none
-
getPop3s
public Pop3Server getPop3s()
- Returns:
- SMTP server operated by this GreenMail instance or null if there is none
-
getManagers
public Managers getManagers()
- Returns:
- Greenmail protocol managers
-
getUserManager
public UserManager getUserManager()
- Returns:
- the user manager for
-
waitForIncomingEmail
public boolean waitForIncomingEmail(long timeout, int emailCount)Description copied from interface:GreenMailOperationsUse this method if you are sending email in a different thread from the one you're testing from. Block waits for an email to arrive in any mailbox for any user. Implementation Detail: No polling wait implementation- Parameters:
timeout- maximum time in ms to wait for emailCount of messages to arrive before giving up and returning falseemailCount- waits for these many emails to arrive before returning- Returns:
- Returns false if timeout period was reached, otherwise true.
-
waitForIncomingEmail
public boolean waitForIncomingEmail(int emailCount)
Description copied from interface:GreenMailOperationsDoes the same thing asGreenMailOperations.waitForIncomingEmail(long, int)but with a default timeout of 5000ms- Parameters:
emailCount- waits for these many emails to arrive before returning- Returns:
- Returns false if timeout period was reached, otherwise true.
-
getReceivedMessages
public jakarta.mail.internet.MimeMessage[] getReceivedMessages()
- Returns:
- Returns all messages in all folders for all users
GreenMailUtilhas a bunch of static helper methods to extract body text etc.
-
getReceivedMessagesForDomain
public jakarta.mail.internet.MimeMessage[] getReceivedMessagesForDomain(String domain)
Description copied from interface:GreenMailOperationsGets all messages containing given domain.- Parameters:
domain- the domain, such as 'icegreen.com' or 'some.example.com'- Returns:
- Returns all received messages for given domain.
-
setUser
public GreenMailUser setUser(String login, String password)
Description copied from interface:GreenMailOperationsSets the password for the account linked to email. If no account exits, one is automatically created when an email is received The automatically created account has the account login and password equal to the email address.- Parameters:
login- Login for which the password should be set. This is assumed to be the same as the email address.password- New password- Returns:
- the user created
-
setUser
public GreenMailUser setUser(String email, String login, String password)
Description copied from interface:GreenMailOperationsSets the password for the account linked to email. If no account exits, one is automatically created when an email is received The automatically created account has the account login and password equal to the email address.- Parameters:
email- Email address for which the password should be setlogin- Login name for login. This may be different to the email address. E.g. the email address could be "info@localhost", the login could be "info".password- New password- Returns:
- the user created
-
setQuotaSupported
public void setQuotaSupported(boolean isEnabled)
Description copied from interface:GreenMailOperationsToggles the IMAP quota support. Quotas are enabled by default.- Parameters:
isEnabled- true, if quotas should be supported.
-
setUsers
public void setUsers(Properties users)
Description copied from interface:GreenMailOperationsSets up accounts with password based on a properties map where the key is the email/login and the value the password- Parameters:
users- User/password map
-
withConfiguration
public GreenMail withConfiguration(GreenMailConfiguration config)
Description copied from interface:GreenMailOperationsConfigure GreenMail instance using the given configuration- Specified by:
withConfigurationin interfaceGreenMailOperations- Overrides:
withConfigurationin classConfiguredGreenMail- Parameters:
config- Configuration to use- Returns:
- self (for method chaining)
-
purgeEmailFromAllMailboxes
public void purgeEmailFromAllMailboxes() throws FolderExceptionDescription copied from interface:GreenMailOperationsRemove/purge all data from all mail stores (POP3/IMAP)- Throws:
FolderException- on error
-
isRunning
public boolean isRunning()
Description copied from interface:GreenMailOperationsChecks if GreenMail is up and running.- Returns:
- true if ready to serve.
-
-