[jboss-user] [Beginners Corner] - Couldn't connect to SMTP server

lema do-not-reply at jboss.com
Mon May 26 07:05:34 EDT 2008


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();
    	}
    }          
}





View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4153322#4153322

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4153322



More information about the jboss-user mailing list