The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Mail from iPlanet

6 replies on 1 page. Most recent reply: Feb 19, 2003 10:32 PM by Prasanna

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Prasanna

Posts: 11
Nickname: pras
Registered: May, 2002

Java Mail from iPlanet Posted: Feb 10, 2003 4:47 AM
Reply to this message Reply
Advertisement
Hi All,
I'm trying to send mail from a servlet deployed in iPlanet webserver 6.0 but without any success.The puzzling part is it dows not throw any error.Please help.The same code works when called from a standalone java application.So I guess it's something to do with iPlanet settings.
Pls help if u can.Thanx,
pras


David Resseguie

Posts: 5
Nickname: dressegu
Registered: Feb, 2003

Re: Java Mail from iPlanet Posted: Feb 13, 2003 2:48 PM
Reply to this message Reply
Maybe you can post some of the code you're using?

A Arun Prabhu

Posts: 1
Nickname: arunprabhu
Registered: Feb, 2003

Re: Java Mail from iPlanet Posted: Feb 18, 2003 9:52 PM
Reply to this message Reply
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>

David Resseguie

Posts: 5
Nickname: dressegu
Registered: Feb, 2003

Re: Java Mail from iPlanet Posted: Feb 18, 2003 9:57 PM
Reply to this message Reply
I'm working on something similar, but using JavaMail. It's very straight forward and compact. I haven't tried running it from within iPlanet yet, but that's on the todo list for tomorrow. Everything works fine from a stand-alone app.

Prasanna

Posts: 11
Nickname: pras
Registered: May, 2002

Re: Java Mail from iPlanet Posted: Feb 19, 2003 12:07 AM
Reply to this message Reply
> Hi All,
> I'm trying to send mail from a servlet deployed in iPlanet
> webserver 6.0 but without any success.The puzzling part is
> it dows not throw any error.Please help.The same code
> works when called from a standalone java application.So I
> guess it's something to do with iPlanet settings.
> Pls help if u can.Thanx,
> pras

My code is similar to the other standard java mail code found in on the net.This code works fine from a standalone application.But problem is when it is called from a selevelet.So I guess it's got something to do with settings.One thing I wanted to ask,where have u put the mail.jar and activation.jar and what r ur classpath setting?
Thanx for any help.

prasanna

David Resseguie

Posts: 5
Nickname: dressegu
Registered: Feb, 2003

Re: Java Mail from iPlanet Posted: Feb 19, 2003 5:48 AM
Reply to this message Reply
I put most jar files like that in [iPlanetDirectory]/bin/https/jre/lib/ext. (I think that's the correct path - off the top of my head) Then I add that to the classpath in jvm.conf. If the classpath is your problem you should be getting errors like "ClassNotFound" in the server's errors log. ([iPlanetDirectory]/https-[serverName]/logs/errors) Is that the problem you're having? What exactly do you mean by "not working"?

Like I mentioned, I haven't incorporated my JavaMail classes into the servlet yet, but that's my plan today. I'll let you know if I have any particular problems.

Prasanna

Posts: 11
Nickname: pras
Registered: May, 2002

Re: Java Mail from iPlanet Posted: Feb 19, 2003 10:32 PM
Reply to this message Reply
Here is the code I'm using
---------------------------------------------------------
String SMTP_HOST=l_udAudit.getParamValue("SMTP_HOST");

if(SMTP_HOST == null)
SMTP_HOST="x.x.x.x";


boolean l_bMailStatus = false;
FileOutputStream fos=null;
DataOutputStream dataout=null;
try
{

//////////////////////////////

fos = new FileOutputStream("C:\\DEBUG\\debug_mail"+str+".log");
dataout = new DataOutputStream(fos);//The error log file

///////////////////////////////////////
String l_strSmtpHost = SMTP_HOST; //Name of the mail server
dataout.writeBytes("\n New ACMail 11/02/2003 called for "+SMTP_HOST+" To :"+av_strTo+" Sub : "+av_strSubject+"Mesg : "+av_strMessageText);
String l_strFrom = "Interface"; //Sender's name
StringTokenizer l_stknTo = new StringTokenizer(av_strTo,",");//To separate individual recipients from the coma separated string
ArrayList l_arralstRecipients=new ArrayList();//To contain the muptiple addresses
ArrayList l_arralstRecipientsInternetAdd=new ArrayList();//To contain the multiple Interntaddress objects

int l_i_noOfRecepients=0; //variable to count the no of recipients

System.out.println("Configuring the mail session for : "+l_strSmtpHost);
dataout.writeBytes("\n Configuring the mail session for New"+SMTP_HOST);


dataout.writeBytes("\n ACMail B4 string tokenizing recepeints New"+SMTP_HOST);
//Populate the array list with the individual mail ids
while(l_stknTo.hasMoreTokens())
{
dataout.writeBytes("\n ACMail inside string tokenizing recepeints New"+SMTP_HOST);
String st=(String)l_stknTo.nextElement();
l_arralstRecipients.add(l_i_noOfRecepients,"raymund@lta-corenet-c1");
l_i_noOfRecepients++;
}
System.out.println("From the string tokenizer "+l_i_noOfRecepients);
dataout.writeBytes("\n ACMail From the string tokenizer New"+l_i_noOfRecepients);

InternetAddress[] address = new InternetAddress[l_i_noOfRecepients];

for(int i=0; i < address.length;i++)
{
dataout.writeBytes("\n ACMail Inside for loop"+i);
address=new InternetAddress((String)l_arralstRecipients.get(i));
}

//System.out.println("Internet address "+address+" length "+address.length);
dataout.writeBytes("\n ACMail Outside for loop");


//System.out.println("In ACMail : Number of recepients"+l_arralstRecipients.size());
dataout.writeBytes("\n In ACMail : Number of recepients"+l_arralstRecipients.size());

Properties l_proMailproperties=new Properties();

l_proMailproperties.put("mail.smtp.host",l_strSmtpHost);
l_proMailproperties.put("mail.smtp.allow8bitmime", "true");
Session l_sessMailSession=Session.getDefaultInstance(l_proMailproperties,null);
l_sessMailSession.setDebug(true);
dataout.writeBytes("\n Constructing the message from " +l_strFrom + " to "+av_strTo +" length "+av_strTo.length());


//Construct the Internet address objects for the sender and the receiver(s)
dataout.writeBytes("\n Constructing the message from 1");
InternetAddress l_iaFromAddress = new InternetAddress(l_strFrom);
dataout.writeBytes("\n Constructing the message from 2");

//Construct Internet address object for each recipient and put these in the array list
for(int ctr=0; ctr<l_arralstRecipients.size() ;ctr++)
{
dataout.writeBytes("\n In ACMail : Adding recepient1 "+(String)l_arralstRecipients.get(ctr));
l_arralstRecipientsInternetAdd.add(ctr,new InternetAddress((String)l_arralstRecipients.get(ctr)));
}

//The mime message object that contains the
Message l_mmMessage=new MimeMessage(l_sessMailSession);
l_mmMessage.setFrom(l_iaFromAddress);

//add the recipient Internet address objects to the mime object
/*for(int ctr=0; ctr< l_arralstRecipientsInternetAdd.size();ctr++)
{
System.out.println("In ACMail : Adding recepient2 "+(InternetAddress)l_arralstRecipientsInternetAdd.get(ctr));
l_mmMessage.addRecipient(RecipientType.TO,(InternetAddress)l_arralstRecipientsI nternetAdd.get(ctr));
}
*/
//dataout.writeBytes("\n Constructing the message from 3");
// l_mmMessage.addRecipients(RecipientType.TO,address);
l_mmMessage.setRecipients(Message.RecipientType.TO, address);
dataout.writeBytes("\n Added the recepients");
l_mmMessage.setSentDate(new Date());
dataout.writeBytes("\n Sets the date");
l_mmMessage.setSubject(av_strSubject);
dataout.writeBytes("\n Sets the subject");
l_mmMessage.setText(av_strMessageText);
dataout.writeBytes("\n Sets the Message Text");//Note :EVERYTHING STOPS HERE DOES NOT GO FURTHER
Transport.send(l_mmMessage);
dataout.writeBytes("\n Sends the Message ");
l_bMailStatus=true;


}catch(MessagingException mex)//Handle excpetion
-------------------

The problem is that the logs file shows upto the line "Sets the Message Text"(can see that in the above code).But nothing happens after the line "Transport.send(l_mmMessage);".NO exception is printed also.I do not know what happens after that.Is there any easy way to debug this.I looked at the iPlanet error log but it says nothing.Please let me know if u come across anything new.I have put the jar files in the jre/ext dirctory under the iPlanet folder.
Thanx.
pras.

Flat View: This topic has 6 replies on 1 page
Topic: Servlet Help 2 Previous Topic   Next Topic Topic: Java compareTo method

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use