找回密码
 加入
搜索
查看: 2657|回复: 0

[AU3基础] 邮件无法自动发送

[复制链接]
发表于 2012-10-14 15:46:45 | 显示全部楼层 |阅读模式
大家运行下这个代码,为何用QQ邮箱发送失败呢??请问哪个地方出错了??谢谢
#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")

#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
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-12-23 13:55 , Processed in 0.073701 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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