View Javadoc
1   package com.nilhcem.fakesmtp.server;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import org.subethamail.smtp.helper.SimpleMessageListener;
6   
7   /**
8    * Listens to incoming emails and redirects them to the {@code MailSaver} object.
9    *
10   * @author Nilhcem
11   * @since 1.0
12   */
13  public final class MailListener implements SimpleMessageListener {
14  	private final MailSaver saver;
15  
16  	/**
17  	 * Creates the listener.
18  	 *
19  	 * @param saver a {@code MailServer} object used to save emails and notify components.
20  	 */
21  	public MailListener(MailSaver saver) {
22  		this.saver = saver;
23  	}
24  
25  	/**
26  	 * Accepts all kind of email <i>(always return true)</i>.
27  	 * <p>
28  	 * Called once for every RCPT TO during a SMTP exchange.<br>
29       * Each accepted recipient will result in a separate deliver() call later.
30       * </p>
31       *
32  	 * @param from the user who send the email.
33  	 * @param recipient the recipient of the email.
34  	 * @return always return {@code true}
35  	 */
36  	public boolean accept(String from, String recipient) {
37  		return true;
38  	}
39  
40      /**
41       * Receives emails and forwards them to the {@link MailSaver} object.
42       */
43  	@Override
44  	public void deliver(String from, String recipient, InputStream data) throws IOException {
45  		saver.saveEmailAndNotify(from, recipient, data);
46  	}
47  }