1 25 26 package net.killingar.actions.email; 27 28 import net.killingar.actions.ActionSupport; 29 30 import java.util.Properties ; 31 32 public class Send extends ActionSupport 33 { 34 String 35 from, 36 to, 37 cc, 38 subject, 39 body, 40 smtpServer; 41 42 44 public String getFrom () { return from; } 46 public String getTo () { return to; } 47 public String getCc () { return cc; } 48 public String getSubject() { return subject; } 49 public String getBody () { return body; } 50 public String getSmtpServer() { return smtpServer; } 51 52 public void setFrom (String in) { from = in; } 54 public void setTo (String in) { to = in; } 55 public void setCc (String in) { cc = in; } 56 public void setSubject(String in) { subject = in; } 57 public void setBody (String in) { body = in; } 58 public void setSmtpServer(String in) { smtpServer = in; } 59 60 public void doValidation() 62 { 63 if (to == null || to.length() == 0) 64 addError("to", "no to mail address specified"); 65 66 if (subject == null) 67 addError("subject", "no subject"); 68 if (body == null) 69 addError("body", "no body"); 70 if (smtpServer == null) 71 addErrorMessage("no smtp server specified"); 72 if (from == null) 73 addError("from", "no from field"); 74 } 75 76 public String doExecute() 77 { 78 try 79 { 80 Properties props = System.getProperties(); 81 props.put("mail.smtp.host", smtpServer); 82 javax.mail.Session mailSession = javax.mail.Session.getInstance(props); 83 javax.mail.Message mail = new javax.mail.internet.MimeMessage (mailSession); 84 85 mail.setFrom(new javax.mail.internet.InternetAddress (from)); 86 mail.setContent(body, "text/plain"); mail.setSubject(subject); 88 mail.setRecipient(javax.mail.Message.RecipientType.TO, new javax.mail.internet.InternetAddress (to)); 89 if (cc != null && cc.length() != 0) 90 mail.setRecipient(javax.mail.Message.RecipientType.CC, new javax.mail.internet.InternetAddress (cc)); 91 mail.setSentDate(new java.util.Date ()); 93 94 javax.mail.Transport.send(mail); 95 } 96 catch (Exception e) 97 { 98 addErrorMessage("sending email failed, exception thrown ("+e.toString()+")"); 99 e.printStackTrace(); 100 101 return ERROR; 102 } 103 104 return SUCCESS; 105 } 106 } 107 | Popular Tags |