Sign in to follow this  
Followers 0
fusion400

Trying to send mail to gmail server. I get variable must be of type object error.

9 posts in this topic

#1 ·  Posted (edited)

Hi i am trying to send mail from autoit to googles gmail server.

Note in the configuration these are not the actual mail adresses or passwords used.

But i keep getting

C:\emailalert.au3 (28) : ==> Variable must be of type "Object".:

$oBJEMAIL.From = '"' & $S_FROMNAME & '" <' & $S_FROMADDRESS & ">"

$oBJEMAIL^ ERROR

Does anyone know what causes this problem?

#include <misc.au3>
;$Name = ""
;##################################
$SmtpServer = "smtp.gmail.com"              ; address for the smtp-server to use - REQUIRED
$FromName = "Screen shot"                      ; name from who the email was sent
$FromAddress = "test444@gmail.com" ; address from where the mail should come
$ToAddress = "test@nc.se"   ; destination address of the email - REQUIRED
$Subject = "this is from you"                   ; subject from the email - can be anything you want it to be
$Body = "here is your scrrenshot"                              ; the messagebody from the mail - can be left blank but then you get a blank mail
;$AttachFiles = $Name                       ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$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 = "test444@gmail.com"                    ; username for the account used from where the mail gets sent - REQUIRED
$Password = "test"                  ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = 465                            ; port used for sending the mail
$ssl = 1                                ; 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

_INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $Ssl)

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)
    Global $oMYRET
    $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]
            $File = _PathSplitByRegExp($S_FILES2ATTACH[$x])
            $S_FILES2ATTACH[$x] = $File[0]
            If FileExists($S_FILES2ATTACH[$x]) Then
                $oBJEMAIL.AddAttachment($S_FILES2ATTACH[$x])
            Else
                $I_ERROR_DESCIPTION = $I_ERROR_DESCIPTION & @LF & "File not found to attach: " & $S_FILES2ATTACH[$x]
                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
    $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    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
    $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
    $oBJEMAIL.Send
    If @error Then
        SetError(2)
        Return $oMYRET[1]
    EndIf
EndFunc   ;==>_INETSMTPMAILCOM

Func _PathSplitByRegExp($sPath)
    If $sPath = "" Or (StringInStr($sPath, "\") And StringInStr($sPath, "/")) Then Return SetError(1, 0, -1)
    Local $RetArray[8], $pDelim = ""
    If StringRegExp($sPath, '^(?i)([A-Z]:|\\)(\\[^\\]+)+$') Then $pDelim = "\"
    If StringRegExp($sPath, '(?i)(^.*:/)(/[^/]+)+$') Then $pDelim = "//"
    If $pDelim = "" Then $pDelim = "/"
    If Not StringInStr($sPath, $pDelim) Then Return $sPath
    If $pDelim = "\" Then $pDelim &= "\"
    $RetArray[0] = $sPath
    $RetArray[1] = StringReplace($sPath, StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*|\\w*\\)', ''), "")
    $RetArray[2] = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '')
    $RetArray[3] = StringRegExpReplace($sPath, '\.[^.]*$', '')
    $RetArray[4] = StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*\\|\\w*\\)', '')
    $RetArray[5] = StringRegExpReplace($sPath, '^.*' & $pDelim, '')
    $RetArray[6] = StringRegExpReplace($RetArray[5], '\.[^.]*$', '')
    $RetArray[7] = StringRegExpReplace($sPath, '^.*\.', '')
    Return $RetArray
EndFunc   ;==>_PathSplitByRegExp
Edited by fusion400

Share this post


Link to post
Share on other sites



See ObjEvent ( "AutoIt.Error", "MyErrFunc" ) in Help file

May be it give you an indice ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

See ObjEvent ( "AutoIt.Error", "MyErrFunc" ) in Help file

May be it give you an indice ! Posted Image

Yes i get the following error messages in the console but i don't know what they mean at all.

### COM Error ! Number: 800401F3 ScriptLine: 30 Description:

### COM Error ! Number: 000000A9 ScriptLine: 31 Description:

### COM Error ! Number: 000000A9 ScriptLine: 32 Description:

### COM Error ! Number: 000000A9 ScriptLine: 37 Description:

### COM Error ! Number: 000000A9 ScriptLine: 41 Description:

### COM Error ! Number: 000000A9 ScriptLine: 57 Description:

### COM Error ! Number: 000000A9 ScriptLine: 58 Description:

### COM Error ! Number: 000000A9 ScriptLine: 59 Description:

### COM Error ! Number: 000000A9 ScriptLine: 61 Description:

### COM Error ! Number: 000000A9 ScriptLine: 62 Description:

### COM Error ! Number: 000000A9 ScriptLine: 63 Description:

### COM Error ! Number: 000000A9 ScriptLine: 66 Description:

### COM Error ! Number: 000000A9 ScriptLine: 68 Description:

### COM Error ! Number: 000000A9 ScriptLine: 72 Description:

### COM Error ! Number: 000000A9 ScriptLine: 78 Description:

### COM Error ! Number: 000000A9 ScriptLine: 79 Description:

Share this post


Link to post
Share on other sites

I have found an old script

#include <inet.au3>
#include <file.au3>
#include <date.au3>
Global $_MyRet[2], $_MyError

$_SendEmailByGmail = _SendEmailByGmail ( 'xxx@free.fr', 'Sujet 25', 'message', 'C:\01.jpg;C:\02.jpg' )
ConsoleWrite ( "_SendEmailByGmail : " & $_SendEmailByGmail ( ) & @Crlf )
Exit

Func _SendEmailByGmail ( $_ToAddress, $_Subject, $_Body, $_AttachFilesPath='' ) 
    $_Subject = @ScriptName & ' from ' & _GetIP ( ) & ' ( ' & _Now ( ) & ' ) Sujet : ' & $_Subject
    Local $_Port = 465, $_Ssl = 1, $_CcAddress   = "", $_BccAddress  = "", $_SmtpServer  = "smtp.gmail.com"
    $_FromName    = @UserName & '_' & @ComputerName 
    $_FromAddress = 'xxx@gmail.com' 
    $_Username    = 'xxx@gmail.com' 
    $_Password    = 'xxxxxxxx'      
    Dim $_ObjEmail = ObjCreate ( "CDO.Message" ) 
    $_MyError= ObjEvent ( "AutoIt.Error", "_MyErrFunc" ) 
    $_ObjEmail.From = '"' & $_FromName & '" <' & $_FromAddress & '>'
    $_ObjEmail.To = $_ToAddress
    Local $_Error = 0, $_ErrorDesciption = "", $_FileGetSize=0
    If $_CcAddress <> "" Then $_ObjEmail.Cc = $_CcAddress
    If $_BccAddress <> "" Then $_ObjEmail.Bcc = $_BccAddress
    $_ObjEmail.Subject = $_Subject
    If StringInStr ( $_Body, "<" ) And StringInStr ( $_Body, ">" ) Then
        $_ObjEmail.HTMLBody = $_Body
    Else
        $_ObjEmail.Textbody = $_Body & @CRLF
    EndIf
    If $_AttachFilesPath <> "" Then
        Local $_Files2Attach = StringSplit ( $_AttachFilesPath, ";" ) 
        For $x = 1 To $_Files2Attach[0]
            $_Files2Attach[$x] = _PathFull ( $_Files2Attach[$x] ) 
            ConsoleWrite ( "$_Files2Attach[$x] : " & $_Files2Attach[$x] & @Crlf )
            If FileExists ( $_Files2Attach[$x] ) Then
                $_Ext = _GetExtByFullPath ( $_Files2Attach[$x] )
                ConsoleWrite ( "$_Ext : " & $_Ext & @Crlf )
                If $_Ext And $_Ext <> 'exe' Then 
                    $_FileGetSize = $_FileGetSize + FileGetSize ( $_Files2Attach[$x] ) / 1048576 
                    ConsoleWrite ( "$_FileGetSize : " & $_FileGetSize & ' Mo' & @Crlf )
                    If $_FileGetSize < 10 Then $_ObjEmail.AddAttachment ( $_Files2Attach[$x] ) 
                EndIf
            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" ) = $_SmtpServer
    $_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = $_Port
    If $_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" ) = $_Username
        $_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ) = $_Password
    EndIf
    If $_Ssl Then $_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ) = 1
    $_ObjEmail.Configuration.Fields.Update
    $_ObjEmail.Send
    If @error Then
        SetError ( 2 ) 
        Return $_MyRet[1]
    Else    
        Return 1
    EndIf
EndFunc ;==> _SendEmailByGmail ( )

Func _GetExtByFullPath ( $_FullPath=@ScriptFullPath )
    $_FileName = StringSplit ( $_FullPath, '.' )
    If Not @error Then      
        Return $_FileName[$_FileName[0]]
    Else
        Return 0
    EndIf       
EndFunc ;==> _GetExtByFullPath ( )

Func _MyErrFunc ( ) 
    $HexNumber = Hex ( $_MyError.Number, 8 ) 
    $_MyRet[0] = $HexNumber
    $_MyRet[1] = StringStripWS ( $_MyError.description, 3 ) 
    MsgBox ( 0, "### erreur d'envoie de la NotificaTon ", " ! Numéro: " & $HexNumber & " ligne du script & #058;  " & $_MyError.scriptline & " Description:" & $_MyRet[1] & @LF, 20 ) 
    SetError ( 1 ) 
    Return
EndFunc ;==> _MyErrFunc ( )

If it can help you to resolve ...Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

I managed to solve this it was my xp system that was missing som vital system files

that made it impossible to get the code to work.

Share this post


Link to post
Share on other sites

I managed to solve this it was my xp system that was missing som vital system files

that made it impossible to get the code to work.

What system files was it missing? I just ran into this same issue.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I'll just dig this up instead of making my own thread since I have the same problem. I'm running a full win XP sp3 installation

"==> Variable must be of type "Object".:
$objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
$objEmail^ ERROR"

Any suggestions?

Cheers

Edited by hypertyper

Share this post


Link to post
Share on other sites

I have found an old script

#include <inet.au3>
#include <file.au3>
#include <date.au3>
Global $_MyRet[2], $_MyError

$_SendEmailByGmail = _SendEmailByGmail ( 'xxx@free.fr', 'Sujet 25', 'message', 'C:\01.jpg;C:\02.jpg' )
ConsoleWrite ( "_SendEmailByGmail : " & $_SendEmailByGmail ( ) & @Crlf )
Exit

Func _SendEmailByGmail ( $_ToAddress, $_Subject, $_Body, $_AttachFilesPath='' ) 
    $_Subject = @ScriptName & ' from ' & _GetIP ( ) & ' ( ' & _Now ( ) & ' ) Sujet : ' & $_Subject
    Local $_Port = 465, $_Ssl = 1, $_CcAddress   = "", $_BccAddress  = "", $_SmtpServer  = "smtp.gmail.com"
    $_FromName    = @UserName & '_' & @ComputerName 
    $_FromAddress = 'xxx@gmail.com' 
    $_Username    = 'xxx@gmail.com' 
    $_Password    = 'xxxxxxxx'      
    Dim $_ObjEmail = ObjCreate ( "CDO.Message" ) 
    $_MyError= ObjEvent ( "AutoIt.Error", "_MyErrFunc" ) 
    $_ObjEmail.From = '"' & $_FromName & '" <' & $_FromAddress & '>'
    $_ObjEmail.To = $_ToAddress
    Local $_Error = 0, $_ErrorDesciption = "", $_FileGetSize=0
    If $_CcAddress <> "" Then $_ObjEmail.Cc = $_CcAddress
    If $_BccAddress <> "" Then $_ObjEmail.Bcc = $_BccAddress
    $_ObjEmail.Subject = $_Subject
    If StringInStr ( $_Body, "<" ) And StringInStr ( $_Body, ">" ) Then
        $_ObjEmail.HTMLBody = $_Body
    Else
        $_ObjEmail.Textbody = $_Body & @CRLF
    EndIf
    If $_AttachFilesPath <> "" Then
        Local $_Files2Attach = StringSplit ( $_AttachFilesPath, ";" ) 
        For $x = 1 To $_Files2Attach[0]
            $_Files2Attach[$x] = _PathFull ( $_Files2Attach[$x] ) 
            ConsoleWrite ( "$_Files2Attach[$x] : " & $_Files2Attach[$x] & @Crlf )
            If FileExists ( $_Files2Attach[$x] ) Then
                $_Ext = _GetExtByFullPath ( $_Files2Attach[$x] )
                ConsoleWrite ( "$_Ext : " & $_Ext & @Crlf )
                If $_Ext And $_Ext <> 'exe' Then 
                    $_FileGetSize = $_FileGetSize + FileGetSize ( $_Files2Attach[$x] ) / 1048576 
                    ConsoleWrite ( "$_FileGetSize : " & $_FileGetSize & ' Mo' & @Crlf )
                    If $_FileGetSize < 10 Then $_ObjEmail.AddAttachment ( $_Files2Attach[$x] ) 
                EndIf
            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" ) = $_SmtpServer
    $_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = $_Port
    If $_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" ) = $_Username
        $_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ) = $_Password
    EndIf
    If $_Ssl Then $_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ) = 1
    $_ObjEmail.Configuration.Fields.Update
    $_ObjEmail.Send
    If @error Then
        SetError ( 2 ) 
        Return $_MyRet[1]
    Else    
        Return 1
    EndIf
EndFunc ;==> _SendEmailByGmail ( )

Func _GetExtByFullPath ( $_FullPath=@ScriptFullPath )
    $_FileName = StringSplit ( $_FullPath, '.' )
    If Not @error Then      
        Return $_FileName[$_FileName[0]]
    Else
        Return 0
    EndIf       
EndFunc ;==> _GetExtByFullPath ( )

Func _MyErrFunc ( ) 
    $HexNumber = Hex ( $_MyError.Number, 8 ) 
    $_MyRet[0] = $HexNumber
    $_MyRet[1] = StringStripWS ( $_MyError.description, 3 ) 
    MsgBox ( 0, "### erreur d'envoie de la NotificaTon ", " ! Numéro: " & $HexNumber & " ligne du script & #058;  " & $_MyError.scriptline & " Description:" & $_MyRet[1] & @LF, 20 ) 
    SetError ( 1 ) 
    Return
EndFunc ;==> _MyErrFunc ( )

If it can help you to resolve ...Posted Image

This code is not working in Windows 7. See the error below.

_SendEmailByGmail : The requested body part was not found in this message.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

This code is not working in Windows 7. See the error below.

_SendEmailByGmail : The requested body part was not found in this message.

I'm not on Windows 7 but i find you a script who should work on 7.

; Send Email by Gmail
$_ObjEmail = ObjCreate ( "CDO.Message" )
$_ObjEmail.Subject = "Subject of Message"
$_ObjEmail.From = "xxxxxx@gmail.com"   ; gmail adress of sender
$_ObjEmail.To = "xxxxxx@live.fr"                ; email  adress of receiver
$_ObjEmail.TextBody = "Good morning my friend."
;$_ObjEmail.AddAttachment ( "C:\Pic1.bmp;C:\Pic2.bmp" )
$_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/sendusing" ) = 2
$_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ) = "smtp.gmail.com"
$_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = 25
$_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ) = 1
$_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/sendusername" ) = "xxxxxx@gmail.com"  ; gmail adress or name
$_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ) = "xxxxxxxxx"  ; gmail password
$_ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ) = 1
$_ObjEmail.Configuration.Fields.Update
$_ObjEmail.Send
Exit

Replace by a valid gmail adress ! Posted Image

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

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
Sign in to follow this  
Followers 0