Package com.atlassian.confluence.it.mail
Class MailServices
- java.lang.Object
-
- com.atlassian.confluence.it.mail.MailServices
-
public class MailServices extends Object
This class provides mail services like POP3 and SMTP for the acceptance tests. It will try to find free TCP/IP ports for the services within a given range.
-
-
Constructor Summary
Constructors Constructor Description MailServices(int portRangeStart, int portRangeEnd, String hostName)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddUser(MailUser user)voidaddUser(User user)Adds a user to the mail service.voidaddUser(String email, String login, String password)Adds a user to the mail service.StringgetHostName()intgetPop3Port()intgetPort(String protocol)javax.mail.internet.MimeMessagegetReceivedMessage()Get the first message of all messages received until now.javax.mail.internet.MimeMessage[]getReceivedMessages()Returns an array of all messages received until now.intgetSmtpPort()booleanisRunning()Returns true if the mail servers are running.voidremoveAllReceivedMessages()Removes all received mails.voidsendMimeMessage(InputStream messageStream)Sends an email based on a MimeMessage input stream, most likely read from a file.voidsendTextMessage(String to, String from, String subject, String body)Sends an email with the content type "text/plain" and the given contents using the test SMTP server.voidstart()Tries to start a SMTP and a POP3 server on any free port in the range given in the constructor.voidstop()Stops the SMTP and POP3 server.javax.mail.internet.MimeMessagewaitAndGetReceivedMessage()Does the same asgetReceivedMessage()but waits until a message is received or until a timeout is reached.javax.mail.internet.MimeMessage[]waitAndGetReceivedMessages(int numMessages)Does the same asgetReceivedMessages()but waits until a message is received or until a timeout is reached.javax.mail.internet.MimeMessage[]waitAndGetReceivedMessages(long timeout, int numMessages)Does the same asgetReceivedMessages()but waits until a message is received or until specified timeout is reached.booleanwaitForIncomingMessage(int numberOfMessages)Wait until the specified number of mails is received or until a timeout is reached.booleanwaitForIncomingMessage(Long timeout, int numberOfMessages)Wait until the specified number of mails is received or until specified timeout is reached.
-
-
-
Constructor Detail
-
MailServices
public MailServices(int portRangeStart, int portRangeEnd, String hostName)
-
-
Method Detail
-
start
public void start() throws ExceptionTries to start a SMTP and a POP3 server on any free port in the range given in the constructor.- Throws:
BindException- if there is no free port available in the given rangeException
-
stop
public void stop()
Stops the SMTP and POP3 server.
-
isRunning
public boolean isRunning()
Returns true if the mail servers are running.
-
addUser
public void addUser(String email, String login, String password)
Adds a user to the mail service. The SMTP server will then accept incoming mails for email and you can fetch these mails via POP3 using login/password for authentication.- Parameters:
email- email of the user to be createdlogin- login of the user to be createdpassword- password of the user to be created
-
addUser
public void addUser(User user)
Adds a user to the mail service. The SMTP server will then accept incoming mails for email and you can fetch these mails via POP3 using login/password for authentication.- Parameters:
user- the user to be created - only the email and password fields are needed
-
addUser
public void addUser(MailUser user)
-
getHostName
public String getHostName()
- Returns:
- the host name the mail services are bound to
-
getPop3Port
public int getPop3Port()
- Returns:
- the port the POP3 server is bound to
-
getSmtpPort
public int getSmtpPort()
- Returns:
- the port the SMTP server is bound to
-
getPort
public int getPort(String protocol)
- Parameters:
protocol- the name of the protocol you want the port number (like "pop3", "pop3s", "smtp", "smtps", "imap", "imaps"...)- Returns:
- the port number, -1 if protocol is unknown
-
sendTextMessage
public void sendTextMessage(String to, String from, String subject, String body)
Sends an email with the content type "text/plain" and the given contents using the test SMTP server.- Parameters:
to- email address this mail should be sent tofrom- email address this mail should appear to be sent fromsubject- subject of the emailbody- body of the email
-
sendMimeMessage
public void sendMimeMessage(InputStream messageStream)
Sends an email based on a MimeMessage input stream, most likely read from a file.- Parameters:
messageStream- input stream of the Mime message to send
-
getReceivedMessages
public javax.mail.internet.MimeMessage[] getReceivedMessages()
Returns an array of all messages received until now.- Returns:
- all received messages
-
getReceivedMessage
public javax.mail.internet.MimeMessage getReceivedMessage()
Get the first message of all messages received until now.- Returns:
- the first message received as MimeMessage or null if there was no message received
-
waitAndGetReceivedMessage
public javax.mail.internet.MimeMessage waitAndGetReceivedMessage()
Does the same asgetReceivedMessage()but waits until a message is received or until a timeout is reached.- Returns:
- the first message received as MimeMessage or null if there was no message received
- Throws:
InterruptedException
-
waitAndGetReceivedMessages
public javax.mail.internet.MimeMessage[] waitAndGetReceivedMessages(int numMessages)
Does the same asgetReceivedMessages()but waits until a message is received or until a timeout is reached.- Returns:
- the first message received as MimeMessage or null if there was no message received
- Throws:
InterruptedException
-
waitAndGetReceivedMessages
public javax.mail.internet.MimeMessage[] waitAndGetReceivedMessages(long timeout, int numMessages)Does the same asgetReceivedMessages()but waits until a message is received or until specified timeout is reached.- Returns:
- the first message received as MimeMessage or null if there was no message received
- Throws:
InterruptedException
-
removeAllReceivedMessages
public void removeAllReceivedMessages() throws com.icegreen.greenmail.store.FolderExceptionRemoves all received mails.- Throws:
com.icegreen.greenmail.store.FolderException
-
waitForIncomingMessage
public boolean waitForIncomingMessage(int numberOfMessages)
Wait until the specified number of mails is received or until a timeout is reached.- Returns:
- Returns false if timeout period was reached, otherwise true.
-
waitForIncomingMessage
public boolean waitForIncomingMessage(Long timeout, int numberOfMessages)
Wait until the specified number of mails is received or until specified timeout is reached.- Returns:
- Returns false if timeout period was reached, otherwise true.
-
-