Example Programs

cancel
Showing results for 
Search instead for 
Did you mean: 

Sending SMTP Email Attachments from LabVIEW

Overview

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

 

Description

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. 

 

 

 

Requirements

  • 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.**

 

 

Comments
Member holyna
Member

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 74.125.91.109:587". 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

Member KKH
Member

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 labmaster
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.

 

labmaster. 

 

Member poderosoalonso
Member

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

Member AlexElb
Member

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:

<HTML> 
<BODY>
<IMG src="Filename with spaces.jpg">
</BODY>
</HTML>

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...