2012-06-26 70 views
0

我想從mail-service.xml文件獲取會話時從jboss發送郵件。使用mail-service.xml文件從jboss發送電子郵件

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

這是我的郵件service.xml文件

<?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> 

,但我得到這個輸出話說認證失敗例外:

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 

我搜索了這個問題,並發現this鏈接。它表明應該使用Authenticator。但問題是,在鏈接代碼不使用從mail-service.xml文件的會話。一切都在程序本身進行硬編碼。在從mail-service.xml獲取會話時,我應該如何使用此身份驗證器?
編輯
我在mail-service.xml文件中分別向用戶和密碼屬性添加了「mail.smtp.user」和「mail.smtp.password」的值。它開始工作。

<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> 

但我已在 「mail.smtp.user」 和 「mail.smtp.password」 值,用戶名和密碼。爲什麼它無法從那裏讀取?它出於這個目的,對吧?

+0

是您mail.smtp.uesr [email protected]或只是XYZ?你應該給它[email protected] –

+0

@SandeepNair:我已經把它作爲[email protected] – Ashwin

回答

0

理想情況下,JBoss會設置會話爲您執行身份驗證,但如果無法正常工作,則可以使用Transport.connect方法指定用戶名和密碼。有關示例,請參閱JavaMail FAQ。

+0

我知道使用transport.connect()方法你可以明確地指定用戶名,密碼和端口號。但我想知道我在做什麼錯 – Ashwin

0

是mail.smtp.password有效的配置屬性還在嗎?我知道老版本的文檔有它,但它似乎已從http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html中刪除。我想當它被刪除時,大部分設置管理和認證JavaMail會話的文檔都停止工作。

+0

JavaMail不允許您使用屬性指定密碼。 JBoss可能會處理財產並做正確的事情,但我不知道。我認爲這實際上是一個比JavaMail問題更多的JBoss問題。 –

0

在郵件service.xml中的服務器內試試這個

<?xml version="1.0" encoding="UTF-8"?> 
<!-- $Id: mail-service.xml 62350 2007-04-15 16:50:12Z [email protected] $ --> 
<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> 
相關問題