Subversion Repositories javautils

Rev

Rev 3 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. package de.viathinksoft.utils.mail.sender;
  2.  
  3. // Ref:
  4. // http://openbook.galileocomputing.de/javainsel8/javainsel_18_012.htm#mjb306e4632c440d0524d00f224d4fa1bb - Kapitel 18.12.6
  5. // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html
  6.  
  7. import java.util.Properties;
  8.  
  9. import javax.mail.Authenticator;
  10. import javax.mail.Message;
  11. import javax.mail.MessagingException;
  12. import javax.mail.Session;
  13. import javax.mail.Transport;
  14. import javax.mail.internet.InternetAddress;
  15. import javax.mail.internet.MimeMessage;
  16.  
  17. abstract public class RawMailSender {
  18.  
  19.         private static final String TRANSPORT_PROTOCOL = "smtp";
  20.  
  21.         protected Session session;
  22.         protected Message msg;
  23.         protected Properties props;
  24.         protected Authenticator auth;
  25.  
  26.         private String smtpHost = "localhost";
  27.         private String smtpUsername = "";
  28.         private String smtpPassword = "";
  29.         private int smtpPort = 25;
  30.         private boolean smtpAuth = false;
  31.         // private String smtpAuthUser = "";
  32.         // private String smtpAuthPass = "";
  33.         private String recipient = "";
  34.         private String subject = "";
  35.         private String mailFrom = "";
  36.  
  37.         public String getMailFrom() {
  38.                 return mailFrom;
  39.         }
  40.  
  41.         public void setMailFrom(String mailFrom) {
  42.                 this.mailFrom = mailFrom.trim();
  43.         }
  44.  
  45.         public String getRecipient() {
  46.                 return recipient;
  47.         }
  48.  
  49.         public void setRecipient(String recipient) {
  50.                 this.recipient = recipient.trim();
  51.         }
  52.  
  53.         public String getSubject() {
  54.                 return subject;
  55.         }
  56.  
  57.         public void setSubject(String subject) {
  58.                 this.subject = subject;
  59.                 if (this.subject == null)
  60.                         this.subject = "";
  61.         }
  62.  
  63.         public String getSmtpHost() {
  64.                 return smtpHost;
  65.         }
  66.  
  67.         public void setSmtpHost(String smtpHost) {
  68.                 this.smtpHost = smtpHost;
  69.                 if (this.smtpHost == null)
  70.                         this.smtpHost = "";
  71.         }
  72.  
  73.         public String getSmtpUsername() {
  74.                 return smtpUsername;
  75.         }
  76.  
  77.         public void setSmtpUsername(String smtpUsername) {
  78.                 this.smtpUsername = smtpUsername;
  79.                 if (this.smtpUsername == null)
  80.                         this.smtpUsername = "";
  81.         }
  82.  
  83.         public String getSmtpPassword() {
  84.                 return smtpPassword;
  85.         }
  86.  
  87.         public void setSmtpPassword(String smtpPassword) {
  88.                 this.smtpPassword = smtpPassword;
  89.                 if (this.smtpPassword == null)
  90.                         this.smtpPassword = "";
  91.         }
  92.  
  93.         public boolean isSmtpAuth() {
  94.                 return smtpAuth;
  95.         }
  96.  
  97.         public void setSmtpAuth(boolean smtpAuth) {
  98.                 this.smtpAuth = smtpAuth;
  99.         }
  100.  
  101.         // public String getSmtpAuthUser() {
  102.         // return smtpAuthUser;
  103.         // }
  104.         //
  105.         // public void setSmtpAuthUser(String smtpAuthUser) {
  106.         // this.smtpAuthUser = smtpAuthUser;
  107.         // if (this.smtpAuthUser == null) this.smtpAuthUser = "";
  108.         // }
  109.         //
  110.         // public String getSmtpAuthPass() {
  111.         // return smtpAuthPass;
  112.         // }
  113.         //
  114.         // public void setSmtpAuthPass(String smtpAuthPass) {
  115.         // this.smtpAuthPass = smtpAuthPass;
  116.         // if (this.smtpAuthPass == null) this.smtpAuthPass = "";
  117.         // }
  118.  
  119.         public int getSmtpPort() {
  120.                 return smtpPort;
  121.         }
  122.  
  123.         public void setSmtpPort(int smtpPort) {
  124.                 this.smtpPort = smtpPort;
  125.         }
  126.  
  127.         protected void generateProperties()
  128.                         throws AuthentificateDataIncompleteException {
  129.                 props = new Properties();
  130.  
  131.                 // http://72.5.124.55/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
  132.                 if ((smtpHost != null) && (!"".equals(smtpHost))) {
  133.                         props.put("mail.smtp.host", smtpHost);
  134.                 }
  135.                 props.setProperty("mail.transport.protocol", TRANSPORT_PROTOCOL);
  136.                 props.setProperty("mail.smtp.port", "" + smtpPort);
  137.                 if ((smtpUsername != null) && (!"".equals(smtpUsername))) {
  138.                         props.setProperty("mail.smtp.user", smtpUsername);
  139.                 }
  140.  
  141.                 props.put("mail.smtp.auth", smtpAuth);
  142.                 if (smtpAuth) {
  143.                         String smtpAuthUser = smtpUsername;
  144.                         String smtpAuthPass = smtpPassword;
  145.  
  146.                         if ((smtpAuthUser != null) && (!"".equals(smtpAuthUser))) {
  147.                                 if ((smtpAuthPass != null) && (!"".equals(smtpAuthPass))) {
  148.                                         auth = new SmtpAuthenticator(smtpAuthUser, smtpAuthPass);
  149.                                 } else {
  150.                                         throw new AuthentificateDataIncompleteException();
  151.                                 }
  152.                         } else {
  153.                                 throw new AuthentificateDataIncompleteException();
  154.                         }
  155.                 } else {
  156.                         auth = null;
  157.                 }
  158.  
  159.                 if ((mailFrom != null) && (!"".equals(mailFrom))) {
  160.                         props.setProperty("mail.smtp.from", mailFrom);
  161.                 }
  162.         }
  163.  
  164.         protected void generateSession() {
  165.                 // http://blog.dafuer.de/2006/08/22/javamail-access-to-default-session-denied/
  166.                 // final Session session = Session.getDefaultInstance(props, auth);
  167.                 session = Session.getInstance(props, auth);
  168.         }
  169.  
  170.         protected void generateMailObject() throws MessagingException,
  171.                         AuthentificateDataIncompleteException {
  172.  
  173.                 /* final Message */msg = new MimeMessage(session);
  174.  
  175.                 final InternetAddress addressTo = new InternetAddress(recipient);
  176.                 msg.setRecipient(Message.RecipientType.TO, addressTo);
  177.                 msg.setSubject(subject);
  178.  
  179.                 if ((mailFrom != null) && (!"".equals(mailFrom))) {
  180.                         final InternetAddress addressFrom = new InternetAddress(mailFrom);
  181.                         msg.setFrom(addressFrom);
  182.                 }
  183.         }
  184.  
  185.         protected void doSend() throws MessagingException {
  186.                 Transport tr = session.getTransport(TRANSPORT_PROTOCOL);
  187.                 tr.connect(smtpHost, smtpPort, smtpUsername, smtpPassword);
  188.                 msg.saveChanges(); // don't forget this
  189.                 tr.sendMessage(msg, msg.getAllRecipients());
  190.                 tr.close();
  191.         }
  192.  
  193.         public void sendMail() throws MessagingException,
  194.                         AuthentificateDataIncompleteException {
  195.  
  196.                 // Damit die Funktionalität später erweitert oder verändert werden kann!
  197.                 generateProperties();
  198.                 generateSession();
  199.                 generateMailObject();
  200.                 doSend();
  201.         }
  202. }