-1

I am writing an application that is supposed to send an email, with up to 3 attachments.

It is just a really simple web form, with 3 FileUpload controls to browse the possible attachments.

The application is deployed in a webfarm and of course runs on server-side.

I managed to make it send the emails, but I am having problems with the attachments. Right now, I am using this procedure to attach the files:

                if (fuAttatchment.HasFile)
                {                        
                    fuAttatchment.SaveAs(Server.MapPath(fuAttatchment.FileName));
                    MyMessage.Attachments.Add(new System.Net.Mail.Attachment(Server.MapPath(fuAttatchment.FileName))); 

                    filesize += fuAttatchment.PostedFile.ContentLength;
                }

The error I am getting once I submit, is the following:

Send failure: System.UnauthorizedAccessException: Access to the path 'E:\Inetpub\IS\MSTicketRequest\wallpaper-3010.jpg' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at System.Web.UI.WebControls.FileUpload.SaveAs(String filename) at MSTicketRequest.WebForm1.btnSubmit_Click(Object sender, EventArgs e) in C:\Users\ggruschka\Desktop\ggruschka\MSTicketRequest\MSTicketRequest\Default.aspx.cs:line 54

I have not been able to figure out why is this happen, probably I am missing something regardin security policies or something like that.

Thank you very much in advance for your help !

Guillermo Gruschka
  • 167
  • 1
  • 3
  • 16

4 Answers4

2

instead of this:

fuAttatchment.SaveAs(Server.MapPath(fuAttatchment.FileName));
                    MyMessage.Attachments.Add(new System.Net.Mail.Attachment(Server.MapPath(fuAttatchment.FileName))); 

do this:

fuAttatchment.SaveAs("somewhere local"+fuAttatchment.FileName);
                    MyMessage.Attachments.Add(new System.Net.Mail.Attachment("somewhere local"+fuAttatchment.FileName)); 

you don't need to be saving the attachments on the server!

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • Thank you for your help! I will try this and let you know ! – Guillermo Gruschka Oct 04 '12 at 22:23
  • I replaced that part of the code with your suggestion, but instead of a string for a fixed local path (where "somewhere local" is), I used Path.GetDirectoryName(fuAttatchment.PostedFile.FileName), and then add the path to the filename, but it is not working. Is it mandatory to use a fixed path for the attachments? – Guillermo Gruschka Oct 04 '12 at 22:51
  • 1
    please first test it with a fixed path, although you dont need a fixed path – Alex Gordon Oct 04 '12 at 22:58
  • I tried with a fixed path, but I am getting this error: **Send failure: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\ggruschka\Pictures\wallpaper-10053.jpg'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at...** – Guillermo Gruschka Oct 09 '12 at 15:53
  • @GuillermoGruschka do you know the Directory Not Found means? – Alex Gordon Oct 10 '12 at 15:28
1

Looks like the user that the site is running under doesn't have access to write to the target file path. Check the directory's security permissions and make sure the IIS user has write access.

hyru
  • 189
  • 6
0

Depends on what type ur application pool is. But if it is networkservice you gotta add networkservice.
IIS_Users for ApplicationPoolIdentity but I'm not sure on this one. http://www.windowsecurity.com/articles/understanding-windows-ntfs-permissions.html

If that doens't help you can try to remove the read only option.

VRC
  • 745
  • 7
  • 16
  • Can't the web application just use the file in the client's hard drive ? is this possible ? – Guillermo Gruschka Oct 04 '12 at 22:01
  • Yeah but your web application has to have access to the file. Web Application runs under an Application Pool Type and that type needs to have access to the file. – VRC Oct 04 '12 at 22:19
0

You an send email via your gmail account. Here is how to do it (I dunno if it helps). 1. You need textbox where you are going to upload attachment. 2. Button 'Browse', and 'OpenFileDialog1'. In button 'Browse' you put this

private void btnBrowse_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            txt_attachment.Text = openFileDialog1.FileName;
        }
    }

You need button 'Send with an attachment' in which you place this:

MailMessage mail = new MailMessage(txt_gmail.Text, txt_to.Text, txt_subject.Text, txt_body.Text);
        mail.Attachments.Add(new Attachment(txt_attachment.Text));
        SmtpClient client = new SmtpClient(txt_server.Text);
        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential(txt_gmail.Text, txt_password.Text);
        client.EnableSsl = true;
        client.Send(mail);
        MessageBox.Show("Mail sent", "Succes", MessageBoxButtons.OK);
        foreach (Control control in this.Controls)
        {
            TextBox box = control as TextBox;
            if (box != null)
            {
                box.Text = "";
            }
        }
    }

an the last thing (because when you do this it will show some errors) you need to create Gmail.dll file. Here is the link ho to do it:Here you can create Gmail.dll

I hope this helps.

D'Jok
  • 51
  • 2
  • 10