tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Asim Alp <asim...@gmail.com>
Subject Re: sending a JSP page by email
Date Tue, 14 Dec 2004 23:03:09 GMT
Here is what I use:


// here is a function from my MailUtils class
	public boolean formSend(String to, String fromAddress, String fromPersonal,
			String msgSubject, PageContext pc, String templateURL) {
		if (templateURL == null || templateURL.equals("")) { return formSend(to,
				msgSubject, pc); }

		String httpHost = pc.getRequest().getServerName();
		Enumeration params = pc.getRequest().getParameterNames();

		String queryString = "?";
		if (templateURL.indexOf("?") >= 0) queryString = "&";
		while (params.hasMoreElements()) {
			String param = (String) params.nextElement();

			try {
				queryString += param
						+ "="
						+ URLEncoder.encode(StringUtils.encodeHTML(JSPUtils.getParam(pc,
								param)), "UTF-8") + "&";
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String url = "http://" + httpHost + templateURL + queryString;

		try {
                        // see the Webpage object
			Webpage wp = new Webpage(url);
			String msgBody = wp.loadPage();

                        // replace it your own send mail function
			send(to, fromAddress, fromPersonal, msgSubject, msgBody, true);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}


// Here is the Webpage
package com.edunet.iwas.util;

import java.net.*;
import java.io.*;

public class Webpage {

	private static final String NEW_LINE = System.getProperty("line.separator");

	private String content;

	private URL u;

	public Webpage(String urlString) throws MalformedURLException {
		urlString = StringUtils.replace(urlString, " ", "%20");
		this.u = new URL(urlString);
	}

	public String loadPage() throws IOException {
		BufferedReader in = new BufferedReader(
				new InputStreamReader(u.openStream()));

		String temp;
		content = "";
		while ((temp = in.readLine()) != null)
			content += temp + NEW_LINE;
/* you don't really need the NEW_LINE */

		in.close();

		return content;
	}

	public String getContent() {
		return content;
	}
}


Please use it at your own risk...

Asim

On Tue, 14 Dec 2004 19:15:13 -0300, Gabriel Belingueres
<belingueres@gmail.com> wrote:
> Hi,
> 
> How can I take advantage of Tomcat's JSP processing engine to use a
> JSP page as a template for an email?
> 
> That is, I want to do something like a page forwarding from a servlet,
> but this forwarding process the JSP page and, instead of send it to
> the browser, it send it by email to somebody.
> 
> Thanks in advance,
> Gabriel
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message