Jump to content

Recommended Posts

Posted

I strongly suspect this has been raised before but when you search on smtp you get so many results.

I have the following problem, I can send mail but the message body doesn't come through. I've posted my code.

Is it because _INetSmtpMail is expecting an array for the body value??

#include <INet.au3>

$SmtpServer = "mx.smtp.abc.xyz"
$FromName = "My Mailbox"
$FromAddress = "myaddress@smtp.abc.xyz"
$ToAddress = "youraddress@smtp.abc.xyz"
$Subject = "My Test email"
$Body = "MESSAGE BODY."
$helo = "domain_name"

$Mail = _INetSmtpMail ($SmtpServer,$FromName,$FromAddress,$ToAddress,$Subject,$Body,$helo)

$err = @error

If $Mail = 1 Then
MsgBox(0, "Mail Sent", "Mail sent successfully.")
Else
MsgBox(0, "Error!", "Mail failed with error code." & $err)
EndIf
Posted

Great.

You know, you could make SMTP object out of that function.

There are several advantages with object approach. For example you introduce only one variable - object, instead of the whole set ($SmtpServer, $FromName, ...). This is especially convenient when there are repetitive tasks within the script.

#include "AutoitObject.au3"
#include <INet.au3>

Opt("MustDeclareVars", 1)

; Error monitoring
Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
; Initialize AutoItObject
_AutoItObject_StartUp()
; This is new in AutoIt
Global $oSMTP = _SmtpMailObject()


Global $aBody[1] = ["Test message"]

With $oSMTP
    .Server = "mx.smtp.abc.xyz"
    .FromName = "My Mailbox"
    .FromAddress = "myaddress@smtp.abc.xyz"
    .ToAddress = "youraddress@smtp.abc.xyz"
    .Subject = "My Test email"
    .Body = $aBody
    .Helo = "domain_name"
    .Trace = True
EndWith


; Send mail
ConsoleWrite("+Return value = " & $oSMTP.Send & @CRLF)

; Check error
ConsoleWrite("!Error number = " & $oSMTP.__error__ & @CRLF)

$oSMTP = 0



Func _SmtpMailObject()
    Local $oObj = _AutoItObject_Create()
    _AutoItObject_AddMethod($oObj, "Send", "_SmtpMailObject_InetSmtpMail")
    _AutoItObject_AddProperty($oObj, "Server")
    _AutoItObject_AddProperty($oObj, "FromName")
    _AutoItObject_AddProperty($oObj, "FromAddress")
    _AutoItObject_AddProperty($oObj, "ToAddress")
    _AutoItObject_AddProperty($oObj, "Subject")
    _AutoItObject_AddProperty($oObj, "Body")
    _AutoItObject_AddProperty($oObj, "Helo")
    _AutoItObject_AddProperty($oObj, "First", $ELSCOPE_PUBLIC, " ")
    _AutoItObject_AddProperty($oObj, "Trace", $ELSCOPE_PUBLIC, False)
    Return $oObj
EndFunc   ;==>_SmtpMailObject

Func _SmtpMailObject_InetSmtpMail($oSelf)
    With $oSelf
        Local $iOut = _INetSmtpMail(.Server, .FromName, .FromAddress, .ToAddress, .Subject, .Body, .Helo, .First, .Trace)
    EndWith
    Return SetError(@error, 0, $iOut)
EndFunc   ;==>_SmtpMailObject_InetSmtpMail


Func _ErrFunc()
    ; Normally you wouldn't print anything here. Line below is just for possible debugging. The 'final product' lacks that line.
    ConsoleWrite("! COM Error !  Number: 0x" & Hex($oError.number, 8) & "   ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
    Return
EndFunc   ;==>_ErrFunc

♡♡♡

.

eMyvnE

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
×
×
  • Create New...