Skip to content

Commit

Permalink
Merge pull request #216 from coolcrowd/mailCleanup
Browse files Browse the repository at this point in the history
Mail cleanup
  • Loading branch information
marcelhollerbach committed Mar 15, 2016
2 parents 7801e35 + aea042b commit fb41b04
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.kit.ipd.crowdcontrol.objectservice.mail;

import edu.kit.ipd.crowdcontrol.objectservice.ConfigLoader;
import edu.kit.ipd.crowdcontrol.objectservice.Main;
import edu.kit.ipd.crowdcontrol.objectservice.config.Config;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -21,7 +20,6 @@
public class MailHandlerTest {
protected MailSender sender;
protected MailFetcher fetcher;
protected String mail;
protected String receiver;
protected String folder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
* A Mailhandler that prints to the command-line, used for running the Object-Service in an development-environment.
* A mail handler that prints to the command-line, used for running the Object-Service in an development-environment.
* @author LeanderK
* @version 1.0
*/
public class CommandLineMailHandler implements MailSender, MailFetcher {
private static final Logger LOGGER = LogManager.getRootLogger();
private static final Logger LOGGER = LogManager.getLogger(CommandLineMailHandler.class);

/**
* Fetches all unseen mails in a certain folder and marks them as seen.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package edu.kit.ipd.crowdcontrol.objectservice.mail;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.mail.*;
import javax.mail.search.FlagTerm;
import java.util.Properties;
Expand All @@ -24,11 +27,16 @@ public enum Protocol {
private final int port;
private final String defaultInbox;
private Properties props;
private static final Logger LOGGER = LogManager.getLogger(MailReceiver.class);


/**
* A Mailhandler object to send and fetch emails.
*
* Creates a new mail receiver, which can fetch mails from a mailbox.
* @param protocol protocol to receive mails (e.g. imap)
* @param user username on the mailserver
* @param password password on the mailserver
* @param host address of the server host
* @param port port, the mail receiver should access the server
*/
public MailReceiver(Protocol protocol, String user, String password, String host, int port, String defaultInbox, boolean debug) {
this.protocol = protocol;
Expand All @@ -50,8 +58,12 @@ public MailReceiver(Protocol protocol, String user, String password, String host
props.setProperty("mail."+protocol+".ssl.checkserveridentity", "true");
}

/**
* {@inheritDoc}
*/
@Override
public Message[] fetchUnseen(String name) throws MessagingException {
LOGGER.trace("Started fetching unseen mails from folder " + name + ".");
Store store = connect();

Message[] messages;
Expand All @@ -70,11 +82,16 @@ public Message[] fetchUnseen(String name) throws MessagingException {

folder.open(Folder.READ_ONLY);

LOGGER.trace("Successfully completed fetching " + messages.length + " unseen mails from folder" + name + ".");
return messages;
}

/**
* {@inheritDoc}
*/
@Override
public Message[] fetchFolder(String name) throws MessagingException {
LOGGER.trace("Started fetching mails from folder " + name + ".");
Store store = connect();

Folder folder = store.getFolder(name);
Expand All @@ -87,11 +104,17 @@ public Message[] fetchFolder(String name) throws MessagingException {
folder.close(true);

folder.open(Folder.READ_ONLY);

LOGGER.trace("Successfully completed fetching " + messages.length + " mails from folder" + name + ".");
return messages;
}

/**
* {@inheritDoc}
*/
@Override
public void markAsUnseen(Message message) throws MessagingException {
LOGGER.trace("Started marking message with subject \"" + message.getSubject() + "\" as unseen.");
Folder folder = message.getFolder();
boolean wasOpen = false;
int mode = 0;
Expand All @@ -108,10 +131,16 @@ public void markAsUnseen(Message message) throws MessagingException {
if (wasOpen) {
folder.open(mode);
}
LOGGER.trace("Successfully completed marking message with subject \"" + message.getSubject() + "\" as unseen.");
}

/**
* {@inheritDoc}
*/
@Override
public void deleteMails(Message message) throws MessagingException {
LOGGER.trace("Started deleting message with subject \"" + message.getSubject() + "\".");

Folder folder = message.getFolder();
boolean wasOpen = false;
int mode = 0;
Expand All @@ -129,6 +158,7 @@ public void deleteMails(Message message) throws MessagingException {
if (wasOpen) {
folder.open(mode);
}
LOGGER.trace("Successfully completed deleting message with subject \"" + message.getSubject() + "\".");
}

@Override
Expand All @@ -138,10 +168,11 @@ public Message[] fetchUnseen() throws MessagingException {

/**
* Closes the folder and the store of the messages.
* @param messages the messages their resources become closed
* @param messages the messages their resources become closed (have to be in the same folder)
* @throws MessagingException in case of problems with closing
*/
public void close(Message[] messages) throws MessagingException {
LOGGER.trace("Started closing folder of " + messages.length + " messages.");
if (messages.length > 0) {
if (messages[0].getFolder().isOpen()) {
messages[0].getFolder().close(true);
Expand All @@ -150,6 +181,7 @@ public void close(Message[] messages) throws MessagingException {
messages[0].getFolder().getStore().close();
}
}
LOGGER.trace("Successfully completed closing folder of " + messages.length + " messages.");
}

private Store connect() throws MessagingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.Properties;

/**
* Created by marcel on 14.03.16.
* Describes a MailSender, which can send mails to other mail addresses.
* @author Felix Rittler
* @author Marcel Hollerbach
*/
public class MailSend implements MailSender {

Expand Down Expand Up @@ -53,6 +55,9 @@ public MailSend(Protocol protocol, String user, String password, String from, St
properties.setProperty("mail." + protocol + ".ssl.checkserveridentity", "true");
}

/**
* {@inheritDoc}
*/
@Override
public void sendMail(String recipientMail, String subject, String message) throws MessagingException, UnsupportedEncodingException {
Session session = Session.getInstance(properties, new Authenticator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*
* @author Felix Rittler
*/

public interface MailSender {

/**
Expand All @@ -18,5 +17,5 @@ public interface MailSender {
* @param subject the subject of the mail
* @param message the content of the mail
*/
public void sendMail(String recipientMail, String subject, String message) throws MessagingException, UnsupportedEncodingException;
void sendMail(String recipientMail, String subject, String message) throws MessagingException, UnsupportedEncodingException;
}

0 comments on commit fb41b04

Please sign in to comment.