ASP.NET - how to get file from folder..?

Asked By sunil pandey
09-Feb-12 02:31 AM
Hi all

i uploaded file into a folder.
and for mail attachment.
i want that file
button()
{
 string fname2 = FileUpload1.PostedFile.FileName;
   string SaveLocation = Server.MapPath("~/Quotation")+"\\"+fname2;
    
   EnhancedMailMessage msg = new EnhancedMailMessage();
 Attachment attach1 = new Attachment(SaveLocation);
 
  msg.Attachments.Add(attach1);
}
button()
{
 if (FileUpload1.HasFile)
      {
        string fname = FileUpload1.FileName.ToString();
    
        String fileName = fname;
        String SaveLocation = Server.MapPath("~/Quotation")+"\\"+fileName;

        FileUpload1.PostedFile.SaveAs(SaveLocation);
      }
}
    
  dipa ahuja replied to sunil pandey
09-Feb-12 02:44 AM
using System.Net.Mail;
using System.Net;
 
protected void btnSent_Click(object sender, EventArgs e)
{
  string toEmailAddress="xx@gmail.com";
  string GmailId="abc@gmail.com";
  string password="";
  string bodyMsg="helo its testing mail";
 
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = txtSubject.Text;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
 
  //Attach file using FileUpload Control and put the file in memory stream
 
  if (FileUpload1.HasFile)
  {
    mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
  }
 
  SmtpClient smtp = new SmtpClient("smtp.gmail.com");
  smtp.EnableSsl = true;
  smtp.UseDefaultCredentials = false;
  smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);
  smtp.Send(mail);
}
 
  smr replied to sunil pandey
09-Feb-12 02:54 AM
hi

try this

public static void ProcessDir(string sourceDir, int recursionLvl)
{
  if (recursionLvl<=HowDeepToScan)
  {
  // Process the list of files found in the directory.
  string [] fileEntries = Directory.GetFiles(sourceDir);
  foreach(string fileName in fileEntries)
  {
     // do something with fileName
     Console.WriteLine(fileName);
  }
 
 
  // Recurse into subdirectories of this directory.
  string [] subdirEntries = Directory.GetDirectories(sourceDir);
  foreach(string subdir in subdirEntries)
     // Do not iterate through reparse points
     if ((File.GetAttributes(subdir) &
      FileAttributes.ReparsePoint) !=
        FileAttributes.ReparsePoint)
 
      ProcessDir(subdir,recursionLvl+1);
  }
}


follow
http://weblogs.asp.net/israelio/archive/2004/06/23/162913.aspx
http://bytes.com/topic/asp-net/answers/328297-getting-list-files-folder 
  smr replied to sunil pandey
09-Feb-12 02:55 AM
you can try this

public void Page_Load()
{
  System.IO.Directory StoreFile = null;
  string[] Files = null;
  string File = null;
  Files = StoreFile.GetFiles("drive:\\directory\\path\\", "*");
  foreach (string File_loopVariable in Files) {
    File = File_loopVariable;
    Response.Write(File + "<br>");
  }
}
  Web Star replied to sunil pandey
09-Feb-12 03:20 AM
if you want to upload one file and than attached that file into email attachement than simply first of all put code for saveas file from file control and than attched that file with mail object's attachment only need to set proper path of the file other thing done by that object itself
 if (FileUpload1.HasFile)
    {
      string fname = FileUpload1.FileName.ToString();
    
      String fileName = fname;
      String SaveLocation = Server.MapPath("~/Quotation")+"\\"+fileName;

      FileUpload1.PostedFile.SaveAs(SaveLocation);
    } 

//after than you need to send email and specify the correct path of that file
Attachment attachFile = new Attachment(SaveLocation );
MyMessage.Attachments.Add(attachFile);
  Somesh Yadav replied to sunil pandey
09-Feb-12 03:36 AM

Hi,

Here is another way to get filenames from all files, including subdirectories (notice the SearchOption.AllDirectories parametre):

// Process the list of files found in all directory.

string [] fileEntries = Directory.GetFiles(sourceDir, "*", SearchOption.AllDirectories);

foreach(string fileName in fileEntries)

{

  // do something with fileName

  Console.WriteLine(fileName);

}

If you change the "*" to ex. "*.log" you will get all filenames that ends with ".log".

Create New Account
help
with the Email Address to whom you want to send the mail System . Net . Mail . MailMessage mail = new System . Net . Mail . MailMessage (); mail . To . Add ( to ); mail . From = new MailAddress ( from , "One Ghost" , System . Text . Encoding . UTF8 ); mail . Subject = "This is a test mail" ; mail Body Text" ; mail . BodyEncoding = System . Text . Encoding . UTF8 ; mail . IsBodyHtml = true ; mail . Priority = MailPriority . High ; SmtpClient client = new SmtpClient (); / / Add the Creddentials- use your own email id and password client . Credentials = new System . Net . NetworkCredential ( from , "Password" ); client . Port = 587 ; / / Gmail works on this port client . Host = "smtp.gmail.com HttpContext . Current . Response . Write ( errorMessage ); } / / end try } HI For sending mail you have to use MailMessage Class. follow this code protected void btnSend_Click(object sender, EventArgs e) { try { MailMessage Msg = new MailMessage (); / / Sender e-mail address. Msg.From = new MailAddress(txtUsername.Text); / / Recipient e
string GmailId = "abc@gmail.com" ; string bodyMsg = "helo its testing mail" ; string subject = "testing mail" ; MailMessage mail = new MailMessage (); mail.To.Add(toEmailAddress); mail.From = new MailAddress (GmailId); mail.Subject = subject; mail.Body = bodyMsg; mail.IsBodyHtml = true ; SmtpClient smtp = new SmtpClient (); smtp.EnableSsl = true ; smtp.Send(mail); } hi, First of all add below mentioned namespace in write this code in click event of button protected void Button1_Click( object sender, EventArgs e) { MailMessage mail = new MailMessage(); mail.To.Add( "jainamit.agra@gmail.com" ); mail.To.Add( "amit_jain_online@yahoo.com" ); mail.From = new MailAddress( "jainamit.agra@gmail.com" ); mail.Subject = "Email using Gmail" ; string Body = "Hi, this mail is
Button1.Click + = new EventHandler(Button1_Click); } protected void Button1_Click(object sender, EventArgs e) { System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); mailMessage.To.Add(new MailAddress("toSomeone@gmail.com")); mailMessage.From = new MailAddress("fromMe@hotmail.com"); mailMessage.Subject = "my test subject"; mailMessage.Body = "my test body"; mailMessage.IsBodyHtml = true; SmtpClient smtpClient = new
SendMail ( string smtpServer , string mailFrom , string mailTo , string [] copyTo , string subject , string mailBody , Attachment [] attachments ) { SmtpClient smtpClient = new SmtpClient ( smtpServer ); MailMessage mail = new MailMessage ( mailFrom , mailTo , subject , mailBody ); if ( copyTo ! = null ) { foreach ( string mailAddress in copyTo ) { if (! String . IsNullOrEmpty ( mailAddress )) mail . CC . Add ( mailAddress ); } } if ( attachments ! = null ) { foreach ( Attachment attachment in attachments ) { if ( attachment ! = null ) mail . Attachments . Add ( attachment