java - 发送邮件可以在 Perl 中使用,但不能在 Java 中使用

标签 java perl email

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

  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 <no-reply@xxxx.com>';
my $to   = 'shardul@xxxx.com';
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 ( $@ ) {
    print STDERR "Confirmation Mail Sending to $to Failed.\n";
    print STDERR 'ERROR: ' . $@;
}

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   => 'test@xxxxxx.com',
            to        => $to,
            bcc       => $bcc,
            subject   => $subject,
            msg       => $msg,
            auth      => $auth,
            authid    => $from,
            authpwd   => $authpwd,
        }
    );
    if ( $result < 0 ) {
        die "$Mail::Sender::Error\n";
    }
}

最佳答案

您收到java.net.ConnectException,因此 Java VM 无法连接到邮件服务器。这并不是真正的邮件问题,因为 Java 无法看到邮件服务器。

你说:

everything works fine using the same credentials and configuration parameters from a Perl Script.

Perl 脚本与 Java 运行在同一系统上吗?如果不是,则表明 Java 主机和邮件服务器之间存在网络连接问题。

如果 Perl 脚本在同一系统上运行,请仔细检查该脚本是否按照您的想法进行操作,也许可以使用诸如 netstat 命令之类的命令。如果服务器有多个网络接口(interface),请检查 Java VM 是否绑定(bind)到无法联系邮件服务器的网络接口(interface)。

更新 - 您的 Perl 脚本使用位于 172.16.16.1 的邮件服务器发送邮件。从这一行你可以看到:

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 规则。例如,网络连接可能受到用户 ID 的限制。您是否以与 Tomcat 相同的用户身份运行 Perl 脚本?

关于java - 发送邮件可以在 Perl 中使用,但不能在 Java 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9837233/

相关文章:

java - 如何在 jframe 上使用 Imageicon 打开图像

Java:插入排序算法交换

perl - 如何使用 Mojo 进行内部重定向?

email - 电子邮件业务流程中未找到媒体异常 (Hybris)

java - 我无法使用 JsonArray 获取 Servlet 中的列表值

java - 重写 stringBuilder 中的追加方法?

perl - 如何增加 Perl Socket 模块中 sndbuf/rcvbuf 大小套接字选项?

带条件的单行 Ruby 多个语句

html - Gmail 不在 html 邮件中执行 CSS (svnspam)

html - Office 365 电子邮件背景图像/渐变或 VML 磁贴/渐变