Example Code

Sending SMTP Email Attachments from LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.


  • LabVIEW

Code and Documents



This example shows how to send email attachments from LabVIEW using any SMTP email service, such as Gmail.



This example is based off of a previous example, which enables you to send an email using Simple Mail Transfer Protocol (SMTP) to sync up with Gmail in LabVIEW. The code has been simplified and cleaned up in this example. 





  • LabVIEW 2012 (or compatible)
  • An e-mail account using a SMTP outgoing mail server


Steps to Implement or Execute Code

  1. Open the attached VI
  2. On the front panel configure the following inputs:
    • Email Account ID and password
    • SMTP Outgoing Mail Server (if using Gmail, you can set it to smtp.gmail.com)
    • The email address for From:
    • The email address for To:
    • Subject:, Body:, and Attachment Path
  3. Run the VI to send an email with all of the configured inputs

Note: If you receive Error -1172 with this example, you may have to change your e-mail account protection settings.


Additional Information or References

VI Block DiagramSend Email Attachment LV 2012 NI Verified.png


**This document has been updated to meet the current required format for the NI Code Exchange.**



Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.


Hi Penata, thanks for your vi. I tried it but got error 1172 saying "No connection could be made because the target machine actively refused it". I'm using gmail to send emails and pretty sure that all the settings in the VI are correct. According to http://mail.google.com/support/bin/answer.py?answer=13287, I tried both 587 and 465 as the port, and nothing would help. 587 will give the 1172 error and 465 will hang the vi for a while after stopping it. This 1172 error is unlikely due to the suggestion from http://decibel.ni.com/content/docs/DOC-2401, "Note: If you receive Error -1172 with this example, try logging into the Gmail account with your browser. This error can occur with Gmail accounts that have been inactive for a period of time. You must log in with a browser and verify CAPTCHA text to reactivate the account." because the error kept on even when I login to my gmail account.

I got myself a solution from http://stackoverflow.com/questions/2737823/unable-to-send-smtp-mail-with-net-network-issue, which says to turn off the antivirus software. I turned off McAfee on my PC and it did work. Now, I'm able to send email, even when I log out my account. However, I'm not happy since I have to keep my antivirus off. My question is, is there any way to walk around this issue? i.e. being able to send email with antivirus on?

Many thanks


I'm having a similar problem -- this VI worked for me a few months ago (port 587) but now it's not (on either 587 or 465).    Tried telnet as per this help screen from Google http://mail.google.com/support/bin/answer.py?hl=en&answer=78775   I can telnet to 465, but not 587.    Using port 465, I get a -1172 error -- ".... the operation has timed out."     I don't have the option of turning off the antivirus program; the corporate IT people quite appropritately don't allow that....    Ideas, anyone?

Active Participant
Active Participant

Thanks for the code.


I would like to add one point the code.

In AttachmentCollection, please do not forget to add "Dispose()" after emailing.

If not, the file handler of the attachment is not released from time to time (not reliable).

I think this is a bug because all resources should be released by closing the handler.





Hi labmaster

can you help me with this issue i have with my code?

but i am not able to figure it out where to put the dispose attachment. 

i want to overwite a file and send it later, but is always used in the vi after i send it by email i cannot get to dispose it.

can you help me? i would really appreciate it


Hello Folks,


I am using nearly the same to send an HTML Mail with images included. But I am facing problems, when using Images with spaces in the filenames.MailAtSpace.png


The HTML generated looks like this:

<IMG src="Filename with spaces.jpg">

The file is added to the mail as an attachment, but is not shown in the mailbody. I did try to replace the spaces with %20 in the HTML string, then in Outlook the file is no longer shown as an attachment, but nevertheless there is no picture showing. Any Ideas? May be an example of adding the attachment via contentStream?


In the Snippet you see my workaround: I just copy the picture to a new file without spaces in the filename - then everything works as expected...


Proud developer at Hampel Software Engineering where we turn '404 not found' into '200 OK'. Join us on Discord

I tried using the VI as such with necessary details for our local network email, it throwed error -1172. so I changed the constant to false (disable ssl as it does not require user name and pwd) and it worked. 


I think when you connect to server outside intranet you need the SSL, so if you use gmail or yahoo email etc.. you need to enable SSL(it is decided by th server settings).


Hope this helps people who look at this thread in the future!!

தமிழ் நேரம்
முதல் இந்திய ஆய்வுமெகபொப சிற்பி
சோதனைநிறுத்தம் சிற்பி
மற்ற சான்றிதழ்கள்
யாதும் ஊரே! யாவரும் கேளிர்!!