pytrelooc
Goto Top

Autoit Smtp Mail versenden

Hallo zusammen,

ich versuche gerade eine E-Mail über einen Smtp-Server zu versenden.
Hierzu habe ich folgenden Code benutzt:


        Local $s_SmtpServer = "XXXXXXX"  
	Local $s_FromName = "XXXXXX"  
	Local $s_FromAddress = "XXXXX"  
	Local $s_ToAddress = "XXXXX"  
	Local $s_Subject = "Das ist ein Test"  
	Local $as_Body[2]
	$as_Body = "AD Email Text"  
	$as_Body[1] = "Zeile2"  
	Local $Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body,@ComputerName ,-1,1)
	Local $err = @error
	If $Response = 1 Then
		MsgBox(0, "Success!", "Mail sent")  
	Else
		MsgBox(0, "Error!", "Mail failed with error code " & $err)  
	EndIf

So funktioniert das Senden der Mail, wenn ich jedoch das @computername weglasse durch welches ich ein Protokoll des Sendeverlaufs bekomme dann schlägt das Senden der Mail fehl. Woran liegt das ?

Vielleicht könnt ihr mir weiterhelfen face-smile

Mit freundlichen Grüßen

pytrelooc

Content-Key: 226759

Url: https://administrator.de/contentid/226759

Ausgedruckt am: 28.03.2024 um 21:03 Uhr

Mitglied: colinardo
Lösung colinardo 15.01.2014 aktualisiert um 15:36:40 Uhr
Goto Top
Hi pytrelooc,
wenn du für den Parameter einen leeren String als ("") angibst wird von der Funktion automatisch der Computername verwendet.

Hier sind die ersten Zeilen der Funktion, dort siehst du das wenn man einen leeren String übergibt die Funktion automatisch den Computernamen auf die Variable setzt:
Func _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_helo = "", $s_first = " ", $b_trace = 0)  
	If $s_SmtpServer = "" Or $s_FromAddress = "" Or $s_ToAddress = "" Or $s_FromName = "" Or StringLen($s_FromName) > 256 Then Return SetError(1, 0, 0)  
	If $s_helo = "" Then $s_helo = @ComputerName  
...
...
komplett auslassen kannst du den Parameter nur, wenn du keine Parameter nach diesem definierst.

Falls du mal eine Mail-Funktion benötigst die SMTP-Authentifizierung und TLS-Verschlüsselung unterstützt guckst du hier face-wink
; --- Mail Einstellungen ----- 
$MAILFROM = "absender@email.de"  
$MAILTO = "empfänger@email.de"  
$MAILFROMNAME = "Absendername"  
$MAILBODY = "Der Body der Nachricht"  
$MAILSUBJECT = "Der Betreff der Nachricht"  
$MAILATTACHMENT = ""  
$MAILSERVER = "SMTP.SERVER.DE"  
$SMTPUSER = "SMTPUSERNAME"  
$SMTPPASS = "SMTPPASSWORD"  
$SMTPPORT = "25"  
$SMTPUSESSL = 1
; ------------------------------

; --- Send Mail
_INetSmtpMailCom($MAILSERVER,$MAILFROMNAME,$MAILFROM,$MAILTO,$MAILSUBJECT,$MAILBODY,$MAILATTACHMENT,"","","Normal",$SMTPUSER,$SMTPPASS,$SMTPPORT,$SMTPUSESSL)  


; Variables for Function _INetSmtpMailCom
;=========================================
;~ $SmtpServer = ""                 ; address for the smtp-server to use - REQUIRED  
;~ $FromName = "User"               ; name from who the email was sent  
;~ $FromAddress = ""                ; address from where the mail should come  
;~ $ToAddress = ""                  ; destination address of the email - REQUIRED  
;~ $Subject = "testsubject"         ; subject from the email - can be anything you want it to be  
;~ $Body = "This Is The Body"       ; 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 = ""                  ; 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=0                           ; put to 1 if using https
;=========================================

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
            $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)
    EndIf
    $objEmail=""  
EndFunc   ;==>_INetSmtpMailCom

Grüße Uwe
Mitglied: pytrelooc
pytrelooc 15.01.2014 um 14:20:41 Uhr
Goto Top
Hallo,

danke für deine Hilfe !
bedeutet das es gibt keine Möglichkeit diesen Parameter wegzulassen oder habe ich dich falsch verstanden ?
Mit Passworteingabe usw. soll es nicht sein da der User mit einem klick diese Mail versenden soll (mit vorgefertigtem Text usw.) und nichts davon mitbekommen soll.
Dies habe ich schon alles gelöst (über ActiveDirectory die Mails ausgelesen usw). Das einzige was der User noch mitbekommt ist nun dieses Protokollfenster was jedoch nicht so sein soll.

Grüße
Mitglied: colinardo
Lösung colinardo 15.01.2014 aktualisiert um 15:36:39 Uhr
Goto Top
Zitat von @pytrelooc:
bedeutet das es gibt keine Möglichkeit diesen Parameter wegzulassen oder habe ich dich falsch verstanden ?
einfach wie geschrieben einen leeren String übergeben => ""
ansonsten kann man sich die Funktion zurechtstutzen wenn das benötigt wird...
Mitglied: pytrelooc
pytrelooc 15.01.2014 um 14:32:05 Uhr
Goto Top
Wenn ich das so mache funktioniert das aber nicht.
Oder wie muss ich den String übergeben ?

        Local $s_SmtpServer = "XXXXXXX"  
	Local $s_FromName = "XXXXXX"  
	Local $s_FromAddress = "XXXXX"  
	Local $s_ToAddress = "XXXXX"  
        Local $s_helo = ""  
	Local $s_Subject = "Das ist ein Test"  
	Local $as_Body[2]
	$as_Body = "AD Email Text"  
	$as_Body[1] = "Zeile2"  
	Local $Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_helo)
	Local $err = @error
	If $Response = 1 Then
		MsgBox(0, "Success!", "Mail sent")  
	Else
		MsgBox(0, "Error!", "Mail failed with error code " & $err)  
	EndIf
Mitglied: colinardo
Lösung colinardo 15.01.2014 aktualisiert um 15:36:37 Uhr
Goto Top
nach deiner obigen Benutzung sollte es so aber gehen.. ansonsten hast du eventuell eine ältere AutoIT-Version ?
Local $Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, "",-1,1)   
Mitglied: pytrelooc
pytrelooc 15.01.2014 um 15:05:22 Uhr
Goto Top
Ah das "-1,1" hat gefehlt, jedoch erscheint nun immer noch dieses SmtP-Trace Fenster.
Kann man das irgendwie entfernen ?
Mitglied: colinardo
Lösung colinardo 15.01.2014 aktualisiert um 15:36:35 Uhr
Goto Top
Zitat von @pytrelooc:

Ah das "-1,1" hat gefehlt, jedoch erscheint nun immer noch dieses SmtP-Trace Fenster.
Kann man das irgendwie entfernen ?

klar, indem du den letzten Parameter weg lässt , also die letzte 1

sieh dir doch mal die Doku zur Funktion an:
http://www.autoitscript.com/autoit3/docs/libfunctions/_INetSmtpMail.htm
Mitglied: pytrelooc
pytrelooc 15.01.2014 um 15:36:31 Uhr
Goto Top
Die habe ich mir angesehen jedoch konnte ich das dort nich rauslesen.
Auf jeden fall funktioniert jetzt alles wie es soll.
Vielen Dank für deine Hilfe !

Gruß

Pytrelooc