none
Problemas para enviar e-mail com anexo RRS feed

  • Pergunta

  • Bom dia,

    Escrevi um código para enviar e-mail com anexo, localmente rodou perfeitamente, porém após publicar o projeto tive problemas relacionados a permissão no servidor, o erro é o seguinte:

    Access to the path 'c:\windows\system32\inetsrv\registro_000023.xls' is denied.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.UnauthorizedAccessException: Access to the path 'c:\windows\system32\inetsrv\registro_000023.txt' is denied.

    ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

    To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Segue o código:

            string filePath = FILE_PREFIX + this.registry.ui_id_registry.ToString().PadLeft(6, '0') + FILE_SUFIX;
            XmlTextWriter escritor = new XmlTextWriter(filePath,
            System.Text.Encoding.Unicode);
            escritor.WriteStartElement("registro");<br/>        ...<br/>        ...
    

        public static bool sendEmail(string cliente, string file, string messageText, string subject) 
        {
          bool toReturn = false;
          SmtpClient smtpClient = new SmtpClient();
          MailMessage message = new MailMessage();
          try
          {
            message.To.Add(cliente);
            message.Subject = subject;
            message.Body = messageText;
            message.Attachments.Add(new Attachment(file,"text/xml"));
            smtpClient.Send(message);
            toReturn = true;
          }
          catch (Exception ex)
          {
            toReturn = false;
          }
    
          return toReturn;
        }
    

    Se eu colocar o arquivo no diretório da aplicação terei o mesmo problema? como faço parra pegar o app.path?

     

    obrigado

     

     

     

    sexta-feira, 3 de junho de 2011 12:50

Respostas

  • Marcelo o correto e vc dar permissão para o user ASP.NET :)
    Não esqueça de usar o componente </> na barra para posta seu código. Microsoft MCPD,MCTS,MCC
    • Sugerido como Resposta Luiz Estevam domingo, 5 de junho de 2011 20:10
    • Marcado como Resposta Harley Araujo terça-feira, 7 de junho de 2011 10:43
    sexta-feira, 3 de junho de 2011 17:12
    Moderador

Todas as Respostas

  • Bom,

    Agora esta funcionando, coloquei o path da maneira abaixo

    string filePath = HttpContext.Current.Server.MapPath("~") + "\\temp\\" + "arquivo.xml";
    

    Na pagina administrativa da locaweb dei permissão de escrita para o diretório \temp (estava somente leitura)

     

    Porém fiquei com essa pasta aberta, o que acredito que seja um problema em termos de segurança.

     

    Comentários são bem vindos

     


    sexta-feira, 3 de junho de 2011 14:55
  • Marcelo o correto e vc dar permissão para o user ASP.NET :)
    Não esqueça de usar o componente </> na barra para posta seu código. Microsoft MCPD,MCTS,MCC
    • Sugerido como Resposta Luiz Estevam domingo, 5 de junho de 2011 20:10
    • Marcado como Resposta Harley Araujo terça-feira, 7 de junho de 2011 10:43
    sexta-feira, 3 de junho de 2011 17:12
    Moderador