1

I am trying to send mail from jboss while obtaining the session from mail-service.xml file.

javax.naming.Context ictx = new javax.naming.InitialContext();
       Session mailSession = (Session) ictx.lookup("java:/Mail"); 

This is my mail-service.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!--DOCTYPE server-->
<!-- $Id: mail-service.xml,v 1.5 2003/08/27 04:34:12 patriot1burke Exp $ -->

<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">
       <!-- Test -->
       <configuration>

          <property name="mail.smtp.starttls.enable" value="true"/>
          <property name="mail.smtp.host" value="smtp.gmail.com"/>
          <property name="mail.smtp.user" value="***"/>
          <property name="mail.smtp.password" value="***"/>
          <property name="mail.smtp.port" value="587"/>
          <property name="mail.smtp.auth" value="true"/>
          <property name="mail.debug" value="true"/>
       </configuration>
    </attribute>
  </mbean>

</server>

But I get this output saying authentication failed exception :

16:29:37,562 INFO  [STDOUT] 220 mx.google.com ESMTP ku7sm11540452pbc.31
16:29:37,578 INFO  [STDOUT] DEBUG SMTP: connected to host "smtp.gmail.com", port
: 587
16:29:37,578 INFO  [STDOUT] EHLO cl-4
16:29:38,000 INFO  [STDOUT] 250-mx.google.com at your service, [101.63.150.159]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
16:29:38,000 INFO  [STDOUT] DEBUG SMTP: Found extension "SIZE", arg "35882577"
16:29:38,000 INFO  [STDOUT] DEBUG SMTP: Found extension "8BITMIME", arg ""
16:29:38,000 INFO  [STDOUT] DEBUG SMTP: Found extension "STARTTLS", arg ""
16:29:38,000 INFO  [STDOUT] DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", a
rg ""
16:29:38,000 INFO  [STDOUT] STARTTLS
16:29:38,453 INFO  [STDOUT] 220 2.0.0 Ready to start TLS
16:29:38,718 INFO  [STDOUT] EHLO cl-4
16:29:40,750 INFO  [STDOUT] 250-mx.google.com at your service, [101.63.150.159]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
16:29:40,750 INFO  [STDOUT] DEBUG SMTP: Found extension "SIZE", arg "35882577"
16:29:40,765 INFO  [STDOUT] DEBUG SMTP: Found extension "8BITMIME", arg ""
16:29:40,765 INFO  [STDOUT] DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN
 XOAUTH"
16:29:40,765 INFO  [STDOUT] DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", a
rg ""
16:29:40,765 INFO  [STDOUT] DEBUG SMTP: Attempt to authenticate
16:29:40,765 INFO  [STDOUT] AUTH LOGIN
16:29:41,187 INFO  [STDOUT] 334 VXNlcm5hbWU6
16:29:41,187 INFO  [STDOUT] bm9b2R5
16:29:41,625 INFO  [STDOUT] 34 UGFzc3dvcmQ6
16:29:41,625 INFO  [STDOUT] cFzc3dvcmQ=
16:29:42,062 INFO  [STDOUT] 535-5.7.1 Username and Password not accepted. Learn
more at
535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 ku7sm1154045
2pbc.31
16:29:42,062 INFO  [STDOUT] in sendmail1 javax.mail.AuthenticationFailedExceptio
n

I searched for this problem and found this link. It suggests that an Authenticator should be used. But the problem is that in the link the code does not use a session from mail-service.xml file. Everything is hardcoded in the program itself. How should I use this Authenticator when obtaing the session from mail-service.xml?
EDIT
I added the value of "mail.smtp.user" and "mail.smtp.password" to the user and password attributes respectively in the mail-service.xml file. It started working.

<server>

  <!-- ==================================================================== -->
  <!-- Mail Connection Factory                                              -->
  <!-- ==================================================================== -->

  <mbean code="org.jboss.mail.MailService"
         name="jboss:service=Mail">
    <attribute name="JNDIName">java:/Mail</attribute>
    <attribute name="User">***</attribute>
    <attribute name="Password">***</attribute>
    <attribute name="Configuration">
       <!-- Test -->
       <configuration>

          <property name="mail.smtp.starttls.enable" value="true"/>
          <property name="mail.smtp.host" value="smtp.gmail.com"/>
          <property name="mail.smtp.user" value="***"/>
          <property name="mail.smtp.password" value="***"/>
          <property name="mail.smtp.port" value="587"/>
          <property name="mail.smtp.auth" value="true"/>
          <property name="mail.debug" value="true"/>
       </configuration>
    </attribute>
  </mbean>

</server><br>

But I have already given the user and password in the "mail.smtp.user" and "mail.smtp.password" values. Why is it not able to read it from there? Its there for that purpose, right?

Community
  • 1
  • 1
Ashwin
  • 12,691
  • 31
  • 118
  • 190

3 Answers3

0

Ideally JBoss would set up the Session to do the authentication for you, but if that's not working you can use the Transport.connect method to specify the username and password. See the JavaMail FAQ for examples.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • I know that using the transport.connect() method you can explicitly specify the username,password and port number. But I want to know what is wrong with what I am doing – Ashwin Jun 27 '12 at 04:52
0

Is mail.smtp.password a valid configuration property still? I know that older versions of the docs had it but it seems to have been removed from http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html. I think when it got removed most of the docs for setting up administered and authenticated JavaMail Sessions stopped working.

Chase
  • 3,123
  • 1
  • 30
  • 35
  • JavaMail doesn't allow you to specify passwords using a property. JBoss might handle the property and do the right thing, but I don't know. I think this is really a JBoss question more than a JavaMail question. – Bill Shannon Jun 28 '12 at 04:45
0

Try this in mail-service.xml within the server

<?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">type sender mail id here</attribute>
<attribute name="Password">type password</attribute>   
<attribute name="Configuration">
  <!-- A test configuration -->
  <configuration>
<property name="mail.smtp.auth" value="true"/>
    <property name="mail.smtp.starttls.enable" value="true"/>
    <property name="mail.smtp.socketFactory.class" value="javax.net.ssl.SSLSocketFactory"/>
    <!-- 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 SMTP gateway server -->
    <property name="mail.smtp.host" value="smtp.gmail.com"/>

    <!-- The mail server port -->
    <property name="mail.smtp.port" value="465"/>
    <!-- Change to the address mail will be from  -->
    <property name="mail.from" value=""/>

    <!-- Enable debugging output from the javamail classes -->
    <property name="mail.debug" value="false"/>
  </configuration>
</attribute>
<depends>jboss:service=Naming</depends>
</mbean>

</server>
Gayathri Rajan
  • 369
  • 1
  • 6
  • 18