jiaosc 发表于 2014-8-28 21:31:26

求助:SMTP发邮件.au3程序在Win XP系统下运行正常,换用Win7系统后报错(已解决)

本帖最后由 jiaosc 于 2014-8-30 09:52 编辑

求助:SMTP发邮件.au3程序在Win XP系统下运行正常,换用Win7系统后报错,求助大师帮忙看看,谢谢!
附源码:

#include<file.au3>
;##################################
; Variables
;##################################
$SmtpServer = "smtp.163.com" ; SMTP服务器address for the smtp-server to use - REQUIRED
$FromName = "23" ; 发送人name from who the email was sent
$FromAddress = "gre@163.com" ; 发送人地址address from where the mail should come - 139mail REQUIRED
$ToAddress = "osc@126.com" ; 收件人地址destination address of the email - REQUIRED
$Subject = "22" ; 邮件标题subject from the email - can be anything you want it to be
$Body = "wrw" ; 邮件正文the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = "" ; 附件链接the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$CcAddress = "" ; 抄送地址address for cc - leave blank if not needed
$BccAddress = "" ; 秘密抄送地址address for bcc - leave blank if not needed
$Importance = "Normal" ; 邮件重要性Send message priority: "High", "Normal", "Low"
$Username = "tjyj" ; 用户名username for the account used from where the mail gets sent - REQUIRED
$Password = "244244" ; 密码password for the account used from where the mail gets sent - REQUIRED
$IPPort = 25 ; port used for sending the mail
$ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS
;~ $IPPort=465                        ; GMAIL port used for sending the mail
;~ $ssl=1                               ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS

;##################################
; Script
;##################################
Global $oMyRet
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
If @error Then
      MsgBox(0, "Error sending message", "Error code:" & @error & "Description:" & $rc)
EndIf
;
; The UDF
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
      Local $objEmail = ObjCreate("CDO.Message")
      $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
      $objEmail.To = $s_ToAddress→这一句win7下报错,好像是说:错误:关键字之前失踪的分隔符.(error: missing separator character before keyword.)但在xp系统下运行正常!
      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])
;~          ConsoleWrite('@@ Debug : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
                        If FileExists($S_Files2Attach[$x]) Then
                              ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
                              $objEmail.AddAttachment($S_Files2Attach[$x])
                        Else
                              ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
                              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
      If Number($IPPort) = 0 Then $IPPort = 25
      $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
      ; Set Email Importance
      Switch $s_Importance
                Case "High"
                        $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "High"
                Case "Normal"
                        $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
                Case "Low"
                        $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
      EndSwitch
      $objEmail.Fields.Update
      ; Sent the Message
      $objEmail.Send
      If @error Then
                SetError(2)
                Return $oMyRet
      EndIf
      $objEmail = ""
EndFunc   ;==>_INetSmtpMailCom

; Com Error Handler
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

附报错截图:

xjlijia 发表于 2014-8-29 11:06:34

等待高手解答,顺便学习。

jiaosc 发表于 2014-8-29 16:59:13

本帖最后由 jiaosc 于 2014-8-29 17:03 编辑

原来xp用得好好的,换用win7后不能用了,急啊,总不能再回到xp吧,迫切希望各路高手出手相助。

MicroBlue 发表于 2014-8-29 23:35:11

会不会是因为权限的问题,OBJ创建失败,你加一句#requireadmin试试呢?

jiaosc 发表于 2014-8-30 07:01:53

本帖最后由 jiaosc 于 2014-8-30 09:12 编辑

试了,不是权限问题,加上#requireadmin以管理员身份运行同样报错。
上一次同样的问题:在编译带有 File Line UDF函数的脚本在Win XP系统下运行正常,Win7下报错提示为:
_ArraySwap($Arr[$x], $Arr[$x - 1])→ error: _ArraySwap() called with wrong number of args.   
后经user3000老师的指点将: _ArraySwap($Arr[$x], $Arr[$x - 1])改为_ArraySwap($Arr,$Arr[$x], $Arr[$x - 1])后运行正常了。
这一次函数报错的信息是$objEmail.To = $s_ToAddress→error: missing separator character before keyword.
                $objEmail.To
        ~~~~~~~~~~^
那又该如何修改呢?

jiaosc 发表于 2014-8-30 09:50:59

哈哈!问题终于找到了!原来报错的这一句:$objEmail.To = $s_ToAddress中, .和TO中间没有空格即:.TO,这个空格在XP中不影响编译,但在win7系统下这个 .和TO中间必须有一个空格,否则就会出现error: missing separator character before keyword.的错误提示!这也算是提供给大家的一个经验把。

lin6051 发表于 2014-8-31 09:00:09

学习了楼主自力更生啊

bdrdc 发表于 2016-7-15 23:35:53

回复 6# jiaosc


    加了空格,还是不成
页: [1]
查看完整版本: 求助:SMTP发邮件.au3程序在Win XP系统下运行正常,换用Win7系统后报错(已解决)