Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Can't get Net::SMTP to work

by kcantrel (Initiate)
on May 09, 2013 at 21:34 UTC ( #1032850=perlquestion: print w/ replies, xml ) Need Help??
kcantrel has asked for the wisdom of the Perl Monks concerning the following question:

Environment:
O/S: Windows 7 64 bit
Perl: ActivePerl 5.16.3

Problem:
I can't seem to get Net::SMTP to work. It fails to even attempt to connect to the mail server. I confirmed that by using 'wireshark' and noticed that no packets are leaving my computer. Here is the very basic code I am trying to use:
use strict; use warnings; use Net::SMTP; use Net::FTP; my $server = "cube"; my $smtp = Net::SMTP->new(Host => $server, Debug => 1, Timeout => 60); #my $smtp = Net::FTP->new($server, Debug => 1); if(!defined($smtp)) { die "Failed to connect to $server\n"; } print $smtp->domain,"\n"; $smtp->quit;

In the above code the call to Net::SMTP->New() always returns "undef". You can see where I tried "Net::FTP" and that one works just fine, so the underlying network routines seem to work. I also confirmed I can telnet to port 25 of the mail server, and I get the typical "welcome" response. I've tried various options and ways of calling SMTP->new but they all result in the same problem. Any ideas will be greatly appreciated.

A slight update, I just added "($!; $@)" to the die line above and it printed:
Failed to connect to cube (No connection could be made because the target machine actively refused it.; Net::SMTP: connect: No connection could be made because the target machine actively refused it.)
Keep in mind that I saw NO network activity with Wireshark and 'telnet' is able to connect to port 25. Oh, and I have disabled the Windows Firewall. So, in my option the error message is misleading at best.

Comment on Can't get Net::SMTP to work
Download Code
Replies are listed 'Best First'.
Re: Can't get Net::SMTP to work
by tobyink (Abbot) on May 09, 2013 at 22:18 UTC

    Have you tried using the server's IP address rather than host name? Net::SMTP->new appears to return undef if it can't lookup the host name.

    $ perl -MNet::SMTP -MData::Dumper -e'print Dumper(Net::SMTP->new(Host +=> "zzzz.tmpdir.eu", Debug => 1, Timeout => 60))' $VAR1 = undef; $ perl -MNet::SMTP -MData::Dumper -e'print Dumper(Net::SMTP->new(Host +=> "mail.tmpdir.eu", Debug => 1, Timeout => 60))' Net::SMTP>>> Net::SMTP(2.31) Net::SMTP>>> Net::Cmd(2.29) Net::SMTP>>> Exporter(5.66) Net::SMTP>>> IO::Socket::INET(1.33) Net::SMTP>>> IO::Socket(1.34) Net::SMTP>>> IO::Handle(1.33) Net::SMTP=GLOB(0x9ff45e8)<<< 220 mail.g5n.co.uk ESMTP Postfix (Ubuntu) Net::SMTP=GLOB(0x9ff45e8)>>> EHLO localhost.localdomain Net::SMTP=GLOB(0x9ff45e8)<<< 250-mail.g5n.co.uk Net::SMTP=GLOB(0x9ff45e8)<<< 250-PIPELINING Net::SMTP=GLOB(0x9ff45e8)<<< 250-SIZE 10240000 Net::SMTP=GLOB(0x9ff45e8)<<< 250-ETRN Net::SMTP=GLOB(0x9ff45e8)<<< 250-STARTTLS Net::SMTP=GLOB(0x9ff45e8)<<< 250-AUTH PLAIN LOGIN Net::SMTP=GLOB(0x9ff45e8)<<< 250-AUTH=PLAIN LOGIN Net::SMTP=GLOB(0x9ff45e8)<<< 250-ENHANCEDSTATUSCODES Net::SMTP=GLOB(0x9ff45e8)<<< 250-8BITMIME Net::SMTP=GLOB(0x9ff45e8)<<< 250 DSN $VAR1 = bless( \*Symbol::GEN0, 'Net::SMTP' );
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Yes, actually, I initially had the IP address, but changing to the hostname was one of my attempts to get it to work.
      Also, what is odd, even with "Debug => 1" set, I don't get ANY output from the 'new' routine. It is almost like it immediately fails and returns.
Re: Can't get Net::SMTP to work
by Krambambuli (Deacon) on May 10, 2013 at 10:06 UTC
    Try the Perl debugger, run your code with $perl -d ... and try to see what's up, step by step.

    As the error is coming up almost immediately, it shouldn't be too hard to follow the upstream calls into Net::Command or IO::Socket::INET in order to find out what the actual problem would be.

    Apparently you can't get a local socket - hard to say why.
      Okay, so I stepped through the code, and apparently, as expected, it is failing at the:
      if (!connect($sock, $addr)) {

      statement from connect() subroutine in the IO::Socket.pm file. Well, if anything, after that call, which is a low level call that I can't step into, all the sudden $! is set to the "No connection could be made because the target machine actively refused it" error and the logic causes it to return with an error.

      I mentioned before that I have turned off the Windows firewall, but this is a company issued laptop that has McAfee installed on it. It should only be doing virus scanning, but I can't help but think it is somehow causing the problem. What doesn't make sense though, is that I can 'telnet' to port 25 without a problem. So, unless there is some kind of rule that allows telnet to get to that port and not Perl, I don't understand how that can be the case.

      I also have Cisco AnyConnect installed, and it implies it does firewalls, but I think only when the VPN is running, and I don't have it working when doing these tests.

      Anyway, if anyone has any other ideas, I'd love to hear them.

      Thanks!
        In the IO::Socket.pm that I can look into, the code is like

        sub connect { @_ == 2 or croak 'usage: $sock->connect(NAME)'; my $sock = shift; my $addr = shift; my $timeout = ${*$sock}{'io_socket_timeout'}; my $err; my $blocking; $blocking = $sock->blocking(0) if $timeout; if (!connect($sock, $addr)) {
        so the call to connect should still be traceable a bit by single-stepping...Might help to know if the issue is before or after getting a local socket to use. Not that I think that would help too much, but it still might get you a bit closer to a solution.

        Any chance to try the code on another machine ? Are you running this with the same privileges as when you do successfully telnet ? Maybe you can check if it behaves the same way when you run it as Administrator?

Re: Can't get Net::SMTP to work
by Anonymous Monk on Feb 24, 2014 at 10:13 UTC
    Hello There, did you manage to solve this issue? In my setup: windows 7 x64, strawberry perl 5.18.2 my app also fails here:
    Net::SMTPS>>> IO::Socket::INET(1.33) Could not connect to SMTP server
    I have tried the involved port with telnet and the firewall lets the communication pass correctly Best regards Nikolas

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032850]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2015-07-30 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls