[已解決]使用SMTPMAIL.AU3 發信的怪問題....該傳輸無法連線到伺服器。
本帖最后由 kk_lee69 于 2011-10-22 14:46 编辑我的程式寫好了....使用GMAIL 寄信一切正常......換成公司的主機就會出錯誤......錯誤如下...
已經確定 設定應該不會有問題................
不知道是不是有人知道解決的方法...........
========================================
COM 錯誤訊息 !!!
錯誤描述: 該傳輸無法連線到伺服器。
WIN 錯誤描述:
錯誤代碼: 80020009
上次 DLL 錯誤: 0
錯誤腳本行: 272
錯誤來源: CDO.Message.1
錯誤幫助檔:
錯誤幫助功能表: 0 测试通过....#include <file.au3>
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
Global $oMyRet
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
$objEmail = ObjCreate("CDO.Message")
$objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
$objEmail.To = $s_ToAddress
Local $i_Error = 0
Local $i_Error_desciption = ""
If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
$objEmail.Subject = $s_Subject
If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
$objEmail.HTMLBody = $as_Body
Else
$objEmail.Textbody = $as_Body & @CRLF
EndIf
If $s_AttachFiles <> "" Then
Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
For $x = 1 To $S_Files2Attach
$S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
If FileExists($S_Files2Attach[$x]) Then
$objEmail.AddAttachment($S_Files2Attach[$x])
Else
$i_Error_desciption = $i_Error_desciption & @LF & 'File not found to attach: ' & $S_Files2Attach[$x]
SetError(1)
Return 0
EndIf
Next
EndIf
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
;Authenticated SMTP
If $s_Username <> "" Then
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
EndIf
If $ssl Then
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
EndIf
;Update settings
$objEmail.Configuration.Fields.Update
; Sent the Message
$objEmail.Send
If @error Then
SetError(2)
Return $oMyRet
EndIf
EndFunc ;==>_INetSmtpMailCom
Func MyErrFunc()
$HexNumber = Hex($oMyError.number, 8)
$oMyRet = $HexNumber
$oMyRet = StringStripWS($oMyError.description, 3)
ConsoleWrite("### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet & @LF)
SetError(1); something to check for when this function returns
Return
EndFunc ;==>MyErrFunc
;==================================================================================
$s_SmtpServer = "" ;SMTP服务器
$s_FromName = "" ;邮件发送人,随便写
$s_FromAddress = "" ;邮件发送者地址
$s_ToAddress = "" ;收件人地址
$s_Subject = "Just a test" ;邮件标题
$as_Body = "" ;邮件正文
$s_AttachFiles = "" ;附件地址
$s_CcAddress = "" ;抄送地址
$s_BccAddress = "" ;密件抄送地址
$s_Username = "" ;用户名
$s_Password = "" ;密码
$IPPort = 25 ;发送端口,Gmail使用的发送端口为465
$ssl = 0
$rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)
If @error Then
MsgBox(0, "邮件发送失败", "错误代号:" & @error & " 描述:" & $rc)
EndIf
;===================================================================================== 要么发出你的代码来看看 本帖最后由 kk_lee69 于 2011-10-22 13:50 编辑
回复 3# devil3380
#Region ;**** 參數創建於 ACNWrapper_GUI ****
DIM $MDBSpath=@ScriptDir & "\技術通報資料庫.mdb"
DIM $Txthtm = @ScriptDir&"\TXT.htm"
DIM $Namehtm = @ScriptDir&"\name.htm"
DIM $ErrorMailList = "yanjan13@yanjan.com.tw;mann@ms2.hinet.net;yan.jan@msa.hinet.net"
;#include "SmtpMail.au3"
#include<array.au3>
#Include <file.au3>
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
Global $oMyRet
;~ $rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)
;~ If @error Then
;~ MsgBox(0, "Error sending message", "Error code:" & @error & " Description:" & $rc)
;~ EndIf
;
;郵件配置
$s_SmtpServer = "smtp.gmail.com" ;SMTP伺服器
$s_FromName = "研展資訊技術通告" ;郵件發送人
$s_FromAddress = "chit357@gmail.com" ;郵件發送者地址
$s_ToAddress = "kevin@puretorq.com" ;收件人地址
$s_Subject = "研展資訊--正航技術通告--";郵件標題
$as_Body = "正航技術通告 " ;郵件正文 @CRLF 為分行符號
$s_AttachFiles = "" ;附件位置 【多個檔案 請用 ; 分號隔開。】
$s_CcAddress = "" ;抄送地址
$s_BccAddress = "" ;密件抄送地址
$s_Username = "t357" ;用戶名
$s_Password = "357i" ;密碼
$IPPort = 465 ;發送埠,Gmail使用的發送埠為465 一般為25
$ssl = 1 ;GMAIL 要用SSL=1一般不啟用 SSL=0
If FileExists($MDBSpath) Then
Else
MsgBox(0,"資料庫檔案不存在","技術通報資料庫.MDB 檔案不存在..........請洽詢 研展資訊"&@CRLF&"請按確定離開程式...............")
EXIT
EndIf
$conn = ObjCreate("ADODB.Connection")
$RS = ObjCreate("ADODB.Recordset")
$conn.Open ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=技術通報資料庫.mdb");主機名IP127.0.0.1,UID=用戶,PWD=密碼,database=資料庫名 ※
$RS.ActiveConnection = $conn;好像是主連接,這個好像比較重要
$RS.open ("select *from系統設定");
While (Not $RS.eof And Not $RS.bof);迴圈讀表
;MsgBox(0, "欄位一", $RS.Fields (0).value,5);顯示第一個欄位 資料
;MsgBox(0, "欄位二", $RS.Fields (4).value);顯示第二個欄位 資料
;郵件配置
If $RS.Fields(0).value="A01" Then $s_SmtpServer = $RS.Fields(2).value ;SMTP伺服器
If $RS.Fields(0).value="A02" Then $s_FromName = $RS.Fields(2).value ;郵件發送人
If $RS.Fields(0).value="A03" Then $s_FromAddress = $RS.Fields(2).value ;郵件發送者地址
If $RS.Fields(0).value="A04" Then $s_Subject = $RS.Fields(2).value ;郵件標題
If $RS.Fields(0).value="A05" Then $s_Username = $RS.Fields(2).value ;用戶名
If $RS.Fields(0).value="A06" Then $s_Password = $RS.Fields(2).value ;密碼
If $RS.Fields(0).value="A07" Then $IPPort = $RS.Fields(2).value ;發送埠,Gmail使用的發送埠為465 一般為25
If $RS.Fields(0).value="A08" Then $ssl = $RS.Fields(2).value ;GMAIL 要用SSL=1一般不啟用 SSL=0
$RS.movenext;跳出迴圈
WEnd;結束迴圈讀表
$RS.close;關閉數據分表
sendmail ()
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = "", $s_Password = "",$IPPort=25, $ssl=0)
$objEmail = ObjCreate("CDO.Message")
$objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
$objEmail.To = $s_ToAddress
Local $i_Error = 0
Local $i_Error_desciption = ""
If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
$objEmail.Subject = $s_Subject
If StringInStr($as_Body,"<") and StringInStr($as_Body,">") Then
$objEmail.HTMLBody = $as_Body
Else
$objEmail.Textbody = $as_Body & @CRLF
EndIf
If $s_AttachFiles <> "" Then
Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
For $x = 1 To $S_Files2Attach
$S_Files2Attach[$x] = _PathFull ($S_Files2Attach[$x])
If FileExists($S_Files2Attach[$x]) Then
$objEmail.AddAttachment ($S_Files2Attach[$x])
Else
$i_Error_desciption = $i_Error_desciption & @lf & 'File not found to attach: ' & $S_Files2Attach[$x]
SetError(1)
return 0
EndIf
Next
EndIf
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
;Authenticated SMTP
If $s_Username <> "" Then
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
EndIf
If $Ssl Then
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
EndIf
;$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 0
;$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
;$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/SMTPConnectionTimeout") = 100
;objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/languagecode") = "0x0804"
;$objMail.BodyPart.Charset="UTF-8"
;Update settings
$objEmail.Configuration.Fields.Update
; Sent the Message
$objEmail.Send
if @error then
SetError(2)
return $oMyRet
EndIf
EndFunc ;==>_INetSmtpMailCom
;
;
; Com Error Handler
Func MyErrFunc()
Local $errstr="COM 錯誤訊息 !!!"& @CRLF& @CRLF & _
"錯誤描述:" & @TAB & $oMyError.description & @CRLF & _
"WIN 錯誤描述:" & @TAB & $oMyError.windescription & @CRLF & _
"錯誤代碼:" & @TAB & hex($oMyError.number,8)& @CRLF & _
"上次 DLL 錯誤:" & @TAB & $oMyError.lastdllerror & @CRLF & _
"錯誤腳本行:" & @TAB & $oMyError.scriptline & @CRLF & _
"錯誤來源:" & @TAB & $oMyError.source & @CRLF & _
"錯誤幫助檔:" & @TAB & $oMyError.helpfile & @CRLF & _
"錯誤幫助功能表:" & @TAB & $oMyError.helpcontext & @CRLF
FileWrite(@ScriptDir&"\ErrorLog.log",@YEAR&@MON&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC&" ..... "&$errstr&@CRLF&@CRLF&@CRLF)
Msgbox(0,"AutoIt COM 錯誤",$errstr,30)
Local $err = $oMyError.number
If $err = 0 Then $err = -1
$g_eventerror = $err; to check for after this function returns
Endfunc
Func sendmail ()
Global $oMyRet
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)
If @error Then
MsgBox(0, "郵件發送失敗", "錯誤代號:" & @error & " 描述:" & $rc)
EndIf
EndFunc
重點是 用上面的 GMAIL 一切正常用自己的 公司的主機 就會出錯誤.................
MAIL 代碼 部分應該都是一樣的...............重點在於 我也知道這是可以跑的.....但是我用公司的主機就是不行....用朋友公司的也不行....不知道為何?? $IPPort = 465 ;發送埠,Gmail使用的發送埠為465 一般為25 本帖最后由 kk_lee69 于 2011-10-22 01:08 编辑
回复 5# devil3380
我知道呀GMAIL 是沒有問題的.....我公司用的主機是用25 PORT沒開SSL
設定後就不能跑了.....上面程式 有從資料庫抓取新的 主機設定改成公司的.....
除了 CDO.Message跟JMAIL 的方式 之外還有其他人有方法嗎??
可以交一下嗎?? 本帖最后由 kk_lee69 于 2011-10-22 01:50 编辑
回复 5# devil3380
有眉目了......可惜我不會寫但是我大概知道原因吧...........
請看以下資料....使用Socket撰写邮件发送程序
当然,如果您觉得SmtpMail不能满足您的需求,CDO又不够直截了当,那就只能自己动手了;其实如果您很熟悉Socket编程,自己写一个发送邮件的程序并不很难,以下就是一个例子。
首先,我们简单介绍一下带验证的SMTP服务器如何使用AUTH原语进行身份验证,其详细的定义可以参考RFC2554。
具体如下:
1)首先,需要使用EHLO而不是原先的HELO。
2)EHLO成功以后,客户端需要发送AUTH原语,与服务器就认证时用户名和密码的传递方式进行协商。
3)如果协商成功,服务器会返回以3开头的结果码,这是就可以把用户名和密码传给服务器。
4)最后,如果验证成功,就可以开始发信了。
下面是一个实际的例子,客户端在WinXP的Command窗口中通过"telnet smtp.263.NET 25"命令连接到263的smtp服务器发信:
220 Welcome to coremail System(With Anti-Spam) 2.1
EHLO 263.NET
250-192.168.30.29
250-PIPELINING
250-SIZE 10240000
250-ETRN
250-AUTH LOGIN
250 8BITMIME
AUTH LOGIN
334 VXNlcm5hbWU6
bXlhY2NvdW50
334 UGFzc3dvcmQ6
bXlwYXNzd29yZA==
235 Authentication successful
MAIL FROM:myaccount@263.NET
250 Ok
RCPT TO:myaccount@263.NET
250 Ok
Data
354 End data with <CR><LF>.<CR><LF>
This is a testing email.
haha.
.
250 Ok: queued as AC5291D6406C4
QUIT
221 Bye
上面的内容就是发信的全过程。其中与身份验证有关的主要是第九到第十四行:
AUTH LOGIN '客户端输入
334 VXNlcm5hbWU6 '服务器提示“Username:="
bXlhY2NvdW50 '客户端输入“myaccount="的Base64编码
334 UGFzc3dvcmQ6 '服务器提示“Password:="
bXlwYXNzd29yZA== '客户端输入“mypassword="的Base64编码
235 Authentication successful '服务器端通过验证
从上面的分析可以看出,在这个身份验证过程中,服务器和客户端都直接通过Socket传递经过标准Base64编码的纯文本。这个过程可以非常方便的用C#实现,或者直接添加到原有的源代码中。
另外,有些ESMTP服务器不支持AUTH LOGIN方式的认证,只支持AUTH CRAM-MD5方式验证。但是这两者之间的区别只是文本的编码方式不同。
实现此功能的源代码可以在SourceForge.NET http://sourceforge.NET/projects/opensmtp-net/ 上找到下载。下面给出了一个简单的伪码:
public void SendMail(MailMessage msg)
{
NetworkStream nwstream = GetConnection();
WriteToStream(ref nwstream, "EHLO " + smtpHost + "\r\n");
string welcomeMsg = ReadFromStream(ref nwstream);
// implement HELO command if EHLO is unrecognized.
if (IsUnknownCommand(welcomeMsg))
{
WriteToStream(ref nwstream, "HELO " + smtpHost + "\r\n");
}
CheckForError(welcomeMsg, ReplyConstants.OK);
// Authentication is used if the u/p are supplied
AuthLogin(ref nwstream);
WriteToStream(ref nwstream, "MAIL FROM: <" + msg.From.Address + ">\r\n");
CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK);
SendRecipientList(ref nwstream, msg.To);
SendRecipientList(ref nwstream, msg.CC);
SendRecipientList(ref nwstream, msg.BCC);
WriteToStream(ref nwstream, "DATA\r\n");
CheckForError(ReadFromStream(ref nwstream), ReplyConstants.START_INPUT);
if (msg.ReplyTo.Name != null && msg.ReplyTo.Name.Length != 0)
{ WriteToStream(ref nwstream, "Reply-To: \"" + msg.ReplyTo.Name + "\" <" +
msg.ReplyTo.Address + ">\r\n"); }
else
{ WriteToStream(ref nwstream, "Reply-To: <" + msg.ReplyTo.Address + ">\r\n"); }
if (msg.From.Name != null && msg.From.Name.Length != 0)
{ WriteToStream(ref nwstream, "From: \"" + msg.From.Name + "\" <" +
msg.From.Address + ">\r\n"); }
else
{ WriteToStream(ref nwstream, "From: <" + msg.From.Address + ">\r\n"); }
WriteToStream(ref nwstream, "To: " + CreateAddressList(msg.To) + "\r\n");
if (msg.CC.Count != 0)
{ WriteToStream(ref nwstream, "CC: " + CreateAddressList(msg.CC) + "\r\n"); }
WriteToStream(ref nwstream, "Subject: " + msg.Subject + "\r\n");
if (msg.Priority != null)
{ WriteToStream(ref nwstream, "X-Priority: " + msg.Priority + "\r\n"); }
if (msg.Headers.Count > 0)
{
SendHeaders(ref nwstream, msg);
}
if (msg.Attachments.Count > 0 || msg.HtmlBody != null)
{
SendMessageBody(ref nwstream, msg);
}
else
{
WriteToStream(ref nwstream, msg.Body + "\r\n");
}
WriteToStream(ref nwstream, "\r\n.\r\n");
CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK);
WriteToStream(ref nwstream, "QUIT\r\n");
CheckForError(ReadFromStream(ref nwstream), ReplyConstants.QUIT);
CloseConnection();
}
private bool AuthLogin(ref NetworkStream nwstream)
{
if (username != null && username.Length > 0 &&
password != null && password.Length > 0)
{
WriteToStream(ref nwstream, "AUTH LOGIN\r\n");
if (AuthImplemented(ReadFromStream(ref nwstream)))
{
WriteToStream(ref nwstream, Convert.ToBase64String(
Encoding.ASCII.GetBytes(this.username.ToCharArray())) + "\r\n");
CheckForError(ReadFromStream(ref nwstream), ReplyConstants.SERVER_CHALLENGE);
WriteToStream(ref nwstream, Convert.ToBase64String(Encoding.ASCII.GetBytes(
this.password.ToCharArray())) + "\r\n");
CheckForError(ReadFromStream(ref nwstream), ReplyConstants.AUTH_SUCCESSFUL);
return true;
}
}
return false;
}
重點應該是 需要使用EHLO而不是原先的HELO。 我查過資料 好像有分........我實際測試過...用HELO 他不理我@@ 已經找到問題的所在點了.........已經解決...
詳情請見
http://www.autoitx.com/forum.php?mod=viewthread&tid=28229&page=1&extra=#pid356133
不過不知道有沒有高手願意把上面那段語法 改成AU3的自己寫另外的 SMTP 的發信版本呢??
^^ 期待高手
页:
[1]