Chinaunix首页 | 论坛 | 博客
  • 博客访问: 630689
  • 博文数量: 184
  • 博客积分: 10057
  • 博客等级: 上将
  • 技术积分: 2505
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-31 16:34
文章分类

全部博文(184)

文章存档

2010年(5)

2009年(104)

2008年(75)

我的朋友

分类:

2008-09-21 21:02:45

#!/usr/bin/perl -w


use Socket;
use strict;


my($mailTo) = 'dave@cs.cf.ac.uk';


my($mailServer) = 'mailhost2.cs.cf.ac.uk';


my($mailFrom) = 'dave@cs.cf.ac.uk';
my($realName) = "Ralph Martin";
my($subject) = 'Test';
my($body) = "Test Line One.nTest Line Two.n";


$main::SIG{'INT'} = 'closeSocket';


my($proto) = getprotobyname("tcp") || 6;
my($port) = getservbyname("SMTP", "tcp") || 25;
my($serverAddr) = (gethostbyname($mailServer))[4];


if (! defined($length)) {


die('gethostbyname failed.');
}


socket(SMTP, AF_INET(), SOCK_STREAM(), $proto)
or die("socket: $!");


$packFormat = 'S n a4 x8'; # Windows 95, SunOs 4.1+
#$packFormat = 'S n c4 x8'; # SunOs 5.4+ (Solaris 2)


connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr))
or die("connect: $!");


select(SMTP); $| = 1; select(STDOUT); # use unbuffemiles i/o.


{
my($inpBuf) = '';


recv(SMTP, $inpBuf, 200, 0);
recv(SMTP, $inpBuf, 200, 0);
}


sendSMTP(1, "HELOn");
sendSMTP(1, "MAIL From: <$mailFrom>n");
sendSMTP(1, "RCPT To: <$mailTo>n");
sendSMTP(1, "DATAn");


send(SMTP, "From: $realNamen", 0);
send(SMTP, "Subject: $subjectn", 0);
send(SMTP, $body, 0);


sendSMTP(1, "rn.rn");
sendSMTP(1, "QUITn");


close(SMTP);


sub closeSocket { # close smtp socket on error
close(SMTP);
die("SMTP socket closed due to SIGINTn");
}


sub sendSMTP {
my($debug) = shift;
my($buffer) = @_;


print STDERR ("> $buffer") if $debug;
send(SMTP, $buffer, 0);


recv(SMTP, $buffer, 200, 0);
print STDERR ("< $buffer") if $debug;


return( (split(/ /, $buffer))[0] );
}

This program displays:

> HELO
< 250 sentinel.cs.cf.ac.uk Hello dave@miles.cs.cf.ac.uk
[207.3.100.120], pleased to meet you
> MAIL From:
< 250 ... Sender ok
> RCPT To:
< 250 ... Recipient ok
> DATA
< 354 Enter mail, end with "." on a line by itself
>
.
< 250 TAA12656 Message accepted for delivery
> QUIT
< 221 sentinel.cs.cf.ac.uk closing connection

The lines in bold are the commands that were sent to the server. The body of the letter is not shown in the output.

阅读(493) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~