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

[系统综合] [已解决]使用以下代码发邮件报错

[复制链接]
发表于 2013-3-25 17:46:32 | 显示全部楼层 |阅读模式
本帖最后由 shuaiw 于 2013-3-29 16:30 编辑

;
;##################################
; Include
;##################################
#Include<file.au3>
;##################################
; Variables
;##################################
$SmtpServer = "smtp.163.com.cn"              ; address for the smtp-server to use - REQUIRED
$FromName = "shuai"                      ; name from who the email was sent
$FromAddress = "shuai@163.com.cn" ; address from where the mail should come
$ToAddress = "shuai@163.com.cn"   ; destination address of the email - REQUIRED
$Subject = "Userinfo"                   ; subject from the email - can be anything you want it to be
$Body = "112"                               ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = ""                       ; the file you want to attach- leave blank if not needed
$CcAddress = "CCadress1@test.com"       ; address for cc - leave blank if not needed
$BccAddress = "BCCadress1@test.com"     ; address for bcc - leave blank if not needed
$Importance = "Normal"                  ; Send message priority: "High", "Normal", "Low"
$Username = "shuai"                    ; username for the account used from where the mail gets sent - REQUIRED
$Password = "890621"                  ; 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[2]
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
    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(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
            If FileExists($S_Files2Attach[$x]) Then
                $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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2013-3-25 21:36:24 | 显示全部楼层
发送成功的列子如下,你看看不是不邮件服务器名称有问题或邮件服务器不支持smtp啊?
#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 = "minshui.x.xi@seagate.com" ; destination address of the email - REQUIRED
        $Subject = "testing" ; 从电子邮件的主题 - 可以是任何你希望它是
        $Body = "have you received this email?" ; 从邮件消息内容 - 可留空,但你得到一个空白邮件
        $AttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - 如果不需要留空白
        $CcAddress = "xms<xms77@163.com>,seagatehsame@gmail.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
回复 1# shuaiw
 楼主| 发表于 2013-3-26 16:18:00 | 显示全部楼层
恩,在这个代码上可以用了。谢谢帮助
发表于 2018-8-6 11:01:06 | 显示全部楼层
---------------------------
Error sending message
---------------------------
Error code:2  Description:与服务器的传输连接失败。
---------------------------
确定   
---------------------------
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-12-23 13:40 , Processed in 0.075852 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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