tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Bruce <chrisabr...@yahoo.com>
Subject Re: EMail from a Servlet
Date Fri, 24 Mar 2000 17:26:45 GMT
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import sun.net.smtp.*;
/**
 * Insert the type's description here.
 * Creation date: (03/17/2000 3:17:51 PM)
 * @author: 
 */
public class Webmail extends HttpServlet {
	public String msg;
	public java.lang.String msgFrom;
	public java.lang.String msgTo;
	public java.lang.String msgSubject;	
	public java.lang.String msgEmail;
	public java.lang.String msgName;
	public java.lang.String msgIP;
	public java.lang.String msgNextPage;
/**
 * Webmail constructor comment.
 */
public Webmail() {
	super();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
	res.setContentType("text/html");	
	getParameters(req);
	if(!sendMail()){
		res.sendError(res.SC_INTERNAL_SERVER_ERROR, "An error occurred while
attempting to access the mail server.");
		return;
	}
	//Send Ack to Browser
	res.sendRedirect(msgNextPage);
}
/**
 * Insert the method's description here.
 * Creation date: (03/17/2000 3:35:46 PM)
 * @param req javax.servlet.http.HttpServletRequest
 */
private void getParameters(HttpServletRequest req) throws
ServletException, IOException {
	this.msgSubject = req.getParameter("txtSubject");

	//Make sure message is addressed to Yourown Domain
	if (req.getParameter("txtTo").indexOf("yourdomain.com") == -1) {
		this.msgTo = "you@yourdomain.com";
	} else {
		this.msgTo = req.getParameter("txtTo");
	}
	this.msgEmail = req.getParameter("txtEmail");
	this.msgIP = req.getRemoteAddr();
	this.msgName = req.getParameter("txtName");
	this.msgFrom = this.msgName + " <" + this.msgEmail + ">";
	this.msg = req.getParameter("txtMessage");
	this.msgNextPage = req.getParameter("txtNextPage");
}
/**
 * Insert the method's description here.
 * Creation date: (03/17/2000 3:54:07 PM)
 * @return boolean
 */
private boolean sendMail() {
	PrintStream out;
	SmtpClient send;

	try{
		send = new SmtpClient("mail.webslate.com");
		send.from(this.msgEmail);
		send.to(this.msgTo);

		out = send.startMessage();
		out.println("From: " + this.msgFrom);
		out.println("IP: " + this.msgIP);
		out.println("To: " + this.msgTo);
		out.println("Subject: " + this.msgSubject);
		out.println("\n--------------------\n");
		out.println(this.msg);
		out.println("\r\n");
		out.flush();
		out.close();
		send.closeServer();
	} catch (IOException e){
		log("Error occured while sending mail: " +  e);
		return false;
	}
	
	return true;
}
}

--- Peter Harrison <peterha@nothingbutnet.co.nz> wrote:
> Is there a facility or example somewhere for a webpage to email
> gateway,
> like the ones for CGI?  I have the CGI ones working, but would like
> to
> intergrate email into a web app I am writing with Servlets.
> 
> 
>
--------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org
> 
> 

__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

Mime
View raw message