使用 AutoIt 发送电子邮件

发布于 2024-09-27 04:12:13 字数 103 浏览 3 评论 0 原文

如何使用 AutoIt 发送电子邮件?只需要一个干净的示例和解释,其中包含

  • 来自
  • 主题
  • 消息

How to send an email with AutoIt? Just need a clean example and explanation, containing:

  • to
  • from
  • subject
  • message

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏有森光若流苏 2024-10-04 04:12:13

使用内置代码有两种主要方法:_INetMail() 或 _INetSmtpMail()

下面是帮助文件中的简单代码示例。如果您对帮助文件中未涵盖的它们的工作原理或如何实现它们有任何具体问题,请发表评论。

在我看来 _INetSmtpMail() 路线更合理。下面是它的一些示例代码。

#include <INet.au3>

$s_SmtpServer = "mysmtpserver.com.au"
$s_FromName = "My Name"
$s_FromAddress = "From eMail Address"
$s_ToAddress = "To eMail Address"
$s_Subject = "My Test UDF"
Dim $as_Body[2]
$as_Body[0] = "Testing the new email udf"
$as_Body[1] = "Second Line"
$Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body)
$err = @error
If $Response = 1 Then
    MsgBox(0, "Success!", "Mail sent")
Else
    MsgBox(0, "Error!", "Mail failed with error code " & $err)
EndIf

_INetMail() 方法使用在 Windows 中注册的内置邮件客户端。

#include <INet.au3>

$Address = InputBox('Address', 'Enter the E-Mail address to send message to')
$Subject = InputBox('Subject', 'Enter a subject for the E-Mail')
$Body = InputBox('Body', 'Enter the body (message) of the E-Mail')
MsgBox(0,'E-Mail has been opened','The E-Mail has been opened and process identifier for the E-Mail client is ' & _INetMail($address, $subject, $body))

There are two main ways to go with built in code, _INetMail() or _INetSmtpMail()

Below are the simple code examples from the help file. If you have any specific questions about how they work or how to implement them not covered by the help file please leave a comment.

In my opinion the _INetSmtpMail() route is more reasonable. Below is some example code of it.

#include <INet.au3>

$s_SmtpServer = "mysmtpserver.com.au"
$s_FromName = "My Name"
$s_FromAddress = "From eMail Address"
$s_ToAddress = "To eMail Address"
$s_Subject = "My Test UDF"
Dim $as_Body[2]
$as_Body[0] = "Testing the new email udf"
$as_Body[1] = "Second Line"
$Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body)
$err = @error
If $Response = 1 Then
    MsgBox(0, "Success!", "Mail sent")
Else
    MsgBox(0, "Error!", "Mail failed with error code " & $err)
EndIf

The _INetMail() method using the built in mail client registered with windows.

#include <INet.au3>

$Address = InputBox('Address', 'Enter the E-Mail address to send message to')
$Subject = InputBox('Subject', 'Enter a subject for the E-Mail')
$Body = InputBox('Body', 'Enter the body (message) of the E-Mail')
MsgBox(0,'E-Mail has been opened','The E-Mail has been opened and process identifier for the E-Mail client is ' & _INetMail($address, $subject, $body))
三月梨花 2024-10-04 04:12:13

如果您想使用 AutoIt 发送电子邮件,则必须选择 Microsoft CDO 方法。不要使用客户端邮件自动化(Outlook、Thunderbird 或其他)。 CDO 的主要优点是它不依赖于您使用的客户端邮件程序。它仅依赖于 SMTP 服务器。

您可以使用 AutoIt Inet.au3 库中的函数 _INetSmtpMail。但如果您想控制任何事情,您可以使用下面的用户定义函数_INetSmtpMail


UDF Autoit 文件:UDF_SMTP_eMail.au3

;==============================================================================================================
; Description :  Send an email with a SMTP server by Microsoft CDO technology
; Parametere  : $s_SmtpServer  
;               $s_FromName     
;               $s_FromAddress  
;               $s_ToAddress    
;               $s_Subject      
;               $as_Body        
;               $s_AttachFiles (path file to join)
;               $s_CcAddress    
;               $s_BccAddress   
;               $s_Username     
;               $s_Password     
;               $IPPort         
;               $ssl            
; Return      :  On success none
;                On error code+msg
;==============================================================================================================
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)

    Local $objEmail = ObjCreate("CDO.Message")
    Local $i_Error = 0
    Local $i_Error_desciption = ""

    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress

    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])
            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[1]
    EndIf
EndFunc ;==>_INetSmtpMailCom
;===========================================================================================================

If you want to send email with AutoIt, you hade to choose Microsoft CDO method. Don't use client mail automation (Outlook, Thunderbird or another one). The principle advantage of CDO, is it doesn't depend of client mail program you use. It's only depend on SMTP server.

You can use the function _INetSmtpMail in librairy AutoIt Inet.au3. But if you want to control any thing, you can use this user defined function _INetSmtpMail below :


UDF Autoit file : UDF_SMTP_eMail.au3

;==============================================================================================================
; Description :  Send an email with a SMTP server by Microsoft CDO technology
; Parametere  : $s_SmtpServer  
;               $s_FromName     
;               $s_FromAddress  
;               $s_ToAddress    
;               $s_Subject      
;               $as_Body        
;               $s_AttachFiles (path file to join)
;               $s_CcAddress    
;               $s_BccAddress   
;               $s_Username     
;               $s_Password     
;               $IPPort         
;               $ssl            
; Return      :  On success none
;                On error code+msg
;==============================================================================================================
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)

    Local $objEmail = ObjCreate("CDO.Message")
    Local $i_Error = 0
    Local $i_Error_desciption = ""

    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress

    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])
            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[1]
    EndIf
EndFunc ;==>_INetSmtpMailCom
;===========================================================================================================

她比我温柔 2024-10-04 04:12:13

这是科帕斯描述的一个变体。应该自动为您发送

#include <INet.au3>

$time1 = _NowTime()
$Address = "" ;To Address
$Subject = ""
$Body = ""
_INetMail($address, $subject, $body)
Local $hWnd = WinWait("[CLASS:rctrl_renwnd32]", "", 2) ;Works with Outlook 2013. Class might vary. Use the info tool to be sure. Eg I seriously doubt it will be the same with something like thunderbird.
ControlClick($hWnd, "", "Button1")

Here's a variant of what Copas described. Should send automatically for you

#include <INet.au3>

$time1 = _NowTime()
$Address = "" ;To Address
$Subject = ""
$Body = ""
_INetMail($address, $subject, $body)
Local $hWnd = WinWait("[CLASS:rctrl_renwnd32]", "", 2) ;Works with Outlook 2013. Class might vary. Use the info tool to be sure. Eg I seriously doubt it will be the same with something like thunderbird.
ControlClick($hWnd, "", "Button1")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文