Advertisement
Guest User

AutoIt SMTP Send Mail

a guest
Nov 4th, 2010
3,850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 4.21 KB | None | 0 0
  1. #include <file.au3>
  2.  
  3. ;AutoIt Error Object (Prevents script from crashing if an unexpected error occurs
  4. Global $oMyError = ObjEvent("AutoIt.Error", "ErrorDebug")
  5.  
  6. ;;SMTP Email Info
  7. Local $Subject = "[AutoIt] Subject"
  8. Local $Username = "yourusername@gmail.com"
  9. Local $Password = "p@5s"
  10. Local $SmtpServer = "smtp.gmail.com"
  11. Local $FromName = "SomeName"
  12. Local $IPPort = 465
  13. Local $ssl = 1
  14. Local $ToAddress = "to@hotmail.com"
  15. Local $FromAddress = "autoit@gmail.com"
  16. Local $AttachFiles = ""
  17. Local $CcAddress = ""
  18. Local $BccAddress = ""
  19. Local $Importance = "High"
  20. Local $body = "This is the body of the message"
  21. Local $oMyRet[1]
  22. Local $objEmail
  23.  
  24.  
  25. _INetSmtpMailCom($SMTPSErver,$FromName,$FromAddress,$ToAddress,$Subject,$Body,"","","",$Importance,$Username,$password,$IPPort,$SSL)
  26.  
  27.  
  28. Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "High", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
  29.     $objEmail = ObjCreate("CDO.Message")
  30.     $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
  31.     $objEmail.To = $s_ToAddress
  32.     Local $i_Error = 0
  33.     Local $i_Error_desciption = ""
  34.     If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
  35.     If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
  36.     $objEmail.Subject = $s_Subject
  37.     If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
  38.         $objEmail.HTMLBody = $as_Body
  39.     Else
  40.         $objEmail.Textbody = $as_Body & @CRLF
  41.     EndIf
  42.     If $s_AttachFiles <> "" Then
  43.         Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
  44.         For $x = 1 To $S_Files2Attach[0]
  45.             $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
  46.             ConsoleWrite('@@ Debug(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
  47.             If FileExists($S_Files2Attach[$x]) Then
  48.                 $objEmail.AddAttachment($S_Files2Attach[$x])
  49.             Else
  50.                 ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
  51.                 SetError(1)
  52.                 Return 0
  53.             EndIf
  54.         Next
  55.     EndIf
  56.     $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  57.     $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
  58.     If Number($IPPort) = 0 Then $IPPort = 25
  59.     $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
  60.     ;Authenticated SMTP
  61.     If $s_Username <> "" Then
  62.         $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  63.         $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
  64.         $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
  65.     EndIf
  66.     If $ssl Then
  67.         $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
  68.     EndIf
  69.     ;Update settings
  70.     $objEmail.Configuration.Fields.Update
  71.     ; Set Email Importance
  72.     Switch $s_Importance
  73.         Case "High"
  74.             $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "High"
  75.         Case "Normal"
  76.             $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
  77.         Case "Low"
  78.             $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
  79.     EndSwitch
  80.     $objEmail.Fields.Update
  81.     ; Sent the Message
  82.     ConsoleWrite("echo Sending" & @LF)
  83.     If Not IsObj($objEmail) Then
  84.         ConsoleWrite("echo error sending (not an object)" & @CRLF)
  85.         $sendfailed = True
  86.     Else
  87.         $sendfailed = False
  88.         $objEmail.Send
  89.     EndIf
  90.     ConsoleWrite("echo SENT" & @LF)
  91.     If @error Then
  92.         $sendfailed = True
  93.         SetError(2)
  94. ;~      _ArrayDisplay($oMyRet, "MyRet")
  95.         Return $oMyRet[1]
  96.     EndIf
  97.     $objEmail = 0
  98. EndFunc   ;==>_INetSmtpMailCom
  99.  
  100.  
  101.  
  102. Func ErrorDebug()
  103.     Local $HexNumber
  104.     Local $strMsg
  105.  
  106.     $HexNumber = Hex($oMyError.Number, 8)
  107.     $strMsg = "Error Number: " & $HexNumber & @CRLF
  108.     $strMsg &= "WinDescription: " & $oMyError.WinDescription & @CRLF
  109.     $strMsg &= "Script Line: " & $oMyError.ScriptLine & @CRLF
  110.     FileWrite(@ScriptDir & "\" & @ScriptName & "_ErrorDebug.txt", "------------------[Error]------------------" & @LF & $strMsg & @LF & @LF)
  111.     SetError(1)
  112. EndFunc   ;==>ErrorDebug
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement