2 Replies Latest reply on Aug 23, 2005 3:36 AM by cinimod

    Sending e-mail from bean

    cinimod

      Hi,

      within my stateful session bean i am using the following method, for sending an e-mail:

      private void sendMail(String subject, String receiver, String sender,
       String content, String mailbox, String password,
       String mailServer) {
       Password authenticator = new Password(mailbox, password);
       Properties sendProps = new Properties();
       sendProps.put("mail.smtp.host", mailServer);
       session = Session.getInstance(sendProps, authenticator);
       try{
       Message message = new MimeMessage(session);
       message.setFrom(new InternetAddress(sender));
       InternetAddress[] to = { new InternetAddress(receiver)};
       message.setRecipients(Message.RecipientType.TO, to);
       message.setSubject(subject);
       message.setSentDate(new Date());
       message.setText(content);
       Transport.send(message);
       }catch(MessagingException mex) { mex.printStackTrace(); }
      }
      


      but if i try sending i receive the following error:

      
      12:29:28,592 INFO [STDOUT] javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: -1
      12:29:28,592 INFO [STDOUT] at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1201)
      12:29:28,592 INFO [STDOUT] at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
      


      As you can see, the smtp host is set to localhost - but that is not what i set for the mailServer parameter, when i am calling up the method.

      Of course, i can set a resource-reference in the ejb-jar.xml respectively jboss.xml but i want the parameters to be different for each method call.

      Does anybody have a solution to this problem?

      Thanks for any advices!

        • 1. Re: Sending e-mail from bean
          genman


          For jboss, use JNDI to look up the "Session", e.g.

          Object o = new InitialContext().lookup("java:/Mail");
          Session s = (Session)s;

          Also, put your server properties in the mail-service.xml file.

          • 2. Re: Sending e-mail from bean
            cinimod

            but i want to set the serverproperties as servername and the password/username dynamically. If the user is connecting he provides these parameters.