package e4s.tutorial; import e4s.html.BODY; import e4s.html.E4CgiParams; import e4s.html.E4Color; import e4s.html.E4Message; import e4s.html.E4Method; import e4s.html.FONT; import e4s.html.FORM; import e4s.html.HTML; import e4s.html.TABLE; import e4s.html.input.extended.CHECKBOX; import e4s.html.input.extended.E4InputFieldName; import e4s.html.input.extended.E4ValidatorEMail; import e4s.html.input.extended.E4ValidatorNoBlanks; import e4s.html.input.extended.TEXTAREA; import e4s.html.input.extended.TEXTFIELD; import e4s.mail.E4SendMail; import e4s.servlet.E4ModuleImplementation; /** * Sending eMails from your servlet. * * {@tutorial Example_MailSend} */ public class Example_MailSend extends E4ModuleImplementation { public static E4Method start = null; public static E4Method sendMail = null; // public static E4Method receiveMail = null; private final E4InputFieldName FIELD_TO = new E4InputFieldName("TO"); private final E4InputFieldName FIELD_CC = new E4InputFieldName("CC"); private final E4InputFieldName FIELD_SUBJ = new E4InputFieldName("SUBJ"); private final E4InputFieldName FIELD_MSG = new E4InputFieldName("MSG"); private final E4InputFieldName FIELD_PLAIN = new E4InputFieldName("PLAIN"); public FORM makeForm( boolean init ) { FORM form = new FORM("TESTFORM"); TEXTFIELD fTo = form.TEXTFIELD(FIELD_TO,"To",40); fTo.setValidator(new E4ValidatorEMail(true)); form.BR(); TEXTFIELD fCc = form.TEXTFIELD(FIELD_CC,"Cc",40); fCc.setValidator(new E4ValidatorEMail(false)); form.BR(); CHECKBOX fPlain = form.CHECKBOX(FIELD_PLAIN,"Send as plain text"); form.BR(); TEXTFIELD fSubj = form.TEXTFIELD(FIELD_SUBJ,"Subject",60); fSubj.setValidator(new E4ValidatorNoBlanks()); form.BR(); TEXTAREA fMsg = form.TEXTAREA(FIELD_MSG,"E4Message",60,4); form.P(); form.setAction(sendMail); form.FORM_Submit("Send"); if (init) { fSubj.setValue("Testmail generated from the e4s tutorial"); fMsg.setValue("Hi there, this is my message to you!"); fPlain.setValue(false); } return form; } public void start( HTML html ) { BODY body = html.BODY(); body.Message("Send Mail Tutorial"); FORM form = makeForm(true); body.addElement(form); body.P(); // body.A(receiveMail).print("Receive"); } public void sendMail( HTML html, E4CgiParams params ) throws Exception { BODY body = html.BODY(); FORM form = makeForm(false); if (! form.validate(params,getServlet())) { body.Message(E4Message.ERROR,"Send Mail Tutorial - please correct the errors"); body.addElement(form); } else { String to = params.get(FIELD_TO); String cc = params.get(FIELD_CC); String subj = params.get(FIELD_SUBJ); String msg = params.get(FIELD_MSG); boolean plain_text = params.getBool(FIELD_PLAIN); try { E4SendMail mail = new E4SendMail( to, cc ); if (plain_text) { mail.sendPLAIN(subj,msg); } else { TABLE table = new TABLE(E4ID()); table.setBorder(); table.setBgColor(E4Color.LIGHT_YELLOW); table.TR().TD().FONT(FONT.ARIAL).B().print(subj); if (isok(msg)) table.TR().TD().FONT(FONT.ARIAL).I().print(msg); table.TR().TD().IMG("http://java.sun.com/im/logo_java.gif"); mail.sendHTML(subj,table,getServlet()); } body.Message(E4Message.INFO,"E-mail message sent to " + to); } catch( Exception e ) { body.Message(E4Message.ERROR,"Cannot send e-mail"); body.P(); body.SystemError(e); } } } /* public void receiveMail( HTML html, E4CgiParams params ) throws Exception { ReceiveMail mail = new ReceiveMail(); mail.receive(); } */ }