Friday, January 30, 2009

Sample of Java Pattern -- Template Method

EmailTemplate.java
package templatemethod;

import javax.mail.*;
import javax.mail.internet.*;

public abstract class EmailTemplate {
public void send(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd, String[] recipients, String subject,String message, String from){
MimeMessage msg = connect(smtpHostName, smtpPort, smtpAuthUser, smtpAuthPwd);
try {
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

msg.setSubject(subject);
msg.setContent(message, "text/html");
Transport.send(msg);
} catch (MessagingException e) {
System.out.println("Could not send this email, title=" + subject + " addressTo=" + recipients[0] + ", please check the email server connection.");
}
}

protected abstract MimeMessage connect(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd);
}


GeneralEmailService.java
package templatemethod;

import java.security.Security;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

import templatemethod.GoogleEmailService.SMTPAuthenticator;

public class GeneralEmailService extends EmailTemplate{
private String smtpAuthUser;
private String smtpAuthPwd;
protected MimeMessage connect(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd){

this.smtpAuthUser = smtpAuthUser;
this.smtpAuthPwd = smtpAuthPwd;

boolean debug = false;

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", smtpHostName);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.quitwait", "false");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

MimeMessage msg = new MimeMessage(session);

return msg;

}

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = smtpAuthUser;
String password = smtpAuthPwd;
return new PasswordAuthentication(username, password);
}
}


}


GoogleEmailService.java
package templatemethod;

import java.security.Security;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

public class GoogleEmailService extends EmailTemplate{
private String smtpAuthUser;
private String smtpAuthPwd;
protected MimeMessage connect(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd){

this.smtpAuthUser = smtpAuthUser;
this.smtpAuthPwd = smtpAuthPwd;

boolean debug = false;

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", smtpHostName);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.quitwait", "false");

props.put("mail.smtp.socketFactory.port", smtpPort);
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

MimeMessage msg = new MimeMessage(session);

return msg;

}

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = smtpAuthUser;
String password = smtpAuthPwd;
return new PasswordAuthentication(username, password);
}
}

}


TemplateMethodTest.java
package templatemethod;

public class TemplateMethodTest {

public static void main(String[] args) {
String smtpHostName = "";
String smtpPort = "";
String smtpAuthUser = "";
String smtpAuthPwd = "";
String[] recipients = {""};
String subject = "";
String message = "";
String from = "";
EmailTemplate googleEmailService = new GoogleEmailService();
googleEmailService.send(smtpHostName, smtpPort, smtpAuthUser, smtpAuthPwd, recipients, subject, message, from);
EmailTemplate generalEmailService = new GeneralEmailService();
generalEmailService.send(smtpHostName, smtpPort, smtpAuthUser, smtpAuthPwd, recipients, subject, message, from);
}

}


you may download the source code.

No comments:

Post a Comment