# Connection refused when sending email

**URL:** <https://forum.inductiveautomation.com/t/connection-refused-when-sending-email/102339>\
**Category:** Ignition\
**Created:** [February 13, 2025, 9:26pm UTC](https://forum.inductiveautomation.com/t/connection-refused-when-sending-email/102339 "2025-02-13T21:26:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_Gerardo\_Rivera](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@Juan\_Gerardo\_Rivera](https://forum.inductiveautomation.com/u/Juan_Gerardo_Rivera)\
**Post date:** [February 13, 2025, 9:26pm UTC](https://forum.inductiveautomation.com/t/connection-refused-when-sending-email/102339/1 "2025-02-13T21:26:09Z")

</div>

Hi everyone,

I have this error code which indicates "(Error when sending email: Error al enviar el correo: (10061, 'Connection refused')

I added the following function and script into the event 'mouseClicked' to a button component ('Prueba')

```python
import smtplib

def send_email():
    from_email = "juan_gerardo_rivera@whirlpool.com"
    to_emails = ["juan_gerardo_rivera@whirlpool.com", "sergio_cruz@whirlpool.com"]
    subject = "Prueba"
    body = "Hola, este correo se envio desde Ignition"

    msg = "Subject: %s\n\n%s" % (subject, body)

    try:
        print "Conectando al servidor SMTP..."
        server = smtplib.SMTP('mailhost.whirlpool.com', 587)
        server.starttls()
        print "Iniciando sesión en el servidor SMTP... " #Logging into server
        server.login(from_email, NaMia/Mantenimiento1506")
        print "Enviando correo..."
        server.sendmail(from_email, to_emails, msg)
        server.quit()
        print "Correo enviado exitosamente" #Email sent successfully
    except Exception, e:
        print "Error al enviar el correo:", str(e) #Error when sending email
        
send_email()

```

Btw, I just modified the password for security reasons.  
This is the configuration set in the gateway.

I don't know if something is wrong with my script or the email settings

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/e/1e83ab97c86da5ae2c09a85246921dba101c1af0.png)

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [February 13, 2025, 9:57pm UTC](https://forum.inductiveautomation.com/t/connection-refused-when-sending-email/102339/2 "2025-02-13T21:57:57Z")

</div>

If you're trying to use a SMTP profile to send email, you need to use one of the built in scripting functions like [system.net.sendEmail()](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-net/system-net-sendEmail) One advantage of using the built in function (other than simplicity) is you get a lot better logging available to you.

---

<div class="post-metadata">

**Author:** ![Juan\_Gerardo\_Rivera](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@Juan\_Gerardo\_Rivera](https://forum.inductiveautomation.com/u/Juan_Gerardo_Rivera)\
**Post date:** [February 14, 2025, 5:26pm UTC](https://forum.inductiveautomation.com/t/connection-refused-when-sending-email/102339/3 "2025-02-14T17:26:56Z")

</div>

Thank you for the advice, Kathy,

I have changed the script so it is more based using the system.net.sendEmail()

```python
smtpProfile = "Whirlpool Mailhost" # Usa el nombre del perfil creado en el Gateway
fromAddr = "juan_gerardo_rivera@whirlpool.com"
to = ["juan_gerardo_rivera@whirlpool.com", "sergio_cruz@whirlpool.com"]
subject = "Prueba desde Ignition"
body = "Hola, este correo se envió desde Ignition usando un perfil SMTP configurado en el Gateway."

try:
    system.net.sendEmail(smtpProfile=smtpProfile, fromAddr=fromAddr, subject=subject, body=body, to=to)
    print "Correo enviado exitosamente"
except Exception, e:
    print "Error al enviar el correo:", str(e)

```

Now the error shown by Ignition is the following one: Traceback (most recent call last):

File "event:mouseClicked", line 8, in

File "event:mouseClicked", line 8, in

I have these setting in the gateway's email configuration and I attached a screenshot of my script within the mouseClicked()

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/f/ff99dfd68ab9d3f213001e2cb917ff42391e7079.png)

I don't know if it is something related to my script or a restriction from IT

Best regards.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 14, 2025, 6:31pm UTC](https://forum.inductiveautomation.com/t/connection-refused-when-sending-email/102339/4 "2025-02-14T18:31:43Z")

</div>

Did you test your email profile configuration?

 ![Ignition email test](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/d/fde10228b830a3515f680494e66d46ef858b314a.png)

---

<div class="post-metadata">

**Author:** ![Juan\_Gerardo\_Rivera](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@Juan\_Gerardo\_Rivera](https://forum.inductiveautomation.com/u/Juan_Gerardo_Rivera)\
**Post date:** [February 14, 2025, 6:46pm UTC](https://forum.inductiveautomation.com/t/connection-refused-when-sending-email/102339/5 "2025-02-14T18:46:53Z")

</div>

Hey, I realized the port number was different, I checked with our IT area which port was available and the told me to use port "25" because it is what Ignition is connected directly and it is working now!

Thank you for help and regards!

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 14, 2025, 7:24pm UTC](https://forum.inductiveautomation.com/t/connection-refused-when-sending-email/102339/6 "2025-02-14T19:24:13Z")

</div>

Set your "from" email address to something that will help trace it to its source if it starts spamming or creating problems. Something like `scriptName.projectName@gatewayName.yourCompany.com` will be a huge help to IT, etc.

I'd also remove the hard-coded "Whirlpool Mailhost" from your script. If you ever change your email provider you would have to rename everything. Instead give it a generic name, "SMTP for Ignition" or similar. Then you can edit the SMTP profile to point to your new service provider but you won't have to find and edit your scripts.
