Pergunta

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 !

Foi útil?

Solução

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!

Outras dicas

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.

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.

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top