Jump to content

Recommended Posts

  • 2 weeks later...
Posted

Hi Jos,

I used your udf to send email of the results generated. The SMTP server is my office outlook  and i have  entered the inputs just as mentioned in udf but i get

COM Error ! Number: 80020009 ScriptLine: 394 Description:The transport failed to connect to the server.

Assume the From .To , Username and Password are valid. 

Tried with all possible ports - 25, 587 , 465 with SSL enabled and Disabled but none of them worked.  Spent sometime to search through the forum for answers but i was kind of lost to find any clue .  My guess is to make it work, TLS should be enabled. Is there anything we should include in script to make TLS enabled ? Honestly i am not sure.

I am sure the UDF works for many people perfectly, but any clue in resolving my issue would be much helpful.

Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")

$SmtpServer = "smtp.office365.com"                          ; address for the smtp-server to use - REQUIRED
$FromName = "Auto Test - AutoIT Workstation"                ; name from who the email was sent
$FromAddress = "pradeep.haridoss@companyXX.com"             ; address from where the mail should come
$ToAddress = "karthik.anandakumar@companyXX.com"            ;destination address of the email - REQUIRED
$Subject = ""                                               ; subject from the email - can be anything you want it to be
$Body = ""                                                  ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = ""                                           ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$CcAddress = ""                                             ; address for cc - leave blank if not needed
$BccAddress = ""                                            ; address for bcc - leave blank if not needed
$Importance = "Low"                                         ; Send message priority: "High", "Normal", "Low"
$Username = "pradeep.haridoss"                               ; username for the account used from where the mail gets sent
$Password = "########"                                  ; password for the account used from where the mail gets sent
;$IPPort = 587                                              ; port used for sending the mail
IPPort = 25
;$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

;Attachmets
$FilesTOAttach = ""                                         ;Help variable with files to attach
$Path = @ScriptDir & "\Results"                             ;Path to Results
$StartTime = StringReplace(_NowCalcDate ( ),"/","") & StringReplace(_NowTime (4),":","")
$MaxSize = 16                                               ;Max size of attachments (in MB)


;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 = 1)
    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

 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

;Checking valid email address
Func _IsValidEmail($email)
    If stringregexp($email,'^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$',0) = 1 Then
        Return 1
    Else
        Return "Invalid email address"
    EndIf
EndFunc

 

Posted (edited)
17 minutes ago, Press said:

Is there anything we should include in script to make TLS enabled ? Honestly i am not sure.

There's a Search feature in the upper right corner of the forum.  You should try it.  ;)

 

Edited by TheXman
Posted (edited)

Hi TheXman,

Thank you for your quick response and helping me out i did search initially in the forum for answers but couldn't find it out.  Am i new learner to autoit and the forum

As per the post i did try changing the line to TLS instead of SSL

$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendtls") = True

### COM Error ! Number: 80020009 ScriptLine: 396 Description:The server rejected the sender address. The server response was: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [BL0PR05CA0026.namprd05.prod.outlook.com]

Now the above error is thrown by the server response as 530 requesting for authentication.

Used  Port for TLS : 587

Any suggestions ?

Edited by Press
Posted

Your current issue is no longer related to any AutoIt issue.  It is now an authentication and/or configuration issue, assuming that you have the privileges necessary to send mail through the SMTP server.

Assuming that the script you posted above is what you are using, the only suggestion that I have is that I would try fully-qualifying your username by including the domain.

i.e.  $Username = "pradeep.haridoss@companyXX.com"

 

Posted (edited)

TheXman,

Sorry I tried that too earlier by mentioning the username with qualifier as $Username = "pradeep.haridoss@companyXX.com" , forgot to mention it .  No luck , It returns the same error. 

### COM Error ! Number: 80020009 ScriptLine: 396 Description:The server rejected the sender address. The server response was: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [BL0PR05CA0026.namprd05.prod.outlook.com]

May be i should check on the authentication /configuration part possibly as per your suggestion.  Thanks a lot for confirming its not an Autoit issue anymore.  I was really having hard time to double check that. Appreciate it. 

Any other suggestion on the authentication part if someone has encountered something similar worth sharing would be much helpful. 

Thanks

Edited by Press
  • Developers
Posted

It works fine for my O365 personal account when I use:

$s_SmtpServer = "smtp.office365.com"              ; address for the smtp-server to use - REQUIRED
$s_FromName = "my name"                      ; name from who the email was sent
$s_FromAddress = "my.emailaddress@outlook.com" ;  address from where the mail should come
$s_ToAddress = "my.emailaddress@yahoo.com"   ; destination address of the email - REQUIRED
$s_Subject = "Test O365 message"                   ; subject from the email - can be anything you want it to be
$as_Body = " test email"                              ; the messagebody from the mail - can be left blank but then you get a blank mail
$s_AttachFiles = ""                       ; the file you want to attach- leave blank if not needed
$s_CcAddress = ""       ; address for cc - leave blank if not needed
$s_BccAddress = ""                          ; address for bcc - leave blank if not needed
$s_Username = "my.emailaddress@outlook.com"                    ; username for the account used from where the mail gets sent - REQUIRED
$s_Password = "my.password"                  ; password for the account used from where the mail gets sent - REQUIRED
$IPPort=25                             ; port used for sending the mail
$ssl=1                                 ; enables/disables secure socket layer sending - put to 1 if using httpS

... and no tls needed.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

  • 3 months later...
Posted

Hey, I've been using the UDF for 2 years now without any problems, really nice work. What i got a problem with is making the script works in Win10 Virtual Machine. Script works fine on main PC, but in VM its returning Error code:2 and empty description. VM Win10 is NOT original, rather stripped down version for performance from this site: Win10-stripped. Can anyone point me right direction what are the requirements for this scipt to works? Tried to install Microsoft Office if in any case the requirements are in Outlook without luck. Thanks for any pointers.

 

Posted
43 minutes ago, HoldanCZ said:

Tried to install Microsoft Office if in any case the requirements are in Outlook without luck.

The UDF uses CDO.  The following link may help.  Pay special attention to the last few paragraphs, especially the last paragraph.

https://support.microsoft.com/en-us/help/171440/where-to-acquire-the-cdo-libraries-all-versions

Posted
48 minutes ago, TheXman said:

The UDF uses CDO.  The following link may help.  Pay special attention to the last few paragraphs, especially the last paragraph.

https://support.microsoft.com/en-us/help/171440/where-to-acquire-the-cdo-libraries-all-versions

Thanks! I checked SysWOW64 folder in VM and cdosys.dll is there (compared with main machine and no other CDO dlls were found). Are there any other requirements for this? Like some objects library or similar? 

Posted (edited)
1 hour ago, HoldanCZ said:

VM its returning Error code:2

An @error code 2 means there was a problem with the send.  Did you look at what, if any, COM error message you received? If so, what did it say?

Edited by TheXman
Posted
Spoiler

### COM Error !  Number: 80040154   ScriptLine: 40   Description:
### COM Error !  Number: 000000A9   ScriptLine: 41   Description:
### COM Error !  Number: 000000A9   ScriptLine: 42   Description:
### COM Error !  Number: 000000A9   ScriptLine: 47   Description:
### COM Error !  Number: 000000A9   ScriptLine: 49   Description:
### COM Error !  Number: 000000A9   ScriptLine: 68   Description:
### COM Error !  Number: 000000A9   ScriptLine: 69   Description:
### COM Error !  Number: 000000A9   ScriptLine: 71   Description:
### COM Error !  Number: 000000A9   ScriptLine: 74   Description:
### COM Error !  Number: 000000A9   ScriptLine: 75   Description:
### COM Error !  Number: 000000A9   ScriptLine: 76   Description:
### COM Error !  Number: 000000A9   ScriptLine: 79   Description:
### COM Error !  Number: 000000A9   ScriptLine: 80   Description:
### COM Error !  Number: 000000A9   ScriptLine: 82   Description:
### COM Error !  Number: 000000A9   ScriptLine: 88   Description:
### COM Error !  Number: 000000A9   ScriptLine: 92   Description:
### COM Error !  Number: 000000A9   ScriptLine: 94   Description:

;
;##################################
; Include
;##################################
#Include<file.au3>
;##################################
; Variables
;##################################
$SmtpServer = "smpt.server.com"           ; address for the smtp-server to use - REQUIRED
$FromName = @UserName                      ; name from who the email was sent
$FromAddress = "from@address.com" ; address from where the mail should come
$ToAddress = "my@mail.com"   ; destination address of the email - REQUIRED
$Subject = "Test"                 ; subject from the email - can be anything you want it to be
$Body = "<br>"&"TEST"&"<br>"                             ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = ""                       ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$CcAddress = ""       ; address for cc - leave blank if not needed
$BccAddress = ""     ; address for bcc - leave blank if not needed
$Importance = "Normal"                  ; Send message priority: "High", "Normal", "Low"
$Username = ********    ; username for the account used from where the mail gets sent - REQUIRED
$Password = ********    ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = 25                            ; port used for sending the mail
$ssl = 1                              ; enables/disables secure socket layer sending - put to 1 if using httpS
$tls = 0                                ; enables/disables TLS when required
;~ $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, $tls)
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, $tls = 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
    ; Set security params
    If $ssl Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    If $tls Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendtls") = True
    ;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

Seems it cant create object CDO.Message for some reason

Posted (edited)

Can you replace the MyErrFunc with the one below, rerun it, and show the console messages?  I just replaced $oMyError.description with $oMyError.windescription.

Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.windescription, 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

0x80040154 means "class not registered" or "invalid class string".  So I agree that the cdo.message object is not getting created.  Did you try to manually register the dll?

Is your VM 32 or 64 bit?

How did you install CDO, manually or by installing the option when you installed Outlook?

I have no idea what Windows components were removed in that "crippled" distribution of Windows that you are using.  So as far as I'm concerned, any further trouble shooting would be a waste of time.

Edited by TheXman
Posted

Why are you trying to register the cdosys.dll file in the syswow64 folder?  When you are doing so, are you using the regsvr32 in the syswow64 folder too?

Is there a cdosys.dll file in the system32 folder?  Did you try registering it also (using the regsvr32 in the system32 folder)?

 

Posted

Tried regsvr32 located in system32 to register said dll in system32 as well as regsvr32 located in SysWOW64 to register cdosys.dll in SysWOW64. Register dll in system32 works but in SysWoW64 doesn't.... successful registration in system32 doesnt make difference on the script.

Posted (edited)
On 3/25/2016 at 9:12 PM, Jos said:
Quote

Just wondering is here any way to set "User Agent"  header (aka mailer client ID) in this UDF?

No idea, this UDF is using the standard CDO COM object, so you could try searching for this whether it can be set.

Jos

I know it's late, but anyway here it is:

$objEmail.Fields("urn:schemas:mailheader:x-mailer") = "My application Name or whatever else"

 

and here is the way how to set "code page" (here Czech) :

this is important when you have chars with diacritics (special non-english letters) in your email

$objEmail.BodyPart.ContentTransferEncoding = "8bit"
$objEmail.BodyPart.CharSet = "windows-1250"

 

Edited by Zedna
Posted

@Jos

just one little code optimization:

instead of

$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
    ; Set security params
    If $ssl Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    If $tls Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendtls") = True

 

use this, where repeating string "http://schemas.microsoft.com/cdo/configuration/" is in variable (only once):

$schema_cfg = "http://schemas.microsoft.com/cdo/configuration/"
    $objEmail.Configuration.Fields.Item ($schema_cfg & "sendusing") = 2
    $objEmail.Configuration.Fields.Item ($schema_cfg & "smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 then $IPPort = 25
    $objEmail.Configuration.Fields.Item ($schema_cfg & "smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item ($schema_cfg & "smtpauthenticate") = 1
        $objEmail.Configuration.Fields.Item ($schema_cfg & "sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item ($schema_cfg & "sendpassword") = $s_Password
    EndIf
    ; Set security params
    If $ssl Then $objEmail.Configuration.Fields.Item ($schema_cfg & "smtpusessl") = True
    If $tls Then $objEmail.Configuration.Fields.Item ($schema_cfg & "sendtls") = True

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...