Java Process hungs when sending mails, without throwing any exception at all.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, July 10, 2002 08:07 PM

Hi. I'm having a problem when sending mails width JDK 1.4 on Solaris 8. The process hungs at the "transport.sendMessage(msg, addrVec);". The problem is that it happens in a random way, not always Even when I try to do a kill -3 to print the stack, there's no answer... Below is the class that sends mails import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.util.*; import java.io.*; public class SendMail { //-------------------------------------------------------------------- private Transport transport; private Session session; private String server; private int    More>>

Hi. I'm having a problem when sending mails width JDK 1.4 on Solaris 8. The process hungs at the "transport.sendMessage(msg, addrVec);". The problem is that it happens in a random way, not always


Even when I try to do a kill -3 to print the stack, there's no answer...


Below is the class that sends mails


			
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
import java.io.*;

public class SendMail
{
//--------------------------------------------------------------------
private Transport transport;
private Session session;
private String server;
private int maxToDisconnect;
private int sentMails = 0;
private boolean debug = false;
//--------------------------------------------------------------------
/*
*Se conecta al servidor smtp
*@param server Direccion IP del servidor smtp
*@param maxToDisconnect Cantidad máxima de mails a enviar
*antes de desconectarse
*/
public SendMail(String server, int maxToDisconnect)
throws NoSuchProviderException,
MessagingException
{
config(server, maxToDisconnect);
}
//--------------------------------------------------------------------
public SendMail(String server, int maxToDisconnect, boolean debug)
throws NoSuchProviderException,
MessagingException
{
this.debug = debug;
config(server, maxToDisconnect);
}
//--------------------------------------------------------------------
protected void finalize()
throws Throwable
{
transport.close();
}
//--------------------------------------------------------------------
private void config(String server, int maxToDisconnect)
throws NoSuchProviderException,
MessagingException
{

//toma del dflt Session;
Properties props = System.getProperties();
props.put("mail.smtp.host", server);
session = Session.getInstance(props, null);

//debug
if (debug == true)
{
session.setDebug(true);
}
else session.setDebug(false);

//se conecta al servidor smtp
transport = session.getTransport("smtp");
transport.connect(server, "", "");

//setea la cantidad max de mails a enviar por conn
this.maxToDisconnect = maxToDisconnect;
this.server = server;
}
//--------------------------------------------------------------------
/*
*Envia el mensaje
*/
public void sendMsg(String Sender,
String Recipient,
String Subject,
String Body,
boolean is_html,
boolean is_multipart)

throws NoSuchProviderException,
MessagingException
{

//System.out.println("MAIL ENVIADO: "+Recipient);

//chequea si esta conectado
if (transport.isConnected() == false)
{
System.out.println("reconectando");
config(server, maxToDisconnect);
}

//si se paso de la cantidad maxima de
//mails a enviar se desconecta
//y se vuelve a conectar
if (sentMails > maxToDisconnect)
{
sentMails = 0;
transport.close();
System.out.println("reconectando");
config(server, maxToDisconnect);
}

//crea un nuevo mensaje
MimeMessage msg = new MimeMessage(session);

//setea el sender
InternetAddress addrSender = new InternetAddress(Sender);
msg.setFrom(addrSender);

//setea el recipient
InternetAddress[] addrVec = new InternetAddress[1];
addrVec[0] = new InternetAddress(Recipient);
//InternetAddress addrRecipient = new InternetAddress(Recipient);
msg.addRecipient(Message.RecipientType.TO, addrVec[0]);

//subject
msg.setSubject(Subject);

if (is_multipart==true && is_html == true)
{
//Multipart
MimeMultipart multipart = new MimeMultipart("alternative");


String bodyText, bodyHTML;
int flag_1 = Body.indexOf(" ");
bodyText = Body.substring(flag_1, flag_2);
bodyHTML = Body.substring(flag_2+3);

//Texto
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(bodyText, "text/plain");
System.out.println(bodyText);
multipart.addBodyPart(messageBodyPart);

//HTML
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(bodyHTML, "text/html; charset=iso-8859-1");
System.out.println(bodyHTML);
multipart.addBodyPart(messageBodyPart);

msg.setContent(multipart);
msg.saveChanges();

}else if (is_html == true)
{

//Mbody del mensaje
msg.setContent(Body, "text/html; charset=iso-8859-1");

//graba el mime
msg.saveChanges();
}else
{
//Mbody del mensaje
msg.setText(Body);

//graba el mime
msg.saveChanges();
}

//envia el mensaje
//transport.send(msg);
transport.sendMessage(msg, addrVec);

//incrementa los sent mails
sentMails++;
}
//--------------------------------------------------------------------
}
   <<Less

Re: Java Process hungs when sending mails, without throwing any exception at all.

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, July 11, 2002 07:22 AM

process hungs at the "transport.sendMessage(msg, addrVec);".



Could it be that the connection to your SMTP server is not responding, or is responding very very slowly?



Each step in the SMTP protocol transaction has an individual timeout, e.g. your client has to wait for the server's response to your initial TCP connection attempt, then wait for its response to your "EHLO/HELO", wait for its response to your "MAIL FROM", "RCPT TO", "DATA", and finally, wait for its response to your end of data indicator.



Your (the JavaMail API client) timeout for some of these may have very long defaults. I don't know what the default timeouts are in the JavaMail API, but with Sendmail, some of them are on the order of minutes or even hours, and sysadmins (I was an email Postmaster, for many years, in several previous lifetimes) often need to shorten the timeouts from the original defaults, on busy sites dealing with flaky remote connections etc.

Re: Java Process hungs when sending mails, without throwing any exception at all.

Posted By:   Gautam_Marwaha  
Posted On:   Wednesday, July 10, 2002 10:22 PM

Set session.setDebug(true) and see output. Chances are it takes time in socket connect to SMTP service.
About | Sitemap | Contact