Showing results for 
Search instead for 
Did you mean: 

sending email and deleting files

Go to solution



I need some help. I am creating a file called Appl5, write some thing to it and close it. Then, I am opening the same file, read the content and use that content as an email body. and at the same time use that file as an email attachement. and then send my email and finally delete the file.


the problem is here:


- after reading the file, and when trying to use it as the body of the email that will be sent, the body of my email doesn't have that content, just maybe a letter, or 1 charcter.


- I am not able to delete the file


could someone take a look?


thank you.

0 Kudos
Message 1 of 23


As far as I can see, you are trying to do things in parallel, just make sure you wire error outputs to ensure data flow. There is no guarantee that your file creation section will execute first as it stands. I have wired couple of error clusters, I haven't tested it so there may be other things wrong with it. Also, the diagram is quite large right now, try to break it down to subvi's for better readability.

Hope this is what your problem is.



CLA CTAChampionI'm attending the GLA Summit!
Subscribe to the Test Automation user group: UK Test Automation Group
0 Kudos
Message 2 of 23


  • Do not use hard-coded paths in the code like that. Use a front-panel path control. And even if you are going to use a path constant, use it once and branch a wire. Placing a second path constant with the same value introduces the potential of a bug where you change it one place and forget to change it someplace else.
  • What is the purpose of the case structure around the whole code?
  • Instead of using the Not Equal operator with a string constant, use the "Empty String/Path?" function.
  • The Close File after the Read Text File is not necessary. When a path constant is wired into the Read Text File function, that function will open, read, and close the file. Thus, the Close File is redundant. Note that this applies to the read portion only.
0 Kudos
Message 3 of 23

Thanks for replying, but I still cannot delete the file at the end.


also the body of the email doesn't contain the content of what is read from the file.


more help needed!



0 Kudos
Message 4 of 23

I see you have the simple error handler at the end which is good. An error in to the delete file function will prevent it from deleting the file. Are you presented with any errors after this vi is run?


It looks like you are using gmail. Could it be related to gmail? Any chance you can try it with a different mail server?


You have a comparison of an empty string going to a case selector for an attachment. This iwll always be true because of the constant wired to the path to string. I realize the constant is there for debugging but there is a function "Not a Number/Path/Refnum?" which is better to use. In fact you could use the Check if File or Folder Exists vi to select the case.


LabVIEW 2012

0 Kudos
Message 5 of 23

Yes, I am getting error 5. No this is not related to gmail. If you place the delete block right after the cc, add case, it works fine. So as you said the problem is coming from the case selector for the attachment part. So I use "check if file exist" and I deleted the case selector part, it is still giving me error 5, and cannot delete the file.

0 Kudos
Message 6 of 23

I don't have LabVIEW right now so I have to try to remember your vi. I remember that you write to a file then close it, do some .NET email stuff, then delete the file.


Error 5 will happen if you try to delete a file that is already open. Although you closed it after writing to it, the .NET file attachment methods may be keeping the file open. Are you calling the dispose method after the file attachment method?

LabVIEW 2012

0 Kudos
Message 7 of 23



Create a state-machine or use the error-wire properly to ensure dataflow, as suggested earlier. Close the reference to the file before deleteing it. Like Steve says, the file might be in use by the .Net nodes. 

Certified LabVIEW Associate Developer

Automated Test Developer
Topro AS
0 Kudos
Message 8 of 23

As I said I am going by memory right now and I only took a brief look at the vi. I don't remember any obvious dataflow issues but that could definately cause problems. State machines are good but in this case it might be overkill. (Again, going by possibly faulty memory)

LabVIEW 2012

0 Kudos
Message 9 of 23

You could also try putting a Wait before the Delete. You might not be giving .NET a chance to do its thing.

Ravi A.
National Instruments | Applications Engineer
0 Kudos
Message 10 of 23