Skip to content

Commit

Permalink
add MailTransporter interface to provide an alternate send path
Browse files Browse the repository at this point in the history
  • Loading branch information
codylerum committed Oct 15, 2011
1 parent 88b2397 commit 41e9729
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 20 deletions.
Expand Up @@ -21,7 +21,6 @@
* @author Cody Lerum
*/
public class BasicEmailContact implements EmailContact {
private static final long serialVersionUID = 1L;

private String address;
private String name;
Expand Down
7 changes: 3 additions & 4 deletions api/src/main/java/org/jboss/seam/mail/core/MailConfig.java
Expand Up @@ -24,16 +24,15 @@
import com.google.common.base.Strings;

/**
* Bean which holds Mail Session configuration options. Can be configured via
* Seam XML
*
* Bean which holds Mail Session configuration options. Can be configured via Seam XML
*
* @author Cody Lerum
*/
@ApplicationScoped
public class MailConfig implements Serializable {

private static final long serialVersionUID = 1L;

private String serverHost = "localhost";
private Integer serverPort = 25;
private String domainName;
Expand Down
23 changes: 23 additions & 0 deletions api/src/main/java/org/jboss/seam/mail/core/MailTransporter.java
@@ -0,0 +1,23 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.seam.mail.core;

public interface MailTransporter {

public EmailMessage send(EmailMessage emailMessage);
}
Expand Up @@ -84,7 +84,8 @@ public void addRecipient(RecipientType recipientType, InternetAddress emailAddre
try {
rootMimeMessage.addRecipient(recipientType.getRecipientType(), emailAddress);
} catch (MessagingException e) {
throw new RuntimeException("Unable to add recipient " + recipientType + ": " + emailAddress.toString() + " to MIME message", e);
throw new RuntimeException("Unable to add recipient " + recipientType + ": " + emailAddress.toString()
+ " to MIME message", e);
}
}

Expand Down Expand Up @@ -316,7 +317,9 @@ private MimeBodyPart buildHTMLBodyPart(String html) {
}

public void addAttachment(EmailAttachment emailAttachment) {
AttachmentPart attachment = new AttachmentPart(emailAttachment.getBytes(), emailAttachment.getContentId(), emailAttachment.getFileName(), emailAttachment.getMimeType(), emailAttachment.getHeaders(), emailAttachment.getContentDisposition());
AttachmentPart attachment = new AttachmentPart(emailAttachment.getBytes(), emailAttachment.getContentId(),
emailAttachment.getFileName(), emailAttachment.getMimeType(), emailAttachment.getHeaders(),
emailAttachment.getContentDisposition());
attachments.put(attachment.getAttachmentFileName(), attachment);
}

Expand Down
19 changes: 15 additions & 4 deletions impl/src/main/java/org/jboss/seam/mail/core/MailMessageImpl.java
Expand Up @@ -40,6 +40,7 @@
import org.jboss.seam.mail.templating.TemplateProvider;
import org.jboss.seam.mail.util.EmailAttachmentUtil;
import org.jboss.seam.mail.util.MailUtility;
import org.jboss.solder.core.ExtensionManaged;

/**
* @author Cody Lerum
Expand All @@ -55,6 +56,7 @@ public class MailMessageImpl implements MailMessage {
private boolean templatesMerged;

@Inject
@ExtensionManaged
private Instance<Session> session;

public MailMessageImpl() {
Expand Down Expand Up @@ -308,7 +310,7 @@ public MailMessage bodyHtmlTextAlt(TemplateProvider htmlBody, TemplateProvider t
bodyText(textBody);
return this;
}

public MailMessage contentType(ContentType contentType) {
emailMessage.setRootContentType(contentType);
return this;
Expand Down Expand Up @@ -352,17 +354,26 @@ public EmailMessage mergeTemplates() {
return emailMessage;
}

public EmailMessage send(Session session) throws SendFailedException {
public EmailMessage send(MailTransporter mailTransporter) throws SendFailedException {

if (!templatesMerged) {
mergeTemplates();
}

MailUtility.send(emailMessage, session);
try {
mailTransporter.send(emailMessage);
} catch (Exception e) {
throw new SendFailedException("Send Failed", e);
}

return emailMessage;
}

public EmailMessage send(Session session) throws SendFailedException {
return send(new MailTransporterImpl(session));
}

public EmailMessage send() throws SendFailedException {
return this.send(session.get());
}
}
}
Expand Up @@ -21,14 +21,16 @@
import javax.mail.Session;

import org.jboss.seam.mail.util.MailUtility;
import org.jboss.solder.core.ExtensionManaged;

/**
* @author Cody Lerum
*/
public class MailSessionProducer {

@Produces
@ExtensionManaged
public Session getMailSession(MailConfig mailConfig) {
return MailUtility.buildMailSession(mailConfig);
return MailUtility.createSession(mailConfig);
}
}
@@ -0,0 +1,42 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.seam.mail.core;

import javax.mail.Session;

import org.jboss.seam.mail.util.MailUtility;

/**
*
* @author Cody Lerum
*
*/
public class MailTransporterImpl implements MailTransporter {

private Session session;

public MailTransporterImpl(Session session) {
this.session = session;
}

public EmailMessage send(EmailMessage emailMessage) {
MailUtility.send(emailMessage, session);
return emailMessage;
}

}
17 changes: 10 additions & 7 deletions impl/src/main/java/org/jboss/seam/mail/util/MailUtility.java
Expand Up @@ -72,7 +72,8 @@ public static InternetAddress internetAddress(EmailContact emailContact) throws
}
}

public static Collection<InternetAddress> internetAddress(Collection<EmailContact> emailContacts) throws InvalidAddressException {
public static Collection<InternetAddress> internetAddress(Collection<EmailContact> emailContacts)
throws InvalidAddressException {
Set<InternetAddress> internetAddresses = new HashSet<InternetAddress>();

for (EmailContact ec : emailContacts) {
Expand All @@ -83,7 +84,7 @@ public static Collection<InternetAddress> internetAddress(Collection<EmailContac
}

public static InternetAddress[] getInternetAddressses(InternetAddress emaiAddress) {
InternetAddress[] internetAddresses = {emaiAddress};
InternetAddress[] internetAddresses = { emaiAddress };

return internetAddresses;
}
Expand All @@ -103,11 +104,11 @@ public static String getHostName() {
}
}

public static Session buildMailSession(MailConfig mailConfig) {
public static Session createSession(MailConfig mailConfig) {
Session session;

Properties props = new Properties();

if (mailConfig.isValid()) {
props.setProperty("mail.smtp.host", mailConfig.getServerHost());
props.setProperty("mail.smtp.port", mailConfig.getServerPort().toString());
Expand All @@ -123,14 +124,16 @@ public static Session buildMailSession(MailConfig mailConfig) {
props.put("mail.seam.domainName", mailConfig.getDomainName());
}

if (mailConfig.getUsername() != null && mailConfig.getUsername().length() != 0 && mailConfig.getPassword() != null && mailConfig.getPassword().length() != 0) {
MailSessionAuthenticator authenticator = new MailSessionAuthenticator(mailConfig.getUsername(), mailConfig.getPassword());
if (mailConfig.getUsername() != null && mailConfig.getUsername().length() != 0 && mailConfig.getPassword() != null
&& mailConfig.getPassword().length() != 0) {
MailSessionAuthenticator authenticator = new MailSessionAuthenticator(mailConfig.getUsername(),
mailConfig.getPassword());

session = Session.getInstance(props, authenticator);
} else {
session = Session.getInstance(props, null);
}

return session;
}

Expand Down
Expand Up @@ -45,6 +45,6 @@ public Session getMailSession() {
mailConfig.setPassword("test12!");
mailConfig.setAuth(true);

return MailUtility.buildMailSession(mailConfig);
return MailUtility.createSession(mailConfig);
}
}

0 comments on commit 41e9729

Please sign in to comment.