找回密码
 加入
搜索
查看: 3920|回复: 3

[AU3基础] 自动发邮件报错

  [复制链接]
发表于 2012-10-1 10:33:48 | 显示全部楼层 |阅读模式
各位高手运行下这个代码,运行后报错,也不知道为何?请指点。谢谢
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include<file.au3>

Global $SmtpServer, $FromName, $FromAddress, $ToAddress, $CcAddress, $Subject, $Body, $AttachFiles, $Importance, $Username, $Password, $IPPort, $ssl, $BccAddress
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")

#cs
        ;$SmtpServer = "Mail.sohu.com" ; address for the smtp-server to use - REQUIRED
        ;$FromName = "LY" ; 名字来自谁是电子邮件的发送
        ;$FromAddress = "dmgy92999@sohu.com" ; address from where the mail should come
        ;$ToAddress = "dmgy92999@sohu.com" ; destination address of the email - REQUIRED
        ;$Subject = "测试一下" ; 从电子邮件的主题 - 可以是任何你希望它是
        ;$Body = "测试这个邮件程序如何?" ; 从邮件消息内容 - 可留空,但你得到一个空白邮件
        ;$AttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - 如果不需要留空白
        ;$CcAddress = "dmgy92999@sohu.com" ; 为抄送地址 - 如果不需要留空白
        ;$BccAddress = "dmgy92999@sohu.com" ; bcc的地址 - 如果不需要留空白
        ;$Importance = "Normal" ; 发送消息的优先级:“高”,“正常”,“低”
        ;$Username = "dmgy92999@sohu.com" ; 从使用的用户名的帐户发送邮件获取 - 需
        ;$Password = "110110" ; 从使用密码的帐户发送邮件获取 - 需
        ;$IPPort = 25 ; 端口用于发送邮件
        ;$ssl = 0 ; 启用/禁用安全套接字层发送 - 如果把使用https 1
        ;~ $IPPort=465                          ; GMAIL port used for sending the mail
        ;~ $ssl=1                               ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS
#ce

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("邮件发送门 V1.0", 550, 450, 190, 120)
$Group1 = GUICtrlCreateGroup("", 5, 0, 540, 445)
$Label1 = GUICtrlCreateLabel("邮件服务器", 15, 30, 60, 20)
$Combo1 = GUICtrlCreateCombo("mail.sohu.com", 100, 30, 150, 20, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "mail.qq.com|", "mail.sohu.com")
$Label2 = GUICtrlCreateLabel("发件人", 310, 30, 60, 20)
$Input1 = GUICtrlCreateInput("", 380, 30, 150, 20)
$Label3 = GUICtrlCreateLabel("发件箱", 15, 60, 60, 20)
$Input2 = GUICtrlCreateInput("", 100, 60, 150, 20)
$Label4 = GUICtrlCreateLabel("收件箱", 310, 60, 60, 20)
$Input3 = GUICtrlCreateInput("", 380, 60, 150, 20)
$Label5 = GUICtrlCreateLabel("用户名", 15, 90, 60, 20)
$Input4 = GUICtrlCreateInput("", 100, 90, 150, 20)
$Label6 = GUICtrlCreateLabel("密码", 310, 90, 60, 20)
$Input5 = GUICtrlCreateInput("", 380, 90, 150, 20)
$Group2 = GUICtrlCreateGroup("服务器设置", 10, 10, 530, 110)
$Group3 = GUICtrlCreateGroup("邮件设置", 10, 130, 530, 281)
$Label7 = GUICtrlCreateLabel("邮件主题", 15, 150, 60, 20)
$Input6 = GUICtrlCreateInput("", 100, 150, 430, 20)
$Label8 = GUICtrlCreateLabel("邮件内容", 15, 215, 60, 20)
$Edit1 = GUICtrlCreateEdit("", 100, 180, 430, 100)
$Label9 = GUICtrlCreateLabel("附件", 15, 290, 60, 20)
$Input7 = GUICtrlCreateInput("", 100, 290, 380, 20)
$Button1 = GUICtrlCreateButton("...", 490, 290, 40, 20)
$Label10 = GUICtrlCreateLabel("抄送", 15, 320, 60, 20)
$Input8 = GUICtrlCreateInput("", 100, 320, 430, 20)
$Label11 = GUICtrlCreateLabel("回复", 15, 350, 60, 20)
$Input9 = GUICtrlCreateInput("", 100, 350, 430, 20)
$Label12 = GUICtrlCreateLabel("优先级", 15, 380, 60, 20)
$Combo2 = GUICtrlCreateCombo("Normal", 100, 380, 60, 20, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "High|low", "Normal")
$Label13 = GUICtrlCreateLabel("端口", 220, 380, 60, 20)
$Input10 = GUICtrlCreateInput("25", 290, 380, 60, 20)
$Checkbox1 = GUICtrlCreateCheckbox("启用安全套接字", 400, 380, 100, 20)
$Button2 = GUICtrlCreateButton("发送邮件", 20, 413, 510, 30)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        $AttachFiles = FileOpenDialog("请选择文件", "", "所有文件(*.*)") ;显示一个文件打开对话框,成功: 返回选中文件的完整
                        GUICtrlSetData($Input7, $AttachFiles)
                        If @error Then
                                ContinueLoop
                                MsgBox(4096, "", "没有选择文件!")
                        EndIf
                        $AttachFiles = GUICtrlRead($Input7)
                Case $nMsg = $Checkbox1
                        If BitAND(GUICtrlRead($Checkbox1), $GUI_CHECKED) = $GUI_CHECKED Then
                                $ssl = 1
                        Else
                                $ssl = 0
                        EndIf
                Case $Button2
                        $SmtpServer = GUICtrlRead($Combo1)
                        $FromName = GUICtrlRead($Input1)
                        $FromAddress = GUICtrlRead($Input2)
                        $ToAddress = GUICtrlRead($Input3)
                        $Subject = GUICtrlRead($Input6)
                        $Body = GUICtrlRead($Edit1)
                        ;$AttachFiles = GUICtrlRead($Input7)
                        $BccAddress = GUICtrlRead($Input8)
                        $Importance = GUICtrlRead($Input9)
                        $Username = GUICtrlRead($Input4)
                        $Password = GUICtrlRead($Input5)
                        $IPPort = GUICtrlRead($Input10)
                        MsgBox(32, "测试", $ssl)
                        $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
        EndSwitch
WEnd

; 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
        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[0]
                        $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[1]
        EndIf
        $objEmail = ""
EndFunc   ;==>_INetSmtpMailCom

; Com Error Handler
Func MyErrFunc()
        $HexNumber = Hex($oMyError.number, 8)
        $oMyRet[0] = $HexNumber
        $oMyRet[1] = StringStripWS($oMyError.description, 3)
        ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
        SetError(1); something to check for when this function returns
        Return
EndFunc   ;==>MyErrFunc
发表于 2012-10-5 10:30:16 | 显示全部楼层
手机看帖,太辛苦了!这个邮件发送器是可以用的,我也写了一个
发表于 2012-10-8 21:31:42 | 显示全部楼层

支持!是路过的,顶起来看

其实我是水蒸气 我看看就走,你们聊!


















防辐射服哪个牌子好
发表于 2012-10-24 23:00:12 | 显示全部楼层
本帖最后由 xms77 于 2012-10-24 23:01 编辑

回复 1# apanworkroom
你的可能是邮件服务器填写不对,我用如下代码测试成功。
#include<file.au3>

Global $SmtpServer, $FromName, $FromAddress, $ToAddress, $CcAddress, $Subject, $Body, $AttachFiles, $Importance, $Username, $Password, $IPPort, $ssl, $BccAddress
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")


        $SmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
        $FromName = "ashui" ; 名字来自谁是电子邮件的发送
        $FromAddress = "smith770430@gmail.com" ; address from where the mail should come
        $ToAddress = "xms77@163.com" ; destination address of the email - REQUIRED
        $Subject = "测试一下" ; 从电子邮件的主题 - 可以是任何你希望它是
        $Body = "测试这个邮件程序如何?" ; 从邮件消息内容 - 可留空,但你得到一个空白邮件
        $AttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - 如果不需要留空白
        $CcAddress = "dmgy92999@sohu.com" ; 为抄送地址 - 如果不需要留空白
        $BccAddress = "" ; bcc的地址 - 如果不需要留空白
        $Importance = "Normal" ; 发送消息的优先级:“高”,“正常”,“低”
        $Username = "smith770430@gmail.com" ; 从使用的用户名的帐户发送邮件获取 - 需
        $Password = "......." ; 从使用密码的帐户发送邮件获取 - 需
        ;$IPPort = 25 ; 端口用于发送邮件
        ;$ssl = 0 ; 启用/禁用安全套接字层发送 - 如果把使用https 1
         $IPPort=465                          ; GMAIL port used for sending the mail
         $ssl=1                               ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS


$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
        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[0]
                        $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[1]
        EndIf
        $objEmail = ""
EndFunc   ;==>_INetSmtpMailCom

; Com Error Handler
Func MyErrFunc()
        $HexNumber = Hex($oMyError.number, 8)
        $oMyRet[0] = $HexNumber
        $oMyRet[1] = StringStripWS($oMyError.description, 3)
        ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
        SetError(1); something to check for when this function returns
        Return
EndFunc   ;==>MyErrFunc
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-23 06:31 , Processed in 0.091463 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表