0 Replies Latest reply on Nov 22, 2012 8:40 AM by armun

    JBoss 5.0.1 GA mail-service jndi

    armun

      Hi i want send a mail from localhost.

      My mail-service.xml:

       

      <?xml version="1.0" encoding="UTF-8"?>

      <!-- $Id: mail-service.xml 62350 2007-04-15 16:50:12Z dimitris@jboss.org $ -->

      <server>

       

       

        <!-- ==================================================================== -->

        <!-- Mail Connection Factory                                              -->

        <!-- ==================================================================== -->

       

       

        <mbean code="org.jboss.mail.MailService"

               name="jboss:service=Mail">

          <attribute name="JNDIName">java:/Mail</attribute>

          <attribute name="User">nobody</attribute>

          <attribute name="Password">password</attribute>

          <attribute name="Configuration">

            <!-- A test configuration -->

            <configuration>

              <!-- Change to your mail server prototocol -->

              <property name="mail.store.protocol" value="pop3"/>

              <property name="mail.transport.protocol" value="smtp"/>

       

       

              <!-- Change to the user who will receive mail  -->

              <property name="mail.user" value="nobody"/>

       

       

       

              <!-- Change to the mail server  -->

              <property name="mail.pop3.host" value="pop3.nosuchhost.nosuchdomain.com"/>

       

       

              <!-- Change to the SMTP gateway server -->

              <property name="mail.smtp.host" value="localhost"/>

             

              <!-- The mail server port -->

              <property name="mail.smtp.port" value="25"/>

             

              <!-- Change to the address mail will be from  -->

              <property name="mail.from" value="nobody@nosuchhost.nosuchdomain.com"/>

       

       

              <!-- Enable debugging output from the javamail classes -->

              <property name="mail.debug" value="false"/>

            </configuration>

          </attribute>

          <depends>jboss:service=Naming</depends>

        </mbean>

       

       

      </server>

       

       

       

      my java class using JNDI java:/Mail

       

      package it;

       

       

       

       

       

       

       

       

       

                import java.util.Properties;

       

       

                import javax.mail.Message;

      import javax.mail.Session;

      import javax.mail.Transport;

      import javax.mail.internet.InternetAddress;

      import javax.mail.internet.MimeMessage;

      import javax.naming.InitialContext;

       

       

      import com.sun.mail.smtp.SMTPTransport;

       

       

                public class SendEmail {

       

       

                  public static void main(String args[]) throws Exception {

       

       

                  String host = "localhost";

       

       

                

                  String from = "test@localhost";

                  String to = "anemail@email.it";

       

       

                  // Get system properties

                    Properties properties = System.getProperties();

                   

                  

                   

       

       

                  // Setup mail server

       

       

                   properties.setProperty("mail.smtp.host", host);

       

       

       

       

                

       

       

                 

       

       

      InitialContext ctx = new InitialContext();

       

       

      Session session = (Session) ctx.lookup("java:/Mail");

       

       

       

       

       

       

       

       

       

       

                    // Create a default MimeMessage object.

                  MimeMessage message = new MimeMessage(session);

       

       

                  // Set the RFC 822 "From" header field using the

                  // value of the InternetAddress.getLocalAddress method.

                  message.setFrom(new InternetAddress(from));

       

       

                  // Add the given addresses to the specified recipient type.

                  message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

       

       

                  // Set the "Subject" header field.

                  message.setSubject("hi..!");

       

       

                  // Sets the given String as this part's content,

                  // with a MIME type of "text/plain".

                  message.setText("Hi ......");

                 

                  message.setContent ("<h1>Hello world</h1>", "text/html");

       

       

       

       

       

       

                  Transport.send(message);

                   System.out.println("Message Send.....");

                  }

      }

       

      the output :

       

       

      Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:/Mail' in SerialContext  [Root exception is javax.naming.NamingException: Invocation exception: Got null ComponentInvocation ]

                at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:439)

                at javax.naming.InitialContext.lookup(Unknown Source)

                at it.SendEmail.main(SendEmail.java:69)

      Caused by: javax.naming.NamingException: Invocation exception: Got null ComponentInvocation

                at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.getComponentId(GlassfishNamingManagerImpl.java:722)

                at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:639)

                at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:150)

                at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:425)

                ... 2 more

       

       

      I think localhost port 25 is disabled.

      If i start another server like "freeSMTP server" it sent mail.

      I can configure mail-service.xml or enable port 25 at localhost?

      i m only interested to send, not receive email. So i'm interested to SMTP not POP3.