A Arun Prabhu
Posts: 1
Nickname: arunprabhu
Registered: Feb, 2003
|
|
Re: Java Mail from iPlanet
|
Posted: Feb 18, 2003 9:52 PM
|
|
Hey i have code it works well in Iplanet server. mail me to get the code. my id is arun@4sight-tech.com //////////////////////////////////////////////////////// C ode:
import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;
public class SendMailServlet extends HttpServlet {
private static final int SMTP_PORT = 25; private static final char SMTP_ERROR_CODE1 = '4'; private static final char SMTP_ERROR_CODE2 = '5';
public String getServletInfo() { return "SendMailServlet 1.0"; }
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String host; String domain; String sender; String recipients; String subject; String mailtext; String maildata; Vector sessionTrace = new Vector(20);
host = getParameter(request, "host"); domain = getParameter(request, "domain"); sender = getParameter(request, "sender"); recipients = getParameter(request, "recipients"); subject = getParameter(request, "subject"); mailtext = getParameter(request, "mailtext");
try { // Send the mail.
maildata = "Date: " + (new Date()).toString() + "\r\n" + "From: " + sender + "\r\n" + "To: " + recipients + "\r\n" + "Subject: " + subject + "\r\n" + "\r\n" + mailtext + "\r\n"; sendMail(host, domain, sender, recipients, subject, maildata, sessionTrace);
// Send a response page to the client.
sendResponse( request, response, "The mail was queued for delivery.", sessionTrace); } catch(IOException theException) { // Send a response page to the client.
sendResponse(request, response, theException.toString(), sessionTrace); } }
protected String getParameter(HttpServletRequest request, String name) { String[] values; String result = "";
values = request.getParameterValues(name);
if (values != null) { result = values[0]; }
return result; }
protected void sendMail( String host, String domain, String sender, String recipients, String subject, String maildata, Vector sessionTrace) throws IOException {
Socket mailSocket; BufferedReader socketIn; DataOutputStream socketOut; String address; StringTokenizer tokenizer;
// Open the connection to the SMTP server, then get references to the // input and output streams.
mailSocket = new Socket(host, SMTP_PORT); socketIn = new BufferedReader( new InputStreamReader(mailSocket.getInputStream()) ); socketOut = new DataOutputStream(mailSocket.getOutputStream());
// Get the initial reply from the server.
readReply(socketIn, sessionTrace);
// Greet the server.
sendCommand(socketOut, "HELO " + domain, sessionTrace); readReply(socketIn, sessionTrace);
// Send the sender's address. sendCommand(socketOut, "MAIL FROM: " + sender, sessionTrace); readReply(socketIn, sessionTrace);
// Send the list of recipients.
tokenizer = new StringTokenizer(recipients, ",");
while (tokenizer.hasMoreElements()) { sendCommand(socketOut, "RCPT TO: " + tokenizer.nextToken(), sessionTrace); readReply(socketIn, sessionTrace); }
// Start the data section.
sendCommand(socketOut, "DATA", sessionTrace); readReply(socketIn, sessionTrace);
// Send the mail message. sendCommand(socketOut, maildata + ".", sessionTrace); readReply(socketIn, sessionTrace);
// End the session. sendCommand(socketOut, "QUIT", sessionTrace); readReply(socketIn, sessionTrace); }
private void sendCommand(DataOutputStream out, String command, Vector sessionTrace) throws IOException { out.writeBytes(command + "\r\n"); sessionTrace.addElement(command); // System.out.println(command); }
private void readReply(BufferedReader reader, Vector sessionTrace) throws IOException { String reply; char statusCode; reply = reader.readLine(); statusCode = reply.charAt(0); sessionTrace.addElement(reply); // System.out.println(reply);
if ( (statusCode == SMTP_ERROR_CODE1) | (statusCode == SMTP_ERROR_CODE2) ) { throw (new IOException("SMTP: " + reply)); } } protected void sendResponse( HttpServletRequest request, HttpServletResponse response, String statusMessage, Vector sessionTrace) throws IOException { ServletOutputStream out;
// Get a reference to the HTTP response writer.
response.setContentType("text/html"); out = response.getOutputStream(); // Send the header.
out.println("<html>"); out.println("<head>"); out.println("<title>" + getServletInfo() + "</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>" + getServletInfo() + "</h2>"); // Send the status message. out.println("<p>" + statusMessage); // Send the request parameters. out.println("<p><b>Request Parameters:</b>"); out.println("<pre>"); out.println("host = " + getParameter(request, "host")); out.println("domain = " + getParameter(request, "domain")); out.println("sender = " + getParameter(request, "sender")); out.println("recipients = " + getParameter(request, "recipients")); out.println("subject = " + getParameter(request, "subject")); out.println("mailtext = " + getParameter(request, "mailtext")); out.println("</pre>"); // Send the session trace. out.println("<p><b>Session Trace:</b>"); out.println("<pre>"); Enumeration e = sessionTrace.elements(); while (e.hasMoreElements()) { out.println((String) e.nextElement()); } out.println("</pre>");
// Send the footer. out.println("</body>"); out.println("</html>"); } }
Run this HTML..after complie of above...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- saved from url=(0070)http://gamelan.earthweb.com/journal/techworkshop/SendMailTest.html.txt --> <HTML><HEAD><TITLE>SendMail Test</TITLE> <META content="text/html; charset=windows-1252" http-equiv=Content-Type> <META content="MSHTML 5.00.2919.6307" name=GENERATOR></HEAD> <BODY BACKGROUND=../../bg.gif bgProperties=fixed> <H2>SendMail Test</H2> <FORM action=http://arun:81/servlet/SendMailServlet> <TABLE border=0> <TBODY> <TR> <TD>Host:</TD> <TD><INPUT name=host size=30 value=mail.inet.agcocorp.com></TD></TR> <TR> <TD>Domain:</TD> <TD><INPUT name=domain size=30 value=atl.agcocorp.com></TD></TR> <TR> <TD>From:</TD> <TD><INPUT name=sender size=30 value=t.rose@agcocorp.com></TD></TR> <TR> <TD>To:</TD> <TD><INPUT name=recipients size=50 value=t.rose@agcocorp.com></TD></TR> <TR> <TD>Subject:</TD> <TD><INPUT name=subject size=50 value=test></TD></TR> <TR> <TD vAlign=top>Text:</TD> <TD vAlign=top><TEXTAREA cols=50 name=mailtext rows=5>test</TEXTAREA></TD></TR></TBODY></TABLE&g t; <P><INPUT name=sendButton type=submit value=Send> </FORM></P></BODY></HTML>
|
|