我正在使用 java 6.0、Spring 3.0 和 Maven。我面临一个奇怪的问题。
我正在尝试从我的应用程序发送邮件,但无法发送。我检查了调试,日志似乎很好 - 没有异常/错误,但邮件没有触发。
相关代码:
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 = "admin@myorg.com";
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("myid@myorg.com", "myid@myorg.com", "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:<address@myorg.com> AUTH=address@myorg.com
250 sender <address@myorg.com> ok
RCPT TO:<address@myorg.com>
250 recipient <address@myorg.com> ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: address@myorg.com
DATA
354 go ahead
Body
.
250 ok: Message 325177010 accepted
QUIT
221 droutbound.logix.in
我也尝试过其他示例程序——有或没有 Spring 配置。没有错误。但也没有邮件。
IMP - 如果我在不使用 maven 的其他项目中使用相同的程序,它们可以正常工作 - 但在其他方面具有相同的配置。 在这些日志中,中间还有一些行......
**DATA
354 go ahead
From: address@myorg.com
To: address@myorg.com
Message-ID: <12694833.01327427956033.JavaMail.myid@ABCDE12345>
Subject: Say Hi ..
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Body
.**
我卡住了。这是因为 Maven 吗?请提出建议。
最佳答案
实际上,引起问题的不仅仅是必需的 jar。
关于提取我的 war - 我注意到下面有 jar :
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 中没有它们的条目 - 由于某些其他依赖性,它们被包含在我的库中。所以,下面确实有效..
<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>
感谢拉尔夫和拉古拉姆。
关于java - 发送邮件在 Spring/Maven/Java 6 环境中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8999003/