2012-03-23 132 views
1

我有一个使用Java使用我们自己的SMTP服务器发送电子邮件的场景。我有一个应用程序部署在Tomcat 6上。从Perl发送邮件但不从Java发送邮件

  1. 当我们使用GMail SMTP时,发送电子邮件的功能起作用。
  2. 当我尝试使用我们自己的服务器时,该功能不起作用。

我收到以下错误

MailException occured while sending emailMail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: 172.16.16.1, port: 25; 
    nested exception is: 
    java.net.ConnectException: Connection timed out. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: 172.16.16.1, port: 25; 
    nested exception is: 
    java.net.ConnectException: Connection timed out 

的问题是,一切从使用Perl脚本相同的证书和配置参数正常工作。

我在Debain系统上。

我怀疑是否存在某种访问权限问题。我是Linux新手,知识非常有限。

任何人都可以请帮忙吗?

Perl脚本

粘贴下面的Perl脚本参考

#!/usr/bin/env perl 

# System Modules 
use strict; 
use warnings; 
use Mail::Sender; 

my $subject = q/Testing Mail Server./; 
my $smtp = q/localhost/; 
my $from = 'Test <[email protected]>'; 
my $to = '[email protected]'; 
my $msg = q/Hi Testing.../; 
my $auth = q{}; 
my $authid = q{}; 
my $authpwd = q{}; 

eval { 
    sendmail($smtp, $from, $to, q{}, $subject, $msg, $auth, $authid, $authpwd); 
}; 
if ([email protected]) { 
    print STDERR "Confirmation Mail Sending to $to Failed.\n"; 
    print STDERR 'ERROR: ' . [email protected]; 
} 

sub sendmail { 
    my ($smtp, $from, $to, $bcc, $subject, $msg, $auth, $authid, $authpwd) = @_; 
    my $sender = new Mail::Sender { smtp => $smtp, from => $from }; 
    $sender->Body(0, 0, 'text/html'); 
    my $result = $sender->MailMsg(
     { 
      replyto => '[email protected]', 
      to  => $to, 
      bcc  => $bcc, 
      subject => $subject, 
      msg  => $msg, 
      auth  => $auth, 
      authid => $from, 
      authpwd => $authpwd, 
     } 
    ); 
    if ($result < 0) { 
     die "$Mail::Sender::Error\n"; 
    } 
} 

回答

1

你得到一个java.net.ConnectException所以Java虚拟机无法连接到邮件服务器。这不是真正的邮件问题,因为Java无法看到邮件服务器。

你说:

一切使用Perl脚本相同的证书和配置参数正常工作。

Perl脚本是否与Java在同一个系统上运行?如果不是,它看起来像Java主机和邮件服务器之间的网络连接问题。

如果Perl脚本在同一个系统上运行,请检查脚本是否按照您的想法进行操作,或许使用类似netstat命令的命令。如果服务器有多个网络接口,请检查Java虚拟机未绑定到无法联系邮件服务器的虚拟机。

更新 - 使用你的邮件服务器在172.16.16.1邮件发送到Perl脚本是。您可以从该行看到:

my $smtp = q/localhost/; 

该脚本通过同一主机的脚本上运行的邮件服务器发送邮件。

它看起来像一个网络问题。尝试运行以下命令:

telnet 172.16.16.1 25 

如果您通过网络连接到端口25上的邮件服务器,你会看到类似这样的:

$ telnet smtp.gmail.com 25 
Trying 173.194.67.109... 
Connected to gmail-smtp-msa.l.google.com. 
Escape character is '^]'. 
220 mx.google.com ESMTP fw5sm12835658wib.0 

如果没有网络连接,你会看到这样的事情:

$ telnet 172.16.16.1 25 
Trying 172.16.16.1... 
telnet: connect to address 172.16.16.1: Operation timed out 
telnet: Unable to connect to remote host 

如果你有连接,则必须存在本地系统防止JVM无法连接,可能是一个iptables规则的东西。例如,网络连接可能会受到用户标识的限制。你是否像Tomcat一样运行Perl脚本?

+0

Perl脚本和Tomcat在同一台机器上。事实上与Tomcat在同一个目录中。 – 2012-03-23 10:19:36

+0

Perl脚本实际上很简单,我已经将它添加到问题中以供参考。 – 2012-03-23 10:27:36

+0

@SB。 - 正如我所建议的那样,剧本并没有按照你的想法去做。 – 2012-03-23 10:32:10

0

您是否正确配置了您的tomcat服务器以连接到使用javamail?

我想你需要在你的context.xml中设置一个资源,并在你的web.xml中设置一个resource-ref。

退房这个职位步骤:

http://haveacafe.wordpress.com/2008/09/26/113/

+0

我假设,因为它可以正常工作与GMail,它应该与任何SMTP作为ling提供所需的凭据。 – 2012-03-26 10:09:59

0

这个问题很简单,在某种程度上它从来没有想过我们,Perl脚本被发送空AUTH参数

my $auth = q{}; 
my $authid = q{}; 
my $authpwd = q{}; 

是SMTP可能不需要任何身份验证来发送电子邮件。

在Java程序中,我们将用户名和密码设置为BLANK,因此Java程序失败。当我们禁用身份验证时,通过将mail.smtp.auth设置为false,它工作正常。

我不确定在从内部SMTP服务器发送电子邮件时不使用身份验证的做法有多好/不好。

感谢您的帮助。

Regards,

Shardul。