Skip to content

Commit

Permalink
#29 Restructured packages
Browse files Browse the repository at this point in the history
#30 Better demonstration test class
  • Loading branch information
bbottema committed Feb 25, 2016
1 parent 93d7e95 commit 7faefe9
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 468 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ Simple Java Mail is available in Maven Central:
<dependency>
<groupId>org.codemonkey.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>2.5.1</version>
<version>3.0.0</version>
</dependency>
```

### Latest Progress ###

v3.0.0

* [#22](https://github.com/bbottema/simple-java-mail/issues/22): Added conversion to and from MimeMessage. You can now consume and produce MimeMessage objects with simple-java-mail
* [#28](https://github.com/bbottema/simple-java-mail/issues/28): Re-added improved email validation facility
* [#29](https://github.com/bbottema/simple-java-mail/issues/29): The package has been restructured for future maintenance, breaking backwards compatibility

v2.5.1

* [#25](https://github.com/bbottema/simple-java-mail/issues/25): Added finally clausule that will always close socket properly in case of an exception
* [#25](https://github.com/bbottema/simple-java-mail/issues/25): Added finally clause that will always close socket properly in case of an exception

v2.5

Expand Down
11 changes: 9 additions & 2 deletions RELEASE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ https://github.com/bbottema/simple-java-mail/
<dependency>
<groupId>org.codemonkey.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>2.5.1</version>
<version>3.0.0</version>
</dependency>

RELEASE NOTES Java Simple Mail

v3.0.0

- #22: Added conversion to and from MimeMessage. You can now consume and produce MimeMessage objects with simple-java-mail
- #28: Re-added improved email validation facility
- #29: The package has been restructured for future maintenance, breaking backwards compatibility


v2.5.1

- #25: Added finally clausule that will always close socket properly in case of an exception
Expand All @@ -18,7 +25,7 @@ v2.5

v2.4

- #21: Builder API uses CC and BCC recepient types incorrectly
- #21: Builder API uses CC and BCC recipient types incorrectly


v2.3
Expand Down
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.2</version>
Expand Down
70 changes: 40 additions & 30 deletions src/main/java/MailTest.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
import javax.mail.Message.RecipientType;

import org.codemonkey.simplejavamail.Email;
import org.codemonkey.simplejavamail.Mailer;
import org.codemonkey.simplejavamail.TransportStrategy;
import org.codemonkey.simplejavamail.email.Email;

import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.util.Base64;
import java.util.Properties;

/**
* Demonstration program for the Simple Java Mail framework.
* <p>
* <b>IMPORTANT</b>: <br>
* This testclass was designed to run from the commandline (or by Ant) and expects some system properties to be present. See
* <b>Readme.txt</b> for instructions. Alternatively, you can assign the host, username and password a hard value and ignore the system
* properties altogether.
*
*
* @author Benny Bottema
*/
public class MailTest {

/**
* Just run as Java application, but remember to set the JVM arguments first.
*
* @param args should be empty, this demo uses JVM arguments only (-Dhost=value etc.).
*/
public static void main(final String[] args) {
final Email email = new Email();
email.setFromAddress("lollypop", "lol.pop@somemail.com");
email.addRecipient("C.Cane", "candycane@candyshop.org", RecipientType.TO);
email.setText("We should meet up!");
email.setTextHTML("<b>We should meet up!</b>");
email.setSubject("hey");
sendMail(email);
}
public static void main(final String[] args) throws IOException, MessagingException {
final Email emailNormal = new Email();
emailNormal.setFromAddress("lollypop", "lol.pop@somemail.com");
// don't forget to add your own address here ->
emailNormal.addRecipient("C.Cane", "candycane@candyshop.org", RecipientType.TO);
emailNormal.setText("We should meet up!");
emailNormal.setTextHTML("<b>We should meet up!</b><img src='cid:winksmiley'>");
emailNormal.setSubject("hey");

// add two text files in different ways and a black thumbs up embedded image ->
emailNormal.addAttachment("dresscode.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain"));
emailNormal.addAttachment("location.txt", "On the moon!".getBytes(), "text/plain");
String base64String = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABeElEQVRYw2NgoAAYGxu3GxkZ7TY1NZVloDcAWq4MxH+B+D8Qv3FwcOCgtwM6oJaDMTAUXOhmuYqKCjvQ0pdoDrCnmwNMTEwakC0H4u8GBgYC9Ap6DSD+iewAoIPm0ctyLqBlp9F8/x+YE4zpYT8T0LL16JYD8U26+B7oyz4sloPwenpYno3DchCeROsUbwa05A8eB3wB4kqgIxOAuArIng7EW4H4EhC/B+JXQLwDaI4ryZaDSjeg5mt4LCcFXyIn1fdSyXJQVt1OtMWGhoai0OD8T0W8GohZifE1PxD/o7LlsPLiFNAKRrwOABWptLAcqc6QGDAHQEOAYaAc8BNotsJAOgAUAosG1AFA/AtUoY3YEFhKMAvS2AE7iC1+WaG1H6gY3gzE36hUFJ8mqzbU1dUVBBqQBzTgIDQRkWo5qCZdpaenJ0Zx1aytrc0DDB0foIG1oAYKqC0IZK8D4n1AfA6IzwPxXpCFoGoZVEUDaRGGUTAKRgEeAAA2eGJC+ETCiAAAAABJRU5ErkJggg==";
emailNormal.addEmbeddedImage("winksmiley", Base64.getDecoder().decode(base64String), "image/png");

// let's try producing and then consuming a MimeMessage ->
final MimeMessage mimeMessage = Mailer.produceMimeMessage(emailNormal, Session.getDefaultInstance(new Properties()));
final Email emailFromMimeMessage = new Email(mimeMessage);

sendMail(emailNormal);
sendMail(emailFromMimeMessage); // should produce the exact same result as emailNormal!
}

private static void sendMail(final Email email) {
final String host = System.getProperty("host") != null ? System.getProperty("host") : "";
final int port = System.getProperty("port") != null ? Integer.parseInt(System.getProperty("port")) : 25;
final String username = System.getProperty("username") != null ? System.getProperty("username") : "";
final String password = System.getProperty("password") != null ? System.getProperty("password") : "";
new Mailer(host, port, username, password, TransportStrategy.SMTP_SSL).sendMail(email);
}
private static void sendMail(final Email email) {
final String host = System.getProperty("host") != null ? System.getProperty("host") : "";
final int port = System.getProperty("port") != null ? Integer.parseInt(System.getProperty("port")) : 25;
final String username = System.getProperty("username") != null ? System.getProperty("username") : "";
final String password = System.getProperty("password") != null ? System.getProperty("password") : "";
new Mailer(host, port, username, password, TransportStrategy.SMTP_SSL).sendMail(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ public final class MailException extends RuntimeException {
protected static final String MISSING_RECIPIENT = "Email is not valid: missing recipients";
protected static final String MISSING_SUBJECT = "Email is not valid: missing subject";
protected static final String MISSING_CONTENT = "Email is not valid: missing content body";
protected static final String PARSE_MIMEMESSAGE_ERROR = "Error parsing MimeMessage: %s";

protected MailException(final String message) {
super(message);
}

protected MailException(final String message, final Exception cause) {
public MailException(final String message, final Exception cause) {
super(message, cause);
}
}
23 changes: 14 additions & 9 deletions src/main/java/org/codemonkey/simplejavamail/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.codemonkey.simplejavamail.email.AttachmentResource;
import org.codemonkey.simplejavamail.email.Email;
import org.codemonkey.simplejavamail.email.Recipient;
import org.codemonkey.simplejavamail.util.EmailAddressValidationCriteria;
import org.codemonkey.simplejavamail.util.EmailValidationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -68,7 +73,7 @@
*/
public class Mailer {

private static final Logger logger = LoggerFactory.getLogger(Mailer.class);
private static final Logger LOGGER = LoggerFactory.getLogger(Mailer.class);

/**
* Encoding used for setting body text, email address, headers, reply-to fields etc. ({@value #CHARACTER_ENCODING}).
Expand Down Expand Up @@ -159,15 +164,15 @@ public Mailer(final String host, final Integer port, final String username, fina
*/
protected Session createMailSession(final String host, final Integer port, final String username, final String password) {
if (transportStrategy == null) {
logger.warn("Transport Strategy not set, using plain SMTP strategy instead!");
LOGGER.warn("Transport Strategy not set, using plain SMTP strategy instead!");
transportStrategy = TransportStrategy.SMTP_PLAIN;
}
Properties props = transportStrategy.generateProperties();
props.put(transportStrategy.propertyNameHost(), host);
if (port != null) {
props.put(transportStrategy.propertyNamePort(), String.valueOf(port));
} else {
// let JavaMail's Transport objects determine deault port base don the used protocol
// let JavaMail's Transport objects determine default port base don the used protocol
}

if (username != null) {
Expand Down Expand Up @@ -205,8 +210,8 @@ public Mailer(final String host, final Integer port, final String username, fina
* let us know why you are needing this on https://github.com/bbottema/simple-java-mail/issues.
*/
public Session getSession() {
logger.warn("Providing access to Session instance for emergency fall-back scenario. Please let us know why you need it.");
logger.warn("\t>https://github.com/bbottema/simple-java-mail/issues");
LOGGER.warn("Providing access to Session instance for emergency fall-back scenario. Please let us know why you need it.");
LOGGER.warn("\t>https://github.com/bbottema/simple-java-mail/issues");
return session;
}

Expand Down Expand Up @@ -263,10 +268,10 @@ public final void sendMail(final Email email)
transport.close();
}
} catch (final UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
LOGGER.error(e.getMessage(), e);
throw new MailException(String.format(MailException.INVALID_ENCODING, e.getMessage()));
} catch (final MessagingException e) {
logger.error(e.getMessage(), e);
LOGGER.error(e.getMessage(), e);
throw new MailException(String.format(MailException.GENERIC_ERROR, e.getMessage()), e);
}
}
Expand All @@ -286,7 +291,7 @@ private void logSession(Session session, TransportStrategy transportStrategy) {
} else {
specifics = properties.toString();
}
logger.debug(String.format("starting mail session (%s)", specifics));
LOGGER.debug(String.format("starting mail session (%s)", specifics));
}

/**
Expand Down Expand Up @@ -537,7 +542,7 @@ private static class MimeEmailMessageWrapper {
multipartRelated.addBodyPart(contentAlternativeMessages);
contentAlternativeMessages.setContent(multipartAlternativeMessages);
} catch (final MessagingException e) {
logger.error(e.getMessage(), e);
LOGGER.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
package org.codemonkey.simplejavamail;

import javax.activation.DataSource;

/**
* A named immutable email attachment information object. The name can be a simple name, a filename or a named embedded
* image (eg. &lt;cid:footer&gt;). Contains a {@link DataSource} that is compatible with the javax.mail API.
*
* @author Benny Bottema
* @see DataSource
*/
final class AttachmentResource {

/**
* @see #AttachmentResource(String, DataSource)
*/
private final String name;

/**
* @see #AttachmentResource(String, DataSource)
*/
private final DataSource dataSource;

/**
* Constructor; initializes the attachment resource with a name and data.
*
* @param name The name of the attachment which can be a simple name, a filename or a named embedded image (eg.
* &lt;cid:footer&gt;)
* @param dataSource The attachment data.
* @see DataSource
*/
public AttachmentResource(final String name, final DataSource dataSource) {
this.name = name;
this.dataSource = dataSource;
}

/**
* Bean getter for {@link #dataSource}.
*/
public DataSource getDataSource() {
return dataSource;
}

/**
* Bean getter for {@link #name}.
*/
public String getName() {
return name;
}
package org.codemonkey.simplejavamail.email;

import javax.activation.DataSource;

/**
* A named immutable email attachment information object. The name can be a simple name, a filename or a named embedded
* image (eg. &lt;cid:footer&gt;). Contains a {@link DataSource} that is compatible with the javax.mail API.
*
* @author Benny Bottema
* @see DataSource
*/
public class AttachmentResource {

/**
* @see #AttachmentResource(String, DataSource)
*/
private final String name;

/**
* @see #AttachmentResource(String, DataSource)
*/
private final DataSource dataSource;

/**
* Constructor; initializes the attachment resource with a name and data.
*
* @param name The name of the attachment which can be a simple name, a filename or a named embedded image (eg.
* &lt;cid:footer&gt;)
* @param dataSource The attachment data.
* @see DataSource
*/
public AttachmentResource(final String name, final DataSource dataSource) {
this.name = name;
this.dataSource = dataSource;
}

/**
* Bean getter for {@link #dataSource}.
*/
public DataSource getDataSource() {
return dataSource;
}

/**
* Bean getter for {@link #name}.
*/
public String getName() {
return name;
}
}
Loading

0 comments on commit 7faefe9

Please sign in to comment.