perl发送邮件

方法一:

#!/usr/bin/perl -w

use Net::SMTP;
use MIME::Lite;
use MIME::Base64;
use Authen::SASL;

my $from = 'xxx@xxx.com';
my $passwd = '*********;
my $to = 'xxx@xxx.com';
my $messages = "Hello Rainbow!";

my $msg = MIME::Lite->new(
    From     =>  $from,
    To       =>  $to,
#   Cc       =>  'xxx@xxx.com',
    Subject  =>  'Hello Rainbow!',
    Type     =>  'multipart/mixed',
    Data     =>  $messages,
);

$msg->attach(
    Type     => 'AUTO',      # the attachment mime type
    Path     => $dir, # local address of the attachment
);

MIME::Lite->send('smtp','smtp.domain.cn',  # domain为域名 如:gmail
    Debug   =>'1',
    AuthUser=>$from,
    AuthPass=>$passwd,
);
$msg->send;

方法二:

#!usr/bin/perl

use Net::SMTP;

$server = 'smtp.domain.com';  # domain为域名 如:gmail
$fromaddress = 'xxx@xxx.com';
$toaddress = 'xxx@xxx.com';
$passwd = '********';

$subject = "Test Example \n\n";

$message = "This is a test \n\n";

$smtp = Net::SMTP->new($server);
$smtp->auth($fromaddress,$passwd) || print "Auth Error!\n";


$smtp->mail($fromaddress);
$smtp->to($toaddress);
$smtp->data();
$smtp->datasend("Subject: $subject");
$smtp->datasend($message);
$smtp->dataend();

$smtp->quit();

说明:经测试方法而可以向外网发送邮件,而法一不行。

其实更简单的方法是使用linux shell来发送和接收邮件,

详见 1 mutt邮件客户端

        2 用linux脚本自动发送和收取邮件

posted @ 2011-06-09 20:14  ACE封印  Views(844)  Comments(0Edit  收藏  举报