I'm running into a weird problem in which the servlet tries to connect to
the localhost even though I'm explicitly specifying the hostname in the
code.  The error in the log file is:

2003-02-18 15:46:38 javax.mail.SendFailedException: Sending failed;
  nested exception is:
 class javax.mail.MessagingException: Could not connect to SMTP host:
localhost, port: 25;

The error is correct since I don't have an email server running on the
localhost.

Oddly enough, when I copy the same lines of code into a standalone java
class file all works as expected.  Could there be a setting somewhere in
Tomcat that's overriding my specified host name?

I don't run into this problem when accessing mail (using IMAP) on the same
host.

Here's the code

public class SendMailAction extends ValidAction {

    protected ActionForward doIt (
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response
    ) throws ServletException {
        HttpSession userSession = request.getSession();
        String status = "Your message was sent.";
        ServletContext ctx = servlet.getServletContext();

        try {

                String host = "synergy";
                String from = "[EMAIL PROTECTED]";
                String to = "[EMAIL PROTECTED]";

                // Get system properties
                Properties props = System.getProperties();

                // Setup mail server
                props.put("mail.smtp.host", host);

                // Get session
                javax.mail.Session smtp =
javax.mail.Session.getDefaultInstance(props, null);
                smtp.setDebug(true);

                // Define message
                MimeMessage message = new MimeMessage(smtp);

                // Set the from address
                message.setFrom(new InternetAddress(from));

                // Set the to address
                message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to));

                // Set the subject
                message.setSubject("Hello JavaMail");

                // Set the content
                message.setText("Welcome to JavaMail");

                // Send message
                Transport.send(message);
        } catch (Exception e) {
            status = e.toString();
            ctx.log(smtpHost + ":" + e.toString());
        }

        return mapping.findForward("email");
    }

Any ideas, anyone?


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to