0 Replies Latest reply on May 26, 2008 7:05 AM by sara isa

    Couldn't connect to SMTP server

    sara isa Newbie

      hallo all,

      i'm trying to send email message through the code snippet below, but everytime it fails to connect to SMTP server. please, smb with experience, give a clue, what am i doing wrong?

      thanks in advance, lema.


      @Stateless(name="mail.sender")
      @Remote(MailSenderRemote.class)
      @Local(MailSenderLocal.class)
      public class MailSenderBean implements MailSenderLocal, MailSenderRemote {
      public static Session newMailSession()
      {
      try {
      // Object ctx = new InitialContext().lookup("java:/Mail");
      // return (Session)PortableRemoteObject.narrow(ctx, Session.class);
      Session ctx = (Session) new InitialContext().lookup("java:/Mail");
      return ctx;
      } catch (javax.naming.NamingException e) {
      e.printStackTrace();
      return null;
      }
      }

      public void sendMessage(String mailFrom,
      String mailTo, String subject, String msg, boolean htmlText)
      {
      try{
      Session session = newMailSession();
      Message message = new MimeMessage(session);
      if (mailFrom == null){
      mailFrom = session.getProperty("mail.from");
      message.setFrom( new InternetAddress(mailFrom));
      }

      String s[] = mailTo.split(";");
      InternetAddress to[] = new InternetAddress[s.length];
      /**
      for(int i=0; i<s.length; i++){
      to = new InternetAddress(s);
      }*/
      to[0] = new InternetAddress(mailTo);

      message.setRecipients(Message.RecipientType.TO, to);
      message.setSubject(subject);
      message.setSentDate(new Date());
      String textType;
      if (htmlText){
      textType = "text/html";
      }
      else {
      textType = "text/plain";
      }

      message.setContent(msg, textType);
      Transport trans = session.getTransport("smtp");
      // trans.connect("111.111.111.11", "", "");
      trans.connect("111.111.111.11",
      session.getProperty("mail.smtp.user"),
      session.getProperty("mail.smtp.password"));

      trans.sendMessage(message, message.getAllRecipients());
      trans.close();
      }
      catch (Exception e) {
      e.printStackTrace();
      }
      }
      }