2012-06-13 45 views
2

我寫了一個perl腳本使用Gmail的SMTP服務器發送電子郵件驗證失敗:smtp.gmail.com -(Perl的淨:: SMTP)使用Gmail的SMTP服務器

use Net::SMTP; 

my $smtp = Net::SMTP->new('smtp.gmail.com', 
        Port=> 587, 
        Timeout => 20, 
        Hello=>'[email protected]' 
        ); 
print $smtp->domain,"\n"; 
$sender = "[email protected]"; 
$password = "mypassword"; 
$smtp->auth ($sender, $password) or die "could not authenticate\n"; 
$receiver = "[email protected]"; 

$subject = "my custom subject"; 
$smtp->mail($sender); 
$smtp->to($receiver); 
$smtp->data(); 
$smtp->datasend("To: <$reciever> \n"); 
$smtp->datasend("From: <$sender> \n"); 
$smtp->datasend("Content-Type: text/html \n"); 
$smtp->datasend("Subject: $subject"); 
$smtp->datasend("\n"); 
$smtp->datasend('the body of the email'); 
$smtp->dataend(); 
$smtp->quit(); 
print "done\n\n"; 

對於 '用戶',我有我的Gmail用戶名和'mypassword'我有密碼。但是當我運行這段代碼時,它停止在auth本身給出:無法進行身份驗證。

我能夠連接到谷歌的smtp serever,因爲我得到:'mx.google.com'作爲結果:
print $ smtp-> domain,「\ n」;

這是什麼,我做錯了?請幫忙。

回答

3
use Net::SMTP; 
my $smtp = Net::SMTP->new .... 

據我所知,您需要使用加密連接通過Gmail來發送,使用的Net :: SMTP :: TLS的Net :: SMTP :: SSL(與端口465)

Hello=>'[email protected]' 

「你好」不是一個電子郵件地址,把你的主機名,而不是有

$sender = "[email protected]"; 
... 
$receiver = "[email protected]"; 

把這些單引號中

如果您仍然「無法驗證」。確保你已經安裝了模塊MIME :: Base64Authen :: SASL

$smtp->datasend("To: <$reciever> \n"); 

應該$接收機,不$ reciever

+0

我放棄了'你好'的說法,並糾正了報價錯誤,仍然沒有結果。 TLS和SSL模塊可用於perl 5.14窗口。我不能使用它,因爲我必須在Windows中使用win32 :: serialport。請通過調整上述代碼本身來告訴我們。 – CuriousSid

+0

對於提及「如果您仍然無法進行身份驗證」,請+1。「確保您已安裝模塊MIME :: Base64和Authen :: SASL。」 – Anshu