8 Replies - 6149 Views - Last Post: 27 January 2015 - 10:50 AM Rate Topic: -----

#1 CloudyHLS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 12

system.net.mail.attachments "Access to path is denied"

Posted 16 August 2012 - 04:07 PM

Im writing a simple program for my company which lets the user fill in several fields and choose a picture. The program then complies the information into a specific text format and sends it to the manufacturer by email. It is essentially an order form. My problem is whenever i try and send my form it says that i don't have access to "blank" path. Im sure i just have to add permissions somewhere but i cant seem to get it. Can anyone help?

namespace OrderRelay
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

           
        }

        private void button1_Click(object sender, EventArgs e)
        {
           

            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            string UserName = "";
            string Password = "";
            string file = pathimage.Text;
            message.To.Add(new System.Net.Mail.MailAddress(""));
            message.From = new System.Net.Mail.MailAddress("");
            message.Subject = "Tester 1";
            message.Body = "Size:";
            message.IsBodyHtml = false;

            

            System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(pathimage.Text);
            inline.ContentDisposition.Inline = true;
            message.Attachments.Add(inline);
            message.Dispose();

            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential(UserName, Password),
                EnableSsl = true
            };
            client.Send("","",message.Subject,message.Body);
            

           

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Validate_Click(object sender, EventArgs e)
        {
            if (morw.Text == "Gender" || sizeshirt.Text == "Size" || ShirtColour.Text == "Shirt" || ImageColour.Text == "Image" || numof.Value == 0)
            {
                MessageBox.Show("Validation Failed: \n\nPlease complete the form before validation.");

            }
            else
            {
                button1.Enabled = true;
                MessageBox.Show("Validation Complete:\n\nPlease review your order before sending.");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            string filename = "";
            string path = "";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                filename = System.IO.Path.GetFileName(ofd.FileName);
                path = System.IO.Path.GetDirectoryName(ofd.FileName);
            }

            fitx.Text = filename;

            pathimage.Text = path;
            fitx.ReadOnly = false;
            pathimage.ReadOnly = false;
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: system.net.mail.attachments "Access to path is denied"

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: system.net.mail.attachments "Access to path is denied"

Posted 16 August 2012 - 04:51 PM

This looks more like it is a school-homework level project, than something production level for work. Just FYI. Tips 2 and 4 below would help your project a lot. It is also lacking a LOT of validation and compensation for real world problems that arise.
What if your ISP is down? How are you holding sent emails until you have internet again?

38 client.Send("","",message.Subject,message.Body);
Get out of the habit of using "". This isn't 1980's BASIC. String.empty is the accepted object for this and will keep you out of trouble across language localisations and .NET firmware versions.


20 message.To.Add(new System.Net.Mail.MailAddress(""));
You have to actually address it to someone. A nullstring address won't fly. You also shouldn't hardcode the address. Make a property, stick it in a Preferences class of some type so you can serialize it and deserialize it. That way your preferences are loaded when you open the app and saved when you close.
Separating data from GUI - PLUS - serializing the data to XML

Quote

i don't have access to "blank" path.

If the path variable is actually blank (string.empty or null or whitespace) then it simply doesn't exist and its just not possible to have access to *nothingness*
You say you get the error when you try to send, but don't tell us on which line the exception happens. Its hard to tell where sending happens with meaningless control names like button_1 and button_2 and no actual "send()" method.



Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

Was This Post Helpful? 0
  • +
  • -

#3 CloudyHLS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 12

Re: system.net.mail.attachments "Access to path is denied"

Posted 16 August 2012 - 09:14 PM

Thank you very much for your reply. I am fairly new to the .Net framework and this will come in handy. However, you misunderstood my post. the blank strings are only there because i didnt want you to see my smtp information or actual email addresses. I understand how it works and i'm am writing it this way for a reason. Please dont worry this is by no means my final product. the program as suffered many tear downs and reassemblies because of my shear frustration of the matter. If you could give me an example of how i attach a png image to my email and have it actually allow me to use it, that would be extremely helpful. Like i said im learning aswell, so i will rewrite this program with your guidlines in mind. But none of that helps me if i cant get access to files...

Thanks, Cloudy
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,235
  • Joined: 05-May 12

Re: system.net.mail.attachments "Access to path is denied"

Posted 16 August 2012 - 11:22 PM

So which like line is the exception happening on? What was the value of pathimage.Text as you run by line 28?

This post has been edited by tlhIn`toq: 17 August 2012 - 06:04 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: system.net.mail.attachments "Access to path is denied"

Posted 17 August 2012 - 02:51 AM

private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            string filename = "";
            string path = "";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                filename = System.IO.Path.GetFileName(ofd.FileName);
                path = System.IO.Path.GetDirectoryName(ofd.FileName);
            }

            fitx.Text = filename;

            pathimage.Text = path;
            fitx.ReadOnly = false;
            pathimage.ReadOnly = false;
        }


Once you leave here, won't pathImage be simply the directory name where the file is located? Shouldn't the filename be a part of this? What is the value of pathImage in the mail sending function? Use your debugger and breakpoints to find out.
Was This Post Helpful? 1
  • +
  • -

#6 CloudyHLS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 12

Re: system.net.mail.attachments "Access to path is denied"

Posted 17 August 2012 - 10:51 AM

pathImage is acquired by a file dialog which gets the path to the image i want to attach. The code seems to work fine up to the point that it said i don't have access to the path! Ive gone over it 100 times and cant find the source of the issue. im beginning to think that the problem is right in front of me and im missing it. Do i need to add permissions to my program? The file is NOT read only so that is not the issue. Note i am using Windows 8. So this may be a windows 8 related issue as well, but id like to assume its not.

This post has been edited by tlhIn`toq: 17 August 2012 - 11:17 AM
Reason for edit:: Don't quote the entire previous post. We can call see it and all you do is make the thread needlessly long

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,235
  • Joined: 05-May 12

Re: system.net.mail.attachments "Access to path is denied"

Posted 17 August 2012 - 11:11 AM

The Attachment constructor that you using requires the filename. As noted by JackOfAllTrades in post #5, you are only passing in the directory of where the file lives, but not not giving it the filename.

Try changing line 28 to:
var pathName = Path.Combine(pathimage.Text, fitx.Text);
System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(pathName);


This post has been edited by Skydiver: 17 August 2012 - 11:11 AM

Was This Post Helpful? 2
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: system.net.mail.attachments "Access to path is denied"

Posted 17 August 2012 - 11:17 AM

Ultra basic breakpoints and debugging would show you the actual values in play here.

What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2. (Click the SHOW button below)



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,235
  • Joined: 05-May 12

Re: system.net.mail.attachments "Access to path is denied"

Posted 27 January 2015 - 10:50 AM

Please do not revive old posts, specially posts where the OP hasn't logged in in ages.

Closing this thread.

EDIT: Removed spam post to which Skydiver is referring.

This post has been edited by JackOfAllTrades: 28 January 2015 - 05:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1