On 7/21/05, Wes Bramhall <[EMAIL PROTECTED]> wrote:
> I've been investigating using Tapestry to create "pages" which will
> actually be sent as HTML e-mails. After doing some research, I found
> some things that helped get it working in a test case.
> 
> PageName page = (PageName) cycle.getPage("PageName");
> ByteArrayOutputStream out = new ByteArrayOutputStream();
> HTMLWriter writer = new HTMLWriter(out);
> cycle.activate(next);
> cycle.renderPage(writer);
> writer.flush();
> String HTMLBody = out.toString();
> 
> Unfortunately, this scheme doesn't work in some of the areas we are
> currently sending e-mail (during a form submit inside a component, etc.)
> I tried to get the current page before that block, CurPage curPage =
> (CurPage) cycle.getPage("CurPage");, and then cycle.activate(curPage);
> after that block, but that kept giving me "page locked after a
> commit();" type errors.

I managed to send an email even inside forms with 

  public static void sendEmail(IPage source, IPage page, IRequestCycle
cycle, String from, String recipient, String subject) {
    IForm cform = Form.get(cycle);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    HTMLWriter writer = new HTMLWriter(baos);
    cycle.activate(page);
    cycle.renderPage(writer);
    writer.flush();
    String semail = baos.toString();
    semail = semail.replaceAll("img src=\"/", "img src=\"http://";
        + cycle.getRequestContext().getServerName() + 
        ((cycle.getRequestContext().getServerPort() == 80)?"":(":"
        + cycle.getRequestContext().getServerPort())) + "/");
    semail = semail.replaceAll("href=\"/", "href=\"http://";
        + cycle.getRequestContext().getServerName() + 
        ((cycle.getRequestContext().getServerPort() == 80)?"":(":"
        + cycle.getRequestContext().getServerPort())) + "/");
    Context initCtx;
    try {
      initCtx = new InitialContext();
  
      Context envCtx = (Context) initCtx.lookup("java:comp/env");
      Session session = (Session) envCtx.lookup("mail/Session");
  
      MimeMultipart content = new MimeMultipart("alternative");
      //MimeBodyPart text = new MimeBodyPart();
      MimeBodyPart html = new MimeBodyPart();

      //content.addBodyPart(text);
      content.addBodyPart(html);
  
      Message message = new MimeMessage(session);
      html.setContent(semail,"text/html;charset=\"UTF-8\"");
      message.setContent(content); 
      message.setFrom(new InternetAddress(from));
      InternetAddress to[] = new InternetAddress[1];
      to[0] = new InternetAddress(recipient);
      message.setRecipients(Message.RecipientType.TO, to);
      message.setSubject(subject);
      message.setSentDate(new Date());
      //message.setContent(content);
      Transport.send(message);
      s_logger.debug("Message sent from " + from + " to " + recipient
+ ":" + subject);
  
    } catch (NamingException e) {
      s_logger.debug(e);
    } catch (AddressException e) {
      s_logger.debug(e);
    } catch (MessagingException e) {
      s_logger.warn(e);
    }
    page.detach();
    
    if (source != null) {
    cycle.activate(source);
    cycle.setAttribute(IForm.ATTRIBUTE_NAME,cform);
    }
  }

An official API to render pages anywhere inside a page for this
purpose would be really nice.

Henri.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to