- From: Pasquale Di Feo <pasdif@netfly.it>
- Date: Fri, 26 Jul 1996 19:11:04 +0200
- To: www-jigsaw@w3.org
I have developed a simple PostableResource that send an e-mail from a form. I want return a file (specified in ATTR_REPLYFILE) for the response. What is the best way ??? Pasquale Following the code (excuse me for mix of Italian and English Language) --------------------------------------------------------------------------- package pdf.jigsaw.postable; import w3c.jigsaw.forms.*; import w3c.jigsaw.html.*; import w3c.jigsaw.http.*; import w3c.mime.* ; import w3c.jigsaw.resources.*; import java.io.* ; import java.net.* ; import java.util.*; class MailException extends Exception { public final static int NO_SENDER = 1; public final static int NO_FROM = 2; public final static int NO_TO = 3; public final static int NO_MESSAGE = 4; public final static int NO_CONNECTION = 5; public int m_nType; public MailException(int nType, String strMessage) { super(strMessage); m_nType = nType; } } class MailMessage { protected String m_strSMTP; protected String m_strSender; protected String m_strFrom; protected String m_strRealName; protected String m_strTo; protected String m_strSubject; protected String m_strReplyTo; protected String m_strMessage; protected String m_strLastline; protected InetAddress m_iaHost; protected final static int PORT = 25; public MailMessage(String strSMTP, String strFrom, String strRealName, String strTo, String strSubject , String strMessage) throws MailException { m_strSMTP = strSMTP; m_strSender = strFrom; m_strFrom = strFrom; m_strRealName = strRealName; m_strTo = strTo; m_strReplyTo = strFrom; m_strSubject = strSubject; m_strMessage = strMessage; } public MailMessage(SendMail sendMail, URLDecoder data, InetAddress iaHost) throws MailException { String strTmp; //////////////////////////////////////////////////////////////////// //////////// // retrieve the smtp receive server if ( (strTmp = data.getValue(SendMail.SMTP)) != null) m_strSMTP = strTmp; else if ( (strTmp = sendMail.getString(SendMail.ATTR_SMTP, null)) != null) m_strSMTP = strTmp; else m_strSMTP = "127.0.0.1"; // default localhost (http server relative) //////////////////////////////////////////////////////////////////// //////////// // retrieve the sender if ( (strTmp = data.getValue(SendMail.SENDER)) != null) m_strFrom = strTmp; else if ( (strTmp = sendMail.getString(SendMail.ATTR_SENDER, null)) != null) m_strFrom = strTmp; else m_strFrom = "webmaster"; //////////////////////////////////////////////////////////////////// //////////// // retrieve From if ( (strTmp = data.getValue(SendMail.FROM)) != null) m_strFrom = strTmp; else throw new MailException(MailException.NO_FROM, "No From variable found"); //////////////////////////////////////////////////////////////////// //////////// // retrieve RealName if ( (strTmp = data.getValue(SendMail.REALNAME)) != null) m_strRealName = strTmp; else m_strRealName = ""; //////////////////////////////////////////////////////////////////// //////////// // retrieve To if ( (strTmp = data.getValue(SendMail.TO)) != null) m_strTo = strTmp; else if ( (strTmp = sendMail.getString(SendMail.ATTR_TO, null)) != null) m_strTo = strTmp; else throw new MailException(MailException.NO_TO, "No To variable found"); //////////////////////////////////////////////////////////////////// //////////// // retrieve Subject if ( (strTmp = data.getValue(SendMail.SUBJECT)) != null) m_strSubject = strTmp; else m_strSubject = ""; if (sendMail.getBoolean(SendMail.ATTR_APPENDSUB, false) == true) m_strSubject = sendMail.getString(SendMail.ATTR_SUBJECT, "") + m_strSubject; //////////////////////////////////////////////////////////////////// //////////// // retrieve Message if ( (strTmp = data.getValue(SendMail.MESSAGE)) != null) m_strMessage = strTmp; else throw new MailException(MailException.NO_MESSAGE, "No Message"); m_iaHost = iaHost; } void expect(String expected, String msg, DataInputStream in) throws Exception { m_strLastline = in.readLine(); if (!m_strLastline.startsWith(expected)) { // System.out.println(msg + ":" + lastline); throw new Exception(msg + ":" + m_strLastline); } while (m_strLastline.startsWith(expected + "-")) m_strLastline = in.readLine(); } public boolean send() throws MailException { String helohost; Socket s = null; DataInputStream in; try { String res; s = new Socket(m_strSMTP, PORT); PrintStream p = new PrintStream(s.getOutputStream()); in = new DataInputStream(s.getInputStream()); expect("220", "no greeting", in); helohost = m_iaHost.getHostName(); p.print("HELO " + helohost + "\r\n"); expect("250", "helo", in); int pos; String hello = "Hello "; if ((pos = m_strLastline.indexOf(hello)) != -1) { helohost = m_strLastline.substring(pos + hello.length()); helohost = helohost.substring(0, helohost.indexOf(' ')); } p.print("MAIL FROM: " + m_strSender + "\r\n"); expect("250", "mail from", in); p.print("RCPT TO: " + m_strTo + "\r\n"); expect("250", "rcpt to", in); p.print("DATA\r\n"); expect("354", "data", in); if (!m_strRealName.equals("")) p.print("From: " + m_strRealName + " <" + m_strFrom + ">\r\n"); else p.print("From: " + m_strFrom + "\r\n"); p.print("To: " + m_strTo + "\r\n"); p.print("Subject: " + m_strSubject + " (" + helohost + ")\r\n" ); // end of headers p.print("\r\n"); DataInputStream is = new DataInputStream(new StringBufferInputStream(m_strMessage)); while (is.available() > 0) { String ln = is.readLine(); // System.out.println("Linea messaggio: " + ln); if (ln.equals(".")) ln = ".."; p.print(ln + "\r\n"); } p.print("\r\n.\r\n"); expect("250", "end of data", in); p.print("QUIT\r\n"); expect("221", "quit", in); } catch(Exception e) { return false; } finally { try { if (s != null) s.close(); } catch(Exception e) { } } return true; } } public class SendMail extends PostableResource { protected static int ATTR_SMTP = -1 ; // smtp server protected static int ATTR_SENDER = -1 ; // sender (smtp user) protected static int ATTR_TO = -1 ; // mail to protected static int ATTR_SUBJECT = -1 ; // mail to protected static int ATTR_APPENDSUB = -1 ; // Append the subject from the form protected static int ATTR_REPLYFILE = -1 ; // replyfile // protected static int ATTR_CONTENT_TYPE = -1; protected final static String SMTP = "smtpServer"; protected final static String SENDER = "sender"; protected final static String FROM = "from"; protected final static String REALNAME = "realname"; protected final static String TO = "to"; protected final static String SUBJECT = "subject"; protected final static String REPLYTO = "replyto"; protected final static String MESSAGE = "message"; protected final static String APPENDSUB = "appendSubject"; protected final static String REPLYFILE = "replyFile"; static { Attribute a = null ; Class cls = null ; try { cls = Class.forName("pdf.jigsaw.postable.SendMail") ; } catch (Exception ex) { ex.printStackTrace() ; System.exit(1) ; } a = new StringAttribute(SMTP , "", Attribute.EDITABLE) ; ATTR_SMTP = AttributeRegistery.registerAttribute(cls, a) ; a = new StringAttribute(SENDER, "", Attribute.EDITABLE) ; ATTR_SENDER = AttributeRegistery.registerAttribute(cls, a) ; a = new StringAttribute(TO, "", Attribute.EDITABLE) ; ATTR_TO = AttributeRegistery.registerAttribute(cls, a) ; a = new StringAttribute(SUBJECT, "", Attribute.EDITABLE) ; ATTR_SUBJECT = AttributeRegistery.registerAttribute(cls, a) ; a = new BooleanAttribute(APPENDSUB, new Boolean(false), Attribute.EDITABLE); ATTR_APPENDSUB = AttributeRegistery.registerAttribute(cls, a) ; a = new StringAttribute(REPLYFILE, "", Attribute.EDITABLE) ; ATTR_REPLYFILE = AttributeRegistery.registerAttribute(cls, a) ; } public Reply handle(Request request, URLDecoder data) throws HTTPException { MailMessage msg; try { msg = new MailMessage(this, data, request.getClient().getInetAddress()); if (!msg.send()) { Reply error = request.makeReply(HTTP.NOT_FOUND) ; error.setContent ("<h1>Messaggio non inviato</h1>"); return error; } } catch (MailException e) { Reply error = request.makeReply(HTTP.NOT_FOUND) ; error.setContent ("<h1>Messaggio non inviato</h1><h2>" + e.getMessage() +"</h2>"); return error; } String referer = request.getField("referer"); Reply reply = request.makeReply(HTTP.OK) ; reply.setContent ("<h1>Messagge OK</h1>" + "<h2>Return to <a href=" + referer + ">" + referer + "</a></h2>"); return reply; // return replyFile(request, data); } } ----------------------------------------------------------------- Pasquale Di Feo e-mail: pasdif@netfly.it NetFLY by Area Informatica s.a.s. http://www.netfly.it Via Guido Cucci, 32 tel. +39 81 5172811 84014 Nocera Inferiore (SA) +39 81 5171188 ITALY fax +39 81 928273 cel. +39 347 3328693 -----------------------------------------------------------------
Received on Friday, 26 July 1996 13:10:50 UTC