This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes need help for mailer servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "need help for mailer servlet" Watch "need help for mailer servlet" New topic
Author

need help for mailer servlet

bibin sk
Greenhorn

Joined: Mar 04, 2001
Posts: 6
sir
ihave copied one mailer servlet from one bookwhen clicking the submit button only it goes to error page.ie mail is not sent.please help me .i have given the mailserver as "rediffmail.com.is there any error in that.
please help me
my coding for html is
<html>
<head><title>MAIN FORM</title></head>
<BODY>
<FORM ACTION="http://localhost:8080/servlet/mailing" METHOD="POST">
<p>To: <INPUT TYPE="TEXT" NAME="To" SIZE="25"></P>
<p>From: <INPUT TYPE="TEXT" NAME="From" SIZE="25"></P>
<p>Subject:<INPUT TYPE="TEXT" NAME="Subject" SIZE="25"></P>
<p>Message:</p>
<p><TEXTAREA NAME="Message" ROWS="5" COLS="70"></TEXTAREA></p>
<INPUT TYPE="HIDDEN" NAME="NextPage" VALUE="http://localhost:8080/mailsent.html">
<INPUT TYPE="HIDDEN" NAME="ErrorPage" VALUE="http://localhost:8080/mailerror.html">
<p><INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit">
<INPUT TYPE="RESET" NAME="Reset" VALUE="Reset">
</FORM>
</BODY>
</html>

*****************************************************************CODING FOR SERVLET IS
****************************************************************
package onlineshop;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;

public class FormMailer extends HttpServlet{
private static final int PORT=25;
private static final int LINELENGTH=72;
private static final String MAILSERVER="rediffmail.com";

public void init(ServletConfig config) throws ServletException{
super.init(config);
}

public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException{
String from,to,subject,message,host;
String nextPage,errorPage;
host=req.getServerName();
from=req.getParameter("From");
to=req.getParameter("To");
subject=req.getParameter("Subject");
message=req.getParameter("Message");
message=wordWrap(message);
nextPage=req.getParameter("NextPage");
errorPage=req.getParameter("ErrorPage");
if (sendMail(from,to,subject,message,host)){
res.sendRedirect(nextPage);
}
else{
res.sendRedirect(errorPage);
}
}
boolean sendMail(String from,String to,String subject,String message,String host){
String line;
Socket mailSocket;
BufferedReader mailIn;
PrintWriter out=null;

try{
mailSocket=new Socket(MAILSERVER,PORT);
out= new PrintWriter(new OutputStreamWriter(mailSocket.getOutputStream()));
//out= new PrintWriter(new OutputStreamWriter(mailSocket.getOutputStream()));
mailIn=new BufferedReader(new InputStreamReader(mailSocket.getInputStream()));
if(!checkResponse(mailIn,"220")){
return false;
}
out.print("HELLO"+host+"\r\n");
out.flush();
if(!checkResponse(mailIn,"250")){
return false;
}
out.print("MAIL FROM:"+from+"\r\n");
out.flush();
if(!checkResponse(mailIn,"250")){
return false;
}
out.print("RCPT TO: " +to+"\r\n");
out.flush();
if(!checkResponse(mailIn,"250")){
return false;
}
out.print("DATA\r\n");
out.flush();
if(!checkResponse(mailIn,"354")){
return false;
}
out.print("Date: "+ new Date().toString()+"\r\n");
out.flush();
out.print("From: "+from+"\r\n");
out.flush();
out.print("Subject: "+subject+"\r\n\r\n");
out.flush();
StringTokenizer stMessage= new StringTokenizer(message,"\n");

while(stMessage.hasMoreTokens()){
line=stMessage.nextToken();
if(line.equals(".")){
line=":";
}
out.println(line);
out.flush();
}
out.print("\r\n.\r\n");
out.flush();
if(!checkResponse(mailIn,"250")){
return false;
}
out.print("QUIT\r\n");
out.flush();
if(!checkResponse(mailIn,"221")){
return false;
}
}catch(UnknownHostException e){return false;}
catch(IOException e){return false;}

finally{
try{
out.close();
}catch (Exception e){}
}
return true;
}

boolean checkResponse(BufferedReader mailIn,String valid){
String line;
try{
line=mailIn.readLine();
if(!line.startsWith(valid)){ return false;}
}catch(IOException e){return false;}
return true;
}

String wordWrap(String message){
String word;
int column=0,length;
StringBuffer messageBuffer= new StringBuffer();
StringTokenizer words=new StringTokenizer(message," ");
while(words.hasMoreTokens()){
word=words.nextToken();
length=word.length();
if(column>0 && (column+length)>LINELENGTH){
messageBuffer.append("\n"+word+" ");
column=length+1;
}
else if (word.endsWith("\n")){
messageBuffer.append(word);
column=0;
}
else{
messageBuffer.append(word+" ");
column+=length+1;
}
}
return messageBuffer.toString();
}

public String getservletInfo(){
return"servlet sends email based on info from HTML form";
}
}




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help for mailer servlet
 
Similar Threads
MMSC test
Sending mail
Wanted: Minimal Web Server Example
Please Help, why this servlet is not working on mycgiserver
IO Exception :Connection timed out