By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,677 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,677 IT Pros & Developers. It's quick & easy.

Generate and Attach a file to email.

P: 40
My requirement is to generate and attach a file and send in an email automatically.

i have a existing functionality where i using the code below to write a CSV file in my local system.

[HTML]StreamWriter sw = File.CreateText(fileName)[/HTML]
[HTML]sw.Write(m_Model.Csv);[/HTML]

the new functionality is to attach the file to email and send automatically. i cannot store in some path and attach since access issues might come later.

Is there any other way to generate the CSV file and attach to email.
Aug 12 '08 #1
Share this Question
Share on Google+
14 Replies


DrBunchman
Expert 100+
P: 979
If you want to add an attachment then you need to create the file first - if you don't then what would you be attaching? Why don't you want to create the file on the server first?

Dr B
Aug 12 '08 #2

P: 40
I was just thinking if the user will not have write access to save the file in the c:\or d:\....

May be i should request for write access to specific folder..
Aug 12 '08 #3

DrBunchman
Expert 100+
P: 979
Are we talking about a website or a windows app here?

Dr B
Aug 12 '08 #4

P: 40
Windows Application..
Aug 12 '08 #5

DrBunchman
Expert 100+
P: 979
As long as your application has permission to access the required folder then you shouldn't have any problems. It depends on how your app is deployed I suppose - if it's running on a server then you'll need to make sure that the server log on (probably the console) has read/write permissions. If it's running on local users machines then they will already have permission to access their own local file system.

Dr B
Aug 12 '08 #6

P: 40
Thanks for the replies...will try it out...
Aug 12 '08 #7

DrBunchman
Expert 100+
P: 979
Okay, let me know if you have any problems.

Dr B
Aug 12 '08 #8

joedeene
100+
P: 583
you could always use the user's local temp directory if its just an attachment you dont need after its sent? you could dispose of it with code immediately after or the temp directory will handle the files sooner or later
Aug 12 '08 #9

P: 40
Yeah..thats a good option..

Path.GetTempFileName()

this should give me the temp directory path rite??or should i use

Environment.GetEnvironmentVariable("TEMP")

Which is better?
Aug 12 '08 #10

joedeene
100+
P: 583
Yeah..thats a good option..

Path.GetTempFileName()

this should give me the temp directory path rite??or should i use

Environment.GetEnvironmentVariable("TEMP")

Which is better?

i'd use the gettemppath method under the path class in the system.io namespace


Expand|Select|Wrap|Line Numbers
  1. String tempFolderPath = Path.GetTempPath();
like put that code before the writing process, then make the streamreader write, i did a simple messagebox.show with it to see if it worked, and it gave me the correct temp directory for my username...
Aug 12 '08 #11

cjjer
P: 4
if in web app,you can use cache depend file.:P
Aug 12 '08 #12

P: 40
Ok thanks for the suggestions!!
Aug 12 '08 #13

joedeene
100+
P: 583
Ok thanks for the suggestions!!
well if you didnt care what the filename was then something like this should work...the gettemppath, only returns the directory, you'd still have to create a file with the text.

heres an example:

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             string tempfolderpath = System.IO.Path.GetTempPath();
  4.             string mytempfile = tempfolderpath + "\\myfilename.csv";
  5.             System.IO.StreamWriter mystreamwriter = new System.IO.StreamWriter(mytempfile);
  6.  
  7.             mystreamwriter.Write("string/value that was generated");
  8.             mystreamwriter.Close();
  9.             //then attach it...;
  10.  
  11.         }
Aug 12 '08 #14

Curtis Rutland
Expert 2.5K+
P: 3,256
I prefer the Environment.GetEnvironmentVariable("TEMP") method myself. You are actually asking windows where the temp directory is.
Aug 12 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.