Howdy, It's a good idea to encapsulate the mail sending functionality in a separate class, e.g. MyMailer, rather than in the servlet. That mailer would have a method like sendMessage(subject, from, to, contents), setHost(), setPort(), and maybe also reset(). It would take care of creating the Message, Session, talking to the Transport, etc.
I know the above doesn't answer your question ;) But it's a good idea anyways. That way you could reset the mailer on error and try again. Can you describe "intermittently" with more detail? E.g. does it always work the first time? By the way, why that no argument constructor in a servlet? Yoav Shapira Millennium ChemInformatics >-----Original Message----- >From: Rob Cartier [mailto:[EMAIL PROTECTED]] >Sent: Wednesday, June 26, 2002 10:53 AM >To: [EMAIL PROTECTED] >Subject: help requested - javax.mail.SendFailedException: intermittently > >I am running tomcat 4.0.2 and jdk 1.4 on RedHat 7.2 >and every so often this peice of code > >import java.io.*; >import java.util.*; >import javax.mail.*; >import javax.mail.internet.InternetAddress; >import javax.mail.internet.MimeMessage; >import javax.servlet.*; >import javax.servlet.http.*; > >public class MailPremierApplication extends HttpServlet >{ > > public MailPremierApplication() > { > } > > public void doPost(HttpServletRequest request, HttpServletResponse >response) > throws IOException, ServletException > { > .... > .... > > PrintWriter writer = response.getWriter(); > response.setContentType("text/html"); > writer.println("<html>"); > writer.println("<head>"); > writer.println("<title>Mail Sending Results</title>"); > writer.println("</head>"); > writer.println("<body bgcolor=\"white\">"); > try > { > Properties props = new Properties(); > props.put("xxxxxx", "xxxxx.com"); > Session mailConnection = Session.getInstance(props, null); > Message msg = new MimeMessage(mailConnection); > javax.mail.Address to_whom = new InternetAddress(to); > javax.mail.Address from_who = new InternetAddress(from); > msg.setContent(full_msg, "text/plain"); > msg.setFrom(from_who); > msg.setRecipient(javax.mail.Message.RecipientType.TO, to_whom); > msg.setSubject(subject); > Transport.send(msg); > writer.println("<strong>Message successfully sent!</strong>"); > } > catch(Throwable t) > { > writer.println("<font color=\"red\">"); > writer.println("ENCOUNTERED EXCEPTION: " + t); > writer.println("<pre>"); > System.out.println("ENCOUNTERED EXCEPTION: " + t); > t.printStackTrace(writer); > writer.println("</pre>"); > writer.println("</font>"); > writer.println("</body>"); > writer.println("</html>"); > > } > > >generates an error occasionally > >ENCOUNTERED EXCEPTION: javax.mail.SendFailedException: Sending failed; > nested exception is: > javax.mail.MessagingException: Could not connect to SMTP host: >xxxxxx.com, >port: 25; > nested exception is: > java.net.ConnectException: Connection refused > > >In order to fix this I need to restart tomcat and all is once again ok. > >Any and all ideas are welcome > > >Robert Cartier >112 Fieldbrook Rd >Middletown, Ct. 06457-1746 > > > > > >-- >To unsubscribe, e-mail: <mailto:tomcat-user- >[EMAIL PROTECTED]> >For additional commands, e-mail: <mailto:tomcat-user- >[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
