Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

map path to file in website

I am testing a simple method to send an attachment in an email.  I created a website on my localhost and have included a sample txt file in the root directory of the site.

In my page, I'm calling the following method shown in the code section below.

When I run my page, I get the following error:

Could not find file 'C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\TestAttachement.txt'.

The file is not located in the above directory.

I thought that if the file is found in the website, I didn't need to use an explicit file path.  I guess I do.

How can I map the path to my file?

I was reading some articles that mentioned using Server.MapPath.  However, when I tried this, the intellisense was not finding the Server object and MapPath method.

I also tried HttpServerUtility namespace, but the MapPath member was not available.

I'm using VS2008.

Thanks for any help.


static void AttachmentFromFile()
        {
            //create the mail message
            MailMessage mail = new MailMessage();
 
            //set the addresses
            mail.From = new MailAddress("no-reply@myisphost.net");
            mail.To.Add("xxxxxxxxxxxxxx@myisphost.net");
 
            //set the content
            mail.Subject = "This is an email";
            mail.Body = "this content is in the body";
 
            //add an attachment from the filesystem
            mail.Attachments.Add(new Attachment("TestAttachement.txt"));
 
            //send the message
            SmtpClient smtp = new SmtpClient("xxx.xxx.xxxxxxx.xxxx");
            smtp.Send(mail);
 
        }
 
        protected void btnSend_Click(object sender, EventArgs e)
        {
            AttachmentFromFile();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of -Dman100-

ASKER

Hi Ghost,

Thanks for replying to my post.  Those were the exact articles I was reading.

I figured out what the problem was.  My AttachmentFromFile() method was static.

Why can't you use Server.MapPath in a static method?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial