Sign in to follow this  
Followers 0
erik7426

Help with SMTP mail

18 posts in this topic

Let me start by saying this was working. We just migrated from Novel Groupwise to Exchange.

On Groupwise the following worked to send mail...

#include <_INetSmtpMailCom.au3>
Global $SmtpServer = "111.22.33.44" ; address for the smtp-server to use - REQUIRED
Global $FromName = "Test Server" ; name from who the email was sent
Global $FromAddress = "Help-Desk@nomail.com" ; address from where the mail should come
Global $ToAddress = "recipient@nomail.com" ; destination address of the email - REQUIRED
Global $Subject = "TESTING" ; subject from the email - can be anything you want it to be
Global $Body = "This is a test of the emergency broadcast system." ; the messagebody from the mail - can be left blank but then you get a blank mail
Global $AttachFiles = "" ; the file you want to attach- leave blank if not needed
Global $CcAddress = "" ; address for cc - leave blank if not needed
Global $BccAddress = "" ; address for bcc - leave blank if not needed
Global $Importance = "Normal" ; Send message priority: "High", "Normal", "Low"
Global $Username = "user" ; username for the account used from where the mail gets sent - REQUIRED
Global $Password = "pass" ; password for the account used from where the mail gets sent - REQUIRED
Global $IPPort = 25 ; port used for sending the mail
Global $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

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

Since the switch to exchange the above code yields the attached error.post-13077-0-42360000-1366292249_thumb.j

I don't know if it is relevant or not, but previously SMTP traffic was sent to the "gwia" server. Since the switch to Exchange I was instructed to send SMTP traffic to the iis server.

Before anyone jumps on the 'Exchange/iis isn't configured properly bandwagon' I would like to point out that the following code DOES work with Exchange...

$sUser = "";YourEmail
$sPass = "" ;YourPassword
$sServer = "smtp.live.com"
$sFrom = "";YourEmail
$sTo = "" ;ToEmail
$sPort = 25
$sObject = "Test"
$sMessage = "MyMessage"
_SendMail(_Base64Encode($sUser), _Base64Encode($sPass), $sServer, $sFrom, $sTo, $sPort, $sObject, $sMessage)
Func _SendMail($user, $pass, $server, $from, $to, $port = 25, $obj = "object", $msg = "message", $visibile = False)
$msg = StringReplace($msg, @CRLF & "." & @CRLF, @CRLF & "," & @CRLF)
$limit_time = 15000
If $visibile = True Then SplashTextOn("Send Email", "Starting Connection...", 200, 100)
TCPStartup()
$main_sock = TCPConnect(TCPNameToIP($server), $port)
$timer = TimerInit()
While TimerDiff($timer) < $limit_time
     $rec = TCPRecv($main_sock, 1000)
     If $rec <> "" Then ExitLoop
WEnd
$timer = 0
If $rec = "" Then Return SetError(1, 0, 0)
If $visibile = True Then SplashTextOn("Send Email", "Connection OK!" & @CRLF & "Dialog with server...", 200, 100)
TCPSend($main_sock, "HELO Email" & @CRLF)
$timer = TimerInit()
While TimerDiff($timer) < $limit_time
     $rec = TCPRecv($main_sock, 1000)
     If $rec <> "" Then ExitLoop
WEnd
$timer = 0
If $rec = "" Then Return SetError(1, 0, 0)
If $visibile = True Then SplashTextOn("Send Email", "Connection OK!" & @CRLF & "Login...", 200, 100)
TCPSend($main_sock, "AUTH LOGIN" & @CRLF)
$timer = TimerInit()
While TimerDiff($timer) < $limit_time
     $rec = TCPRecv($main_sock, 1000)
     If $rec <> "" Then ExitLoop
WEnd
$timer = 0
If $rec = "" Then Return SetError(1, 0, 0)
TCPSend($main_sock, $user & @CRLF)
$timer = TimerInit()
While TimerDiff($timer) < $limit_time
     $rec = TCPRecv($main_sock, 1000)
     If $rec <> "" Then ExitLoop
WEnd
$timer = 0
TCPSend($main_sock, $pass & @CRLF)
$timer = TimerInit()
While TimerDiff($timer) < $limit_time
     $rec = TCPRecv($main_sock, 1000)
     If $rec <> "" Then ExitLoop
WEnd
$timer = 0
If $rec = "" Then Return SetError(1, 0, 0)
If $visibile = True Then SplashTextOn("Send Email", "Connection OK!" & @CRLF & "Sending email...", 200, 100)
TCPSend($main_sock, "MAIL FROM:<" & $from & ">" & @CRLF)
$timer = TimerInit()
While TimerDiff($timer) < $limit_time
     $rec = TCPRecv($main_sock, 1000)
     If $rec <> "" Then ExitLoop
WEnd
$timer = 0
If $rec = "" Then Return SetError(1, 0, 0)
TCPSend($main_sock, "RCPT TO:<" & $to & ">" & @CRLF)
$timer = TimerInit()
While TimerDiff($timer) < $limit_time
     $rec = TCPRecv($main_sock, 1000)
     If $rec <> "" Then ExitLoop
WEnd
$timer = 0
TCPSend($main_sock, "DATA" & @CRLF)
$timer = TimerInit()
While TimerDiff($timer) < $limit_time
     $rec = TCPRecv($main_sock, 1000)
     If $rec <> "" Then ExitLoop
WEnd
$timer = 0
If $rec = "" Then Return SetError(1, 0, 0)
TCPSend($main_sock, "SUBJECT:" & $obj & @CRLF)
TCPSend($main_sock, "" & @CRLF)
TCPSend($main_sock, $msg & @CRLF & "." & @CRLF)
$timer = TimerInit()
While TimerDiff($timer) < $limit_time
     $rec = TCPRecv($main_sock, 1000)
     If $rec <> "" Then ExitLoop
WEnd
$timer = 0
If $rec = "" Then Return SetError(1, 0, 0)
If $visibile = True Then SplashOff()
TCPCloseSocket($main_sock)
TCPShutdown()
Return $rec
EndFunc ;==>_SendMail
Func _Base64Encode($Data, $LineBreak = 76)
Local $Opcode = "0x5589E5FF7514535657E8410000004142434445464748494A4B4C4D4E4F505152535455565758595A6162636465666768696A6B6C6D6E6F707172737475767778797A303132333435363738392B2F005A8B5D088B7D108B4D0CE98F0000000FB633C1EE0201D68A06880731C083F901760C0FB6430125F0000000C1E8040FB63383E603C1E60409C601D68A0688470183F90176210FB6430225C0000000C1E8060FB6730183E60FC1E60209C601D68A06884702EB04C647023D83F90276100FB6730283E63F01D68A06884703EB04C647033D8D5B038D7F0483E903836DFC04750C8B45148945FC66B80D0A66AB85C90F8F69FFFFFFC607005F5E5BC9C21000"
Local $CodeBuffer = DllStructCreate("byte[" & BinaryLen($Opcode) & "]")
DllStructSetData($CodeBuffer, 1, $Opcode)
$Data = Binary($Data)
Local $Input = DllStructCreate("byte[" & BinaryLen($Data) & "]")
DllStructSetData($Input, 1, $Data)
$LineBreak = Floor($LineBreak / 4) * 4
Local $OputputSize = Ceiling(BinaryLen($Data) * 4 / 3)
$OputputSize = $OputputSize + Ceiling($OputputSize / $LineBreak) * 2 + 4
Local $Ouput = DllStructCreate("char[" & $OputputSize & "]")
DllCall("user32.dll", "none", "CallWindowProc", "ptr", DllStructGetPtr($CodeBuffer), _
         "ptr", DllStructGetPtr($Input), _
         "int", BinaryLen($Data), _
         "ptr", DllStructGetPtr($Ouput), _
         "uint", $LineBreak)
Return DllStructGetData($Ouput, 1)
EndFunc ;==>_Base64Encode

The problem I have with the above code is that it does NOT allow me to attach files.

As a side note, I have tried the OutlookEx UDF which does work. However, it sends the mail from my account and I need to be able to send the mail from specific email address.

Any help would be greatly appreciated. I would prefer to continue using _INetSmtpMailCom.au3 if possible.

Share this post


Link to post
Share on other sites



Run the script uncompiled to see where the error is coming from.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Uncompiled yields error at line 81

Which looks like it corresponds to $objEmail.Send in _INetSmtpMailCom.au3

Share this post


Link to post
Share on other sites

What's the WIndows version the client is running?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

What's the WIndows version the client is running?

XP Pro SP3

Edited by erik7426

Share this post


Link to post
Share on other sites

Can you please add a COM error handler to your script so we get more detailed error information? Have a look at the help file for function ObjEvent.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Can you please add a COM error handler to your script so we get more detailed error information? Have a look at the help file for function ObjEvent.

_INetSmtpMailCom.au3 already had a COM error handler in it. The output was in the attachment on my first post.

I did go to the help file and replaced the existing COM error handler in _INetSmtpMailCom.au3 with the one from help.

The output looks like it has the same info to me. See attached screen shot post-13077-0-35647800-1366300337_thumb.j

Line 86 referenced in the error corresponds to

$objEmail.Send
in the _INetSmtpMailCom function

Share this post


Link to post
Share on other sites

The transport error code can have multiple reasons. One of them being account information (username/domain/password) is incorrect


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

As you migrated to Exchange server, do you have Outlook installed on the clients?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

As you migrated to Exchange server, do you have Outlook installed on the clients?

Yes, Outlook is installed on client PCs

Share this post


Link to post
Share on other sites

Can you also show the code in "_INetSmtpMailCom.au3" ?

This is my test code in it's entirety..

Global $SmtpServer = "111.22.33.44" ; address for the smtp-server to use - REQUIRED
Global $FromName = "Test Server" ; name from who the email was sent
Global $FromAddress = "HelpDesk@nomail.com" ; address from where the mail should come
Global $ToAddress = "recipient@nomail.com" ; destination address of the email - REQUIRED
Global $Subject = "TESTING" ; subject from the email - can be anything you want it to be
Global $Body = "This is a test of the emergency broadcast system." ; the messagebody from the mail - can be left blank but then you get a blank mail
Global $AttachFiles = "" ; the file you want to attach- leave blank if not needed
Global $CcAddress = "" ; address for cc - leave blank if not needed
Global $BccAddress = "" ; address for bcc - leave blank if not needed
Global $Importance = "Normal" ; Send message priority: "High", "Normal", "Low"
Global $Username = "user" ; username for the account used from where the mail gets sent - REQUIRED
Global $Password = "pass" ; password for the account used from where the mail gets sent - REQUIRED
Global $IPPort = 25 ; port used for sending the mail
Global $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
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "ErrFunc")
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
If @error Then
$mailError = @error
Sleep(1000)
SplashOff()
;MsgBox(0,"",$rc)
;MsgBox(16, "Error sending message", "Error code:" & $mailError)
Sleep(1500)
Sleep(1000)
EndIf
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
;
;
; 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
Func ErrFunc()
Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"     & @CRLF & @CRLF & _
             "err.description is: " & @TAB & $oMyError.description & @CRLF & _
             "err.windescription:"   & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "       & @TAB & hex($oMyError.number,8) & @CRLF & _
             "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
             "err.scriptline is: "   & @TAB & $oMyError.scriptline   & @CRLF & _
             "err.source is: "       & @TAB & $oMyError.source       & @CRLF & _
             "err.helpfile is: "     & @TAB & $oMyError.helpfile     & @CRLF & _
             "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
         )
Local $err = $oMyError.number
If $err = 0 Then $err = -1
$g_eventerror = $err ; to check for after this function returns
Endfunc

myErrFunc was the original error handler in _INetSmtpMailCom.au3

ErrFunc is the error handler I copied from Help.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Do you actually have a username and password filled in? or tried without it as that shouldn't be needed?

Jos

Edited by 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

Do you actually have a username and password filled in? or tried without it as that shouldn't be needed?

Jos

I've tried it with and without.

Share this post


Link to post
Share on other sites

mmm strange. just for the heck of it try this line to call the UDF:

$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body)

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

As an alternative you could try my OutlookEX UDF to send emails.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

One other thing to find out is if/how authentication is configured on your exchange server. When SMTP requires authentication, you also need to find out whether it is Basic or NTLM.

The current UDF uses Basic. To try NTLM just fill in the porper username and password and change this line:

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

Jos

Edited by 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

Hi.

  • Take a LAN trace, (wireshark), set the filter "tcp.port==25" and see, what's going on packet side.
  • try start, run, "telnet ipaddres-your-mail-system 25", helo some.domain, mail from: <test@test.de>, rcpt to <john@theripper.com>, data, test, test, test, .,
  • the above has to "include" the "<" and ">", every comma = RETURN
  • My personal suggestion: Use Blat.[exe|dll], it simply works ;)

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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