2012-01-25 44 views
4

我正在使用java 6.0,Spring 3.0 & Maven。我面臨一個奇怪的問題。發送郵件在Spring/Maven/Java 6環境下不工作

我想從我的應用程序發送郵件,但無法這樣做。我檢查調試,日誌看起來很好 - 沒有例外/錯誤,但郵件不觸發。

相關代碼:

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMessage.RecipientType; 

public class JavaEmail 
{ 
    private final String SMTP_AUTH_USER = "[email protected]"; 
    private final String SMTP_AUTH_PWD = "secret"; 

    public void sendMain(String strFrom, String strTo, String strSubject, String  
strContent) throws MessagingException 
    { 
     Message message = new MimeMessage(getSession()); 

     message.addRecipient(RecipientType.TO, new InternetAddress(strTo)); 
     message.addFrom(new InternetAddress[] { new InternetAddress(strFrom)}); 

     message.setSubject(strSubject); 
     message.setContent(strContent, "text/plain"); 

     try { 
      Transport.send(message); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private Session getSession() { 
     Authenticator authenticator = new Authenticator(); 

     Properties properties = new Properties(); 
     properties.setProperty("mail.smtp.submitter", 
authenticator.getPasswordAuthentication().getUserName()); 
     properties.setProperty("mail.smtp.auth", "true"); 

     properties.setProperty("mail.smtp.host", "smtp.myorg.com"); 
     properties.setProperty("mail.smtp.port", "25"); 
     properties.setProperty("mail.debug", "true"); 

     return Session.getInstance(properties, authenticator); 
    } 

    private class Authenticator extends javax.mail.Authenticator 
    { 
     private PasswordAuthentication authentication; 

     public Authenticator() 
     { 
      String username = SMTP_AUTH_USER; 
      String password = SMTP_AUTH_PWD; 
      authentication = new PasswordAuthentication(username, password); 
     } 

     protected PasswordAuthentication getPasswordAuthentication() 
     { 
      return authentication; 
     } 
    } 


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

     JavaEmail email = new JavaEmail(); 

     email.sendMain("[email protected]", "[email protected]", "Say Hi ..", "Body"); 

    } 

} 

POM(相關):

<dependency> 
      <groupId>javax.mail</groupId> 
      <artifactId>mail</artifactId> 
      <version>1.4.1</version> 
     </dependency> 

日誌:

DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: trying to connect to host "smtp.myorg.com", port 25, isSSL false 
220 droutbound.logix.in ESMTP 
DEBUG SMTP: connected to host "smtp.myorg.com", port: 25 

EHLO ABCDE1234 
250-droutbound.logix.in 
250-8BITMIME 
250-SIZE 26214400 
250-STARTTLS 
250-AUTH PLAIN LOGIN 
250 AUTH=PLAIN LOGIN 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "SIZE", arg "26214400" 
DEBUG SMTP: Found extension "STARTTLS", arg "" 
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN" 
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN" 
DEBUG SMTP: Attempt to authenticate 
AUTH LOGIN 
334 VXNl1cm25hbW2U6 
c2NjYWRta1W5Ae3mVuc42Fy5Lmlu 
334 UGFz7c63dv2cmQ6 
WmVuc22Fy5MT7IzIw== 
235 #2.0.0 OK Authenticated 
DEBUG SMTP: use8bit false 
MAIL FROM:&lt;[email protected]&gt; [email protected] 
250 sender &lt;[email protected]&gt; ok 
RCPT TO:&lt;[email protected]&gt; 
250 recipient &lt;[email protected]&gt; ok 
DEBUG SMTP: Verified Addresses 
DEBUG SMTP: [email protected] 
DATA 
354 go ahead 

Body 
. 
250 ok: Message 325177010 accepted 
QUIT 
221 droutbound.logix.in 

我與其他示例程序試圖以及 - 與或沒有pring配置。沒有錯誤。但是沒有郵件。

IMP - 如果我在另一個不使用maven的項目中使用它們,同樣的程序工作的很好,但是否則它們具有相同的配置。 在這些日誌中有..

**DATA 
354 go ahead 
From: [email protected] 
To: [email protected] 
Message-ID: &lt;[email protected]&gt; 
Subject: Say Hi .. 
MIME-Version: 1.0 
Content-Type: text/plain; charset=us-ascii 
Content-Transfer-Encoding: 7bit 
Body 
.** 

之間我堅持一些線。這是由Maven引起的嗎?請建議。

+0

如何發佈相關的pom片段?也許你有一個丟失的運行時jar。 – Raghuram

+0

我已經把相關的POM片段,它的代碼部分。這又是: –

+0

\t \t \t javax.mail \t \t \t 郵件 \t \t \t 1.4.1 \t \t

回答

2

實際上,這是超過所需的罐子造成的問題。

在提取我的戰爭 - 我注意到有以下罐:

Geronimo的activation_1.1_spec-1.0.1.jar,Geronimo的javamail_1.4_spec-1.2.jar,Geronimo的jta_1.1_spec-1.1。 jar,geronimo-stax-api_1.0_spec-1.0.1.jar等

在我的pom中沒有它們的條目 - 由於其他一些依賴性,這些包含在我的lib中。所以,下面做工作..

<plugin> 
<artifactId>maven-war-plugin</artifactId> 
<version>2.1.1</version> 
<configuration> 
<packagingExcludes>WEB-INF/web.xml,WEB-INF/lib/geronimo-activation_1.1_spec- 
     1.0.1.jar, WEB-INF/lib/geronimo-javamail_1.4_spec-1.2.jar 
</packagingExcludes> 
<warName>SCTM_Retailer</warName> 
</configuration> 
</plugin> 

感謝拉爾夫&拉古拉姆。

+1

謝謝,這有助於發現問題。順便說一句,你可以剛剛使用dependencyManagement以更整齊(maven-ish)的方式排除它們 – Cu7l4ss