Generate and Attach a file to email.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreemathy2000
    New Member
    • Oct 2007
    • 40

    Generate and Attach a file to email.

    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_Mode l.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.
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    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

    Comment

    • sreemathy2000
      New Member
      • Oct 2007
      • 40

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

      Comment

      • DrBunchman
        Recognized Expert Contributor
        • Jan 2008
        • 979

        #4
        Are we talking about a website or a windows app here?

        Dr B

        Comment

        • sreemathy2000
          New Member
          • Oct 2007
          • 40

          #5
          Windows Application..

          Comment

          • DrBunchman
            Recognized Expert Contributor
            • Jan 2008
            • 979

            #6
            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

            Comment

            • sreemathy2000
              New Member
              • Oct 2007
              • 40

              #7
              Thanks for the replies...will try it out...

              Comment

              • DrBunchman
                Recognized Expert Contributor
                • Jan 2008
                • 979

                #8
                Okay, let me know if you have any problems.

                Dr B

                Comment

                • joedeene
                  Contributor
                  • Jul 2008
                  • 579

                  #9
                  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

                  Comment

                  • sreemathy2000
                    New Member
                    • Oct 2007
                    • 40

                    #10
                    Yeah..thats a good option..

                    Path.GetTempFil eName()

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

                    Environment.Get EnvironmentVari able("TEMP")

                    Which is better?

                    Comment

                    • joedeene
                      Contributor
                      • Jul 2008
                      • 579

                      #11
                      Originally posted by sreemathy2000
                      Yeah..thats a good option..

                      Path.GetTempFil eName()

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

                      Environment.Get EnvironmentVari able("TEMP")

                      Which is better?

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


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

                      Comment

                      • cjjer
                        New Member
                        • Aug 2008
                        • 4

                        #12
                        if in web app,you can use cache depend file.:P

                        Comment

                        • sreemathy2000
                          New Member
                          • Oct 2007
                          • 40

                          #13
                          Ok thanks for the suggestions!!

                          Comment

                          • joedeene
                            Contributor
                            • Jul 2008
                            • 579

                            #14
                            Originally posted by sreemathy2000
                            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:

                            Code:
                            private void button1_Click(object sender, EventArgs e)
                                    {
                                        string tempfolderpath = System.IO.Path.GetTempPath();
                                        string mytempfile = tempfolderpath + "\\myfilename.csv";
                                        System.IO.StreamWriter mystreamwriter = new System.IO.StreamWriter(mytempfile);
                            
                                        mystreamwriter.Write("string/value that was generated");
                                        mystreamwriter.Close();
                                        //then attach it...;
                            
                                    }

                            Comment

                            • Curtis Rutland
                              Recognized Expert Specialist
                              • Apr 2008
                              • 3264

                              #15
                              I prefer the Environment.Get EnvironmentVari able("TEMP") method myself. You are actually asking windows where the temp directory is.

                              Comment

                              Working...