Error sending email

Hi, I’m trying to send an email with a button with this script and I get an error, I’m testing to send just text, but when this works I need to send an Image attached as well.



system.net.sendEmail("smtp.gmail.com:587:tls","myEmail@gmail.com","test email","this is a test",0,["toMail@hotmail.com"], username="MyUser", password="MyPassword")

I get this Error:

Traceback (most recent call last):
  File "<event:actionPerformed>", line 1, in <module>
	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.newGatewayException(GatewayInterface.java:328)

	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.sendMessage(GatewayInterface.java:302)

	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.sendEmail(GatewayInterface.java:624)

	at com.inductiveautomation.factorypmi.application.script.builtin.ClientNetUtilities._sendEmail(ClientNetUtilities.java:105)

	at com.inductiveautomation.ignition.common.script.builtin.AbstractNetUtilities.sendEmail(AbstractNetUtilities.java:115)

	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.lang.reflect.Method.invoke(Unknown Source)


com.inductiveautomation.ignition.client.gateway_interface.GatewayException: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Error sending email.

	at org.python.core.Py.JavaError(Py.java:495)
	at com.inductiveautomation.factorypmi.application.script.builtin.ClientNetUtilities._sendEmail(ClientNetUtilities.java:108)
	at com.inductiveautomation.ignition.common.script.builtin.AbstractNetUtilities.sendEmail(AbstractNetUtilities.java:115)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:186)
	at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:428)
	at org.python.core.PyObject.__call__(PyObject.java:320)
	at org.python.pycode._pyx80.f$0(<event:actionPerformed>:1)
	at org.python.pycode._pyx80.call_function(<event:actionPerformed>)
	at org.python.core.PyTableCode.call(PyTableCode.java:165)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1275)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:624)
	at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:168)
	at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:265)
	at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:55)
	at com.sun.proxy.$Proxy22.actionPerformed(Unknown Source)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Error sending email.
	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.newGatewayException(GatewayInterface.java:328)
	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.sendMessage(GatewayInterface.java:302)
	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.sendEmail(GatewayInterface.java:624)
	at com.inductiveautomation.factorypmi.application.script.builtin.ClientNetUtilities._sendEmail(ClientNetUtilities.java:105)
	... 54 more
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvwj
534-5.7.14 u_qLk1xcmJ2rp5LvLJD8Ddm_CzClDbNHuwW1e3hCGT37YRj_ula4Y2I4rzsKzwb_Xl6-0T
534-5.7.14 3A3LovVi2xbuQQgk0n8ZNXhOyMMw5FDJEsQKR8xZf8ePFTgaUQ5TVe2IQ74_XkfYM3XWbp
534-5.7.14 97l-LCpKpX9TGnlV6ZftO1BHk0_8QL9bxYUQHCu8iRsa_pUHmwiQZaQDBUKjc2znzXWjAp
534-5.7.14 I9vEy4SC884r7k5rL1O7KD06EzUY> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 k71sm4518907qhc.42 - gsmtp

	at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:823)
	at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:673)
	at javax.mail.Service.connect(Service.java:317)
	at javax.mail.Service.connect(Service.java:176)
	at javax.mail.Service.connect(Service.java:125)
	at javax.mail.Transport.send0(Transport.java:194)
	at javax.mail.Transport.send(Transport.java:124)
	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:155)
	at com.inductiveautomation.ignition.gateway.servlets.Gateway._sendEmail(Gateway.java:994)
	at com.inductiveautomation.ignition.gateway.servlets.Gateway.doPost(Gateway.java:393)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at com.inductiveautomation.ignition.gateway.bootstrap.MapServlet.service(MapServlet.java:85)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:769)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1125)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1059)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
	at org.eclipse.jetty.server.Server.handle(Server.java:497)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:248)
	at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539)
	at java.lang.Thread.run(null)

Ignition v7.7.4 (b2015033012)
Java: Oracle Corporation 1.8.0_40

Authentication error.

Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvwj 534-5.7.14 u_qLk1xcmJ2rp5LvLJD8Ddm_CzClDbNHuwW1e3hCGT37YRj_ula4Y2I4rzsKzwb_Xl6-0T 534-5.7.14 3A3LovVi2xbuQQgk0n8ZNXhOyMMw5FDJEsQKR8xZf8ePFTgaUQ5TVe2IQ74_XkfYM3XWbp 534-5.7.14 97l-LCpKpX9TGnlV6ZftO1BHk0_8QL9bxYUQHCu8iRsa_pUHmwiQZaQDBUKjc2znzXWjAp 534-5.7.14 I9vEy4SC884r7k5rL1O7KD06EzUY> Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 k71sm4518907qhc.42 - gsmtp
From https://support.google.com/mail/answer/78754:


My guess is that you have to the Account settings and allow “less secure apps”. This let’s you use apps other then the “Google approved” ones.

I had to “activate the access of apps less secure” on my gmail account and that resolve the problem, now I getting emails, but I read gmail is not the best smtp to use, maybe I need to use other or I’m going get my account blocked.

Now what I need is to attach and image, I have this script to print a container to an image:

Date = str(system.tag.getTagValue("Produccion/Tiempo Turno/Fecha solo"))
Date = Date.replace(":","-")
system.print.printToImage(event.source.parent.getComponent('Container'), Date + '.jpg')

Is there a way to attach this image recently created?

Take at look at this thread. That should help point you in the right direction! :slight_smile:

Thanks Jordan, Now I can email Excel files and image files, this is the script to email images, but I have a little problem explained below.



system.print.printToImage(event.source.parent.getComponent('Container'), "image.jpg")
filename="image.jpg"
filedata = fpmi.file.readFileAsBytes(filename)
system.net.sendEmail("smtp.gmail.com:587:tls","myEmail@gmail.com","Subject","Content",0,["ToEmail"], [filename],[filedata], username="myUser", password="myPassword")

I have a problem using this function:

system.print.printToImage(component [, filename])

This function doesn’t have a path file, so I get the image in my desk, is there a way to change the default path? or simple delete the image?

The path is part of the filename. Assuming Windows:

filename="c:\\path\\to\\the\\directory\\image.jpg"

system.print.printToImage(event.source.parent.getComponent('Container'), filename)
filedata = fpmi.file.readFileAsBytes(filename)
system.net.sendEmail("smtp.gmail.com:587:tls","myEmail@gmail.com","Subject","Content",0,["ToEmail"], [filename],[filedata], username="myUser", password="myPassword")

Note the double backslash. In python, a backslash is an “escape character”. It lets you put in special characters in a string (’\n’ is a newline, for example). So if you need an actual backslash in your string, you need to use two of them.

Hope this helps!

Hi again, that example worked but now I having another problem, what I want to do now is to execute this script when a tag turn 1, so I bind this tag to a numeric label and I wrote this script in the propertyChange, all is working great but when I log in with a second client in a diferent computer and this tag turn 1, the system.print.printToImage function is replicated saving the images in the two computers.

My question is, Can this function system.print.printToImage don’t save any image to my hard drive? then this way I can receive the image only in my email, I don’t want the image in my computer’s hard drive.

I think system.print.createImage can do what I need but I don’t know how to use it.

Since the image needs to be an attachment, it needs to be a file. But nothing says you can’t delete the file after a successful email. Adding to the sample code from above:

import os

filename="c:\\path\\to\\the\\directory\\image.jpg"

system.print.printToImage(event.source.parent.getComponent('Container'), filename)
filedata = fpmi.file.readFileAsBytes(filename)
system.net.sendEmail("smtp.gmail.com:587:tls","myEmail@gmail.com","Subject","Content",0,["ToEmail"], [filename],[filedata], username="myUser", password="myPassword")

os.remove(filename)