Design: Email in Java

I am creating an email system for a framework that I am developing. So far I have a generic MailMessage interface that will implement every other type of email message, something like strings

public interface MailMessage {
    public String[] getTo();
    public void setTo(String[] to);
    public String getFrom();
    public void setFrom(String from);
    ...
    // you can guess the setters/getters here
    ...
}

      

Then I got SimpleMailMessage, which is exactly what you expect as a simple interface implementation (no encryption or encoding, just text).

I created a MailMessageFactory interface that was used as an abstract factory. I have a SimpleMailMessageFactory that implements a factory to create instances of SimpleMailMessage.

One type of email that I would like to send to the framework is an Alert email, which is essentially a regular email, except that "[Alert]" is a subject line prefix (another could be an email, a list of items order, but I'm not sure where the responsibility for converting the list to String for email lies.) I can subclass SimpleMailMessage and override the setSubject (String subject) method with something like

public class AlertMailMessage {
    ...
    public void setSuject(String subject) {
        this.to = "[Alert]" + to;
    }
    ...
}

      

Or I can create a decorator:

public abstract class EmailDecorator implements MailMessage {
    protected MailMessage email;
    ...
    // delegate all implemented methods to email
    ...
}

public class AlertEmailDecorator extends EmailDecorator {
    public void setSubject(String subject) {
        email.setSubject("[Alert]" + subject);
    }
}

      

Or I can just delegate the addition of the "[Alert]" line to the SimpleMailMessageFactory.

Thoughts on what I have? I think the problem is that I may be too thoughtful and visionary, but I want the perfect design.

0


a source to share


3 answers


The designer seems to me the best option. I think maybe you need to add a subject line with Fwd:

or Re:

, or you may need signature support where you will add a signature to the email body.



+1


a source


Decorator seems to be the best option. However, why are you writing your own Java email framework? Why not just use the JavaMail API?



+1


a source


Sounds the same as Spring support for JavaMail. Don't reinvent the wheel, use existing, proven solutions and build on top of it.

+1


a source







All Articles