Mateus_Terra

Sent smtp mail with func

28 posts in this topic

#1 ·  Posted

Exemplo ()

Func Exemplo()
TCPStartup() ; Inicie o serviço TCP.

    ; Registre OnAutoItExit para ser chamado quando o script é fechado.
OnAutoItExitRegister ("OnAutoItExit")

    ; Atribua variáveis locais o endereço IP de loopback e a porta.
Local $sIPAddress = "10.0.19.68" ; Este endereço IP só funciona para testar em seu próprio computador.
Local $iPort = 80 ; Porta usada para a conexão.


    ; Atribua uma variável local ao soquete e conecte a um soquete de escuta com o endereço IP ea porta especificados.
Local $iSocket = TCPConnect ($sIPAddress, $iPort)

    ; Se um erro ocorrer exibir o código de erro e retornar Falso.
if @error Then ; O servidor provavelmente está offline / a porta não está aberta no servidor.
   Local $iError = @Error


;~     MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Não foi possível conectar-se, o código de erro:" & $iError )
Return False


$SmtpServer = "smtp-mail.outlook.com"                            ; address for the smtp-server to use - REQUIRED
$FromName = ""                          ; name from who the email was sent
$FromAddress = ""                           ; address from where the mail should come
$ToAddress = ""                            ; destination address of the email - REQUIRED
$subject = ""
$body = ""
$AttachFiles = ""
$CcAddress = ""                             ; address for cc - leave blank if not needed
$BccAddress = ""        ; address for bcc - leave blank if not needed
$Importance = "High"                      ; 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



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


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




Else

MsgBox($MB_SYSTEMMODAL, "", "Conexão bem sucedida")




EndIf
  ; Feche o soquete.
TCPCloseSocket ($iSocket)
EndFunc ; ==> Exemplo


Func OnAutoItExit()
TCPShutdown() ; Feche o serviço TCP.
EndFunc ; ==> OnAutoItExit

Hello friends, someone help me!
I need to do this tcp connection test and problem send an email ... But it seems like it can not have a function inside another function, I'm hooked on it!

What can I do in this case?

 

error: "Func" statement has no matching "EndFunc".:

Share this post


Link to post
Share on other sites



#2 ·  Posted

Correct, so simply move them to the bottom of the script!

Jos

Exemplo()

Func Exemplo()
    TCPStartup() ; Inicie o serviço TCP.

    ; Registre OnAutoItExit para ser chamado quando o script é fechado.
    OnAutoItExitRegister("OnAutoItExit")

    ; Atribua variáveis locais o endereço IP de loopback e a porta.
    Local $sIPAddress = "10.0.19.68" ; Este endereço IP só funciona para testar em seu próprio computador.
    Local $iPort = 80 ; Porta usada para a conexão.


    ; Atribua uma variável local ao soquete e conecte a um soquete de escuta com o endereço IP ea porta especificados.
    Local $iSocket = TCPConnect($sIPAddress, $iPort)

    ; Se um erro ocorrer exibir o código de erro e retornar Falso.
    If @error Then ; O servidor provavelmente está offline / a porta não está aberta no servidor.
        Local $iError = @error

;~     MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Não foi possível conectar-se, o código de erro:" & $iError )
        Return False
        $SmtpServer = "smtp-mail.outlook.com" ; address for the smtp-server to use - REQUIRED
        $FromName = "" ; name from who the email was sent
        $FromAddress = "" ; address from where the mail should come
        $ToAddress = "" ; destination address of the email - REQUIRED
        $subject = ""
        $body = ""
        $AttachFiles = ""
        $CcAddress = "" ; address for cc - leave blank if not needed
        $BccAddress = "" ; address for bcc - leave blank if not needed
        $Importance = "High" ; 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



        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
    Else

        MsgBox($MB_SYSTEMMODAL, "", "Conexão bem sucedida")
    EndIf
    ; Feche o soquete.
    TCPCloseSocket($iSocket)
EndFunc   ;==>Exemplo


Func OnAutoItExit()
    TCPShutdown() ; Feche o serviço TCP.
EndFunc   ;==>OnAutoItExit

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


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

 


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted

And you can simple use "Call()" to run the function.;)

Share this post


Link to post
Share on other sites

#4 ·  Posted

29 minutes ago, KickStarter15 said:

And you can simple use "Call()" to run the function.;)

.. Not sure how that solves the issue? ..  and on top of that: one should not use Call() unless a variable is used for function parameter.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#5 ·  Posted

Yap, that is what I did when I've got the same issue when running function within the function. Maybe that will not solve the issue but just a tip anyway.:sweating:

I have it like this:

Func SendClick()
    $VAR_SmtpServer = "domain.com"
    $FromName = "Name"
    $FromAddress = "FromAddress"
    $ToAddress = "Address"
    $Subject = GUICtrlRead($SubjectLine)
    $Body = "Text"
    $AttachFiles = "Address1"
    $CcAddress = "Address2"
    $Importance = "High"
    $IPPort = 25
    $ssl = 0
   Global $oMyRet[2]
   Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
   $rc = _INetSmtpMailCom($VAR_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
   Call("_INetSmtpMailCom")
EndFunc
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)
; something here
EndFunc

Great script you made on this Jos, thanks it helps me a lot.:D

Share this post


Link to post
Share on other sites

#6 ·  Posted

I am still lost what that Call() does in your posted example? You first run the Func _INetSmtpMailCom() as I would do and then later do a Call() ...why?

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#7 ·  Posted

Good morning friends, Thank you, you guys are the best!

I had an idea / doubt. Is there any way today to send a message via whatsapp?
Like this my application ... instead of sending by email send a message via whatsapp?

Share this post


Link to post
Share on other sites

#8 ·  Posted

And another question ... I need this email alert application to run another application on a different server ... Just run another script .. any suggestions?

Share this post


Link to post
Share on other sites

#10 ·  Posted

Very good ... that's what I needed ... and as for whatsapp any ideas?

Share this post


Link to post
Share on other sites

#11 ·  Posted

Whatsapp is pretty closed, but you could easily use the Telegram Bot functionality. I've see a UDF for it in the  Examples forum. :)

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#12 ·  Posted

What a squeeze I'm going through, for something so simple .. But I can not get the autoit to execute psexec in any way ....

What am I doing wrong?
In a .bat I get calm.

 

Run ("psexec -i -s -d \\10.0.19.68 -u machinename\administrator -p password calc")

How to test open the calculator!

Share this post


Link to post
Share on other sites

#13 ·  Posted

Try:

Run(@comspec & " /c psexec -i -s -d \\10.0.19.68 -u machinename\administrator -p password calc")

Use /k in stead of /c when only the cmd prompt flashes and nothing else.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#14 ·  Posted

'Psexec' is not recognized as an internal command
Or external, an operable program or a batch file.

Share this post


Link to post
Share on other sites

#16 ·  Posted

Not in .bat it runs on system32, already on that wheel in the script directory

Share this post


Link to post
Share on other sites

#18 ·  Posted

 

 

2017-05-12_140359.jpg

Share this post


Link to post
Share on other sites

#20 ·  Posted

like this?? .... Run (@comspec & "/ k psexec.exe -i -s -d \\ 10.0.19.68


Psexec is on system32

 

Share this post


Link to post
Share on other sites

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