Jump to content

Smtp Mailer That Supports Html And Attachments.


Jos
 Share

Recommended Posts

  • Developers

I cannot test this myself, but SMTP settings look good at first glance.

Have you tried it with a Gmail account when to say you tested with the Gmail settings?

Does these settings work when you try it in Outlook to ensure you don't have a firewall blocking the port?

Jos

EDIT: Looks like your parameters do not match up... you might want to check that first: You missed $S_AttacheFiles.

$SndEml = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $EmlBody, $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)
Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 3 weeks later...

Hi all,
I'm using a slight variant of the template script and am having some trouble with the File attachments accepting wildcards.

I've made a change whereby i can get the variables for the email account from a .INI file ( works great ).

My issue is, if I use a wildcard in the '$AttachFiles' variable, it doesn't attach the files to the email. i.e. i have to state the name of the file exactly - which works fine. i.e. I can't put "*.Gif" or "C:myfiles*.gif"

I've also added a few lines that will pre-pend a SENT prefix and move the files to another directory.

This functionality picks up the wildcard entry via the $AttachFiles variable without a problem, as the files indeed get moved into the new directory & renamed. I also get an email, just no attachments :/

I'm very new to AutoIT and quite new to programming in general.

Im using this script in conjunction with another application which takes a screenshot, saves it in the directory and the invokes this SMTP mail script to email me the image.

here is the code:
 

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.2.12.1
Author:      myName
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
;
;##################################
; Include
;##################################
#Include<file.au3>
;##################################
; Variables
;##################################
$SmtpServer = IniRead("Alerter_Mail.ini", "mail_settings", "SmtpServer", "NotFound"); address for the smtp-server to use - REQUIRED
$FromName = IniRead("Alerter_Mail.ini", "mail_settings", "FromName", "NotFound"); name from who the email was sent
$FromAddress = IniRead("Alerter_Mail.ini", "mail_settings", "FromAddress", "NotFound"); address from where the mail should come
$ToAddress = IniRead("Alerter_Mail.ini", "mail_settings", "ToAddress", "NotFound"); destination address of the email - REQUIRED
$Subject = IniRead("Alerter_Mail.ini", "mail_settings", "Subject", "NotFound"); subject from the email - can be anything you want it to be
$Body = IniRead("Alerter_Mail.ini", "mail_settings", "Body", "NotFound"); the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = IniRead("Alerter_Mail.ini", "mail_settings", "AttachFiles", "NotFound"); the file you want to attach- leave blank if not needed
$CcAddress = IniRead("Alerter_Mail.ini", "mail_settings", "CcAddress", "NotFound"); address for cc - leave blank if not needed
$BccAddress = IniRead("Alerter_Mail.ini", "mail_settings", "BccAddress", "NotFound"); address for bcc - leave blank if not needed
$Importance = IniRead("Alerter_Mail.ini", "mail_settings", "Importance", "NotFound"); Send message priority: "High", "Normal", "Low"
$Username = IniRead("Alerter_Mail.ini", "mail_settings", "Username", "NotFound"); username for the account used from where the mail gets sent - REQUIRED
$Password = IniRead("Alerter_Mail.ini", "mail_settings", "Password", "NotFound"); password for the account used from where the mail gets sent - REQUIRED
$IPPort = IniRead("Alerter_Mail.ini", "mail_settings", "IPPort", "NotFound"); port used for sending the mail
$ssl = IniRead("Alerter_Mail.ini", "mail_settings", "ssl", "NotFound"); 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
;##################################
; Script
;##################################
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
$sFileOld = $AttachFiles
$sFileRenamed = "Sent\SENT_"&$AttachFiles
FileMove($sFileOld, $sFileRenamed,9)
;
;
; 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
Edited by Jos
Link to comment
Share on other sites

OK, so i fixed one thing and broke another :)

I inserted the code:

$a_files = _FileListtoArray("C:\myfiles", "*.gif", 1)
$AttachFiles = _ArrayToString($a_files, ";", 1)

which works great, now i get all the *.gif files attached.

But it's now broken the FileMove code i had further down the script, which was:

$sFileOld = $AttachFiles
$sFileRenamed = "Sent\SENT_"&$AttachFiles
FileMove($sFileOld, $sFileRenamed,9)

I've also tried flat out explicit directory path & Wildcards, but that also doesn't work. Even though the Function List entry for FileMove states it does. AND it did when the $AttachFiles variable was just the plain text from the .INI file. Very strange.

Link to comment
Share on other sites

  • 3 weeks later...

Hey Jos,

Tried using CDO Message lately and i got some problems. This is my code. It worked to someone else but doesn't seem to work on my pc

$SmtpServer = "smtp.live.com"
$FromName = "user"
$FromAddress = "test@hotmail.com" ;Your email
$ToAddress = "test2@hotmail.com" ; TO email
$Subject = "Userinfo" ; subject
$Body = "try" ; text
$AttachFiles = ""
$CcAddress = ""
$BccAddress = ""
$ssl = 1 ; SSL
$Port = 25
$Importance = "Normal"
$Username = "test@hotmail.com" ; Email
$Password = "code" ; Password
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
EmailSend($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $CcAddress, $BccAddress, $Username, $Password, $Port, $ssl)
Func EmailSend($sSmtpServer, $sFromName, $sFromAddress, $sToAddress, $sSubject = "", $sBody = "",  $s_CcAddress = "", $s_BccAddress = "", $sUsername = "", $sPassword = "", $sPort = 25, $Sssl = 0)
 $Message = ObjCreate("CDO.Message")
 $config = ObjCreate("CDO.Configuration")
 $field = $config.Fields
With $field
 .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $SmtpServer
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $Port
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
 .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $Username
 .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $Password
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = $ssl
 .Update()
EndWith
With $Message
 .Configuration = $config
 .From = '"' & $FromName & '" <' & $FromAddress & '>'
 .To = $ToAddress
 .Subject = $Subject
 .TextBody = $Body
 .Send()
EndWith
EndFunc

My error from your function is this: "### COM Error ! Number: 80020009 ScriptLine: 43 Description:The transport failed to connect to the server."

As i found on internet that error could be because of these:

  • Is a valid SMTP Server
  • Check to be sure the server System.Web.Mail is executing on can connect to the mail server. Some times firewalls or proxy servers can get in the way.
  • Try specifying the value by IP address. Poor DNS resolution can sometimes hinder name lookups.
  • Make sure the that the mail server is running at port 25.
  • If you did not specify a SmtpMail.SmtpServer property, or if SmtpMail.SmtpServer points to "localhost" (or the equivalent), be sure the SMTP Service is running on port 25.
  • For testing purposes change the MailMessage.From and MailMessage.To properties to an address that exists on SmtpMail.SmtpServer. Some times this exception is thrown, when it really is a relay issue.
but none of these seems to be my problem...

Any ideas?

This is my post on General Help and Support

I feel nothing.It feels great.

Link to comment
Share on other sites

  • Developers

I don't have a hotmail account to test with, but your info looks correct according these specs assuming you are using the correct/valid email address for from and userid files.

Did you try the exact same code on another computer and that worked?

Was this computer in the same network?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I don't have a hotmail account to test with, but your info looks correct according these specs assuming you are using the correct/valid email address for from and userid files.

Did you try the exact same code on another computer and that worked?

Was this computer in the same network?

Jos

Yes but it was not in the same network... It has to be my network's problem but i can't seem to find it.

I will try it via TCP to see what it gives.

Cheers

Edit: TCP doesn't seems to work neither...

Edit: Tried also your function with gmail account, error was the same.

Edited by ileandros

I feel nothing.It feels great.

Link to comment
Share on other sites

  • Developers

I guess using a normal mail client like outlook express also doesn't work, have you tried that?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I guess using a normal mail client like outlook express also doesn't work, have you tried that?

No i haven't because i have never used a a mail client.

I tried the above script in 7 different networks and it is working pretty fine. Seems the problem is in my network but i can't find out what it excactly is.

Ports must be blocked for some reasons. And i have no idea how to unblock them. Could be ISP

P.s emails i received from the script were sent to junks... Why is that?

Microsoft says they are not safe.

I feel nothing.It feels great.

Link to comment
Share on other sites

  • Developers

No i haven't because i have never used a a mail client.

I tried the above script in 7 different networks and it is working pretty fine. Seems the problem is in my network but i can't find out what it excactly is.

Ports must be blocked for some reasons. And i have no idea how to unblock them. Could be ISP

Well, maybe you should try it so you can determine the reason for you problems. ;)

P.s emails i received from the script were sent to junks... Why is that?

Microsoft says they are not safe.

Maybe you need to ask the programmer of the CDO program we are using which is ..... ahhh Microsoft :lol: Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 4 weeks later...

getting the same error here but at line 92...
### COM Error ! Number: 80020009 ScriptLine: 92 Description: The transport failed to connect to the server.

EDIT: I hid the password...
 

;
;##################################
; Include
;##################################
#Include<file.au3>;
##################################
; Variables
;##################################
$SmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
$FromName = "Me" ; name from who the email was sent
$FromAddress = "contactmailfimo@gmail.com" ; address from where the mail should come
$ToAddress = "contactmailfimo@gmail.com" ; destination address of the email - REQUIRED
$Subject = "Mail Test" ; subject from the email - can be anything you want it to be
$Body = "" ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$CcAddress = "CCadress1@test.com" ; address for cc - leave blank if not needed
$BccAddress = "BCCadress1@test.com" ; address for bcc - leave blank if not needed
$Importance = "Normal" ; Send message priority: "High", "Normal", "Low"
$Username = "contactmailfimo@gmail.com" ; 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 = 465 ; port used for sending the mail
$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

;##################################
; Script
;##################################
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 : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
If FileExists($S_Files2Attach[$x]) Then
ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
$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
Edited by Jos
Link to comment
Share on other sites

Which version of Windows do you run?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Developers

Yes, this solved the problem... I tought I wasnt using secure socket... Thank you!

It is Google that dictates the use of SSL for SMTP mail.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 3 weeks later...

Hi. Im having some issues with the COM Handler. Im trying to create a script that checks for errors aon a server and then send an email using smtp if there is a problem.
Evereything wporks as slong as you use the right settings, but then there is a error (if you enter tyhe wrong smtp adress or wrong port number) the script crashes with the text:
$objEmail.Send
$objEmail.Send^ ERROR

This is the script that im using. Hope that someone can help me :)
 

_sendmail("Error error!")

func _sendmail($message)
$mail_smtp = IniRead($path_config,"Mail settings","SMTP","error")
$mail_fromname = IniRead($path_config,"Mail settings","FROM_NAME","error")
$mail_fromadress = IniRead($path_config,"Mail settings","FROM_ADRESS","error")
$mail_to = IniRead($path_config,"Mail settings","TO","error")
$mail_subject = IniRead($path_config,"Mail settings","SUBJECT","error")
$mail_username = IniRead($path_config,"Mail settings","USERNAME","error")
$mail_password = IniRead($path_config,"Mail settings","PASSWORD","error")
$mail_port = Int(IniRead($path_config,"Mail settings","PORT",25))
$mail_ssl = Int(IniRead($path_config,"Mail settings","SSL",0))
if $mail_smtp = "error" then exit MsgBox(0,"ERROR","Unable to read SMTP from " & $path_config)
if $mail_fromname = "error" then exit MsgBox(0,"ERROR","Unable to read FROM_NAME from " & $path_config)
if $mail_fromadress = "error" then exit MsgBox(0,"ERROR","Unable to read FROM_ADRESS from " & $path_config)
if $mail_to = "error" then exit MsgBox(0,"ERROR","Unable to read TO from " & $path_config)
if $mail_subject = "error" then exit MsgBox(0,"ERROR","Unable to read SUBJECT from " & $path_config)
if $mail_username = "error" then exit MsgBox(0,"ERROR","Unable to read USERNAME from " & $path_config)
if $mail_password = "error" then exit MsgBox(0,"ERROR","Unable to read PASSWORD from " & $path_config)
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($mail_smtp, $mail_fromname, $mail_fromadress, $mail_to , $mail_subject, $message ,"" ,"" ,"" ,"normal", $mail_username , $mail_password, $mail_port, $mail_ssl)
If @error then
msgbox(0,"Error sending message","Error code:" & @error & " Description:" & $rc)
EndIf
EndFunc

;~ Func for sending email using SMTP
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 : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
If FileExists($S_Files2Attach[$x]) Then
ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
$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 for MAIL
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
Edited by Jos
Link to comment
Share on other sites

Tjalve,

compile your script with the latest Beta of AutoIt. COM error handling has been enhanced so a script will never crash. In case of a COM error @error is set and the script continues.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

When the COM error handler is enabled then your script shouldn't crash any longer (even with 3.3.8.1)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...