0

i have to do the following: my documents are stored on a server. The only path i'm getting is an URI I was trying to get the document but i'm keep getting a "403 Forbidden".

string[] myArray = context.Request.QueryString["ItemsArray"].Split(',');

            MailMessage m = new MailMessage(new MailAddress("bart@schelkens.be"),
                    new MailAddress("bart@schelkens.be")
                    );

            m.Subject = "Emailing documents";

            foreach (string path in myArray)
            {
                using (WebClient wb = new WebClient())
                {
                    wb.UseDefaultCredentials = false;
                    wb.Credentials = new NetworkCredential(_userName, _passWord);
                    var stream = wb.OpenRead(path);

                    m.Attachments.Add(new Attachment(stream, ""));
                }
            }

            string mailBody = "Dear<BR/> Please find attached a new version of the documents.";
            mailBody += "<BR/>";

            m.Body = mailBody;
            m.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient("hybrid.kaneka.be");
            smtp.EnableSsl = false;

            smtp.Send(m);

Or is there another way to get my document from the website and then mail it?

In SharePoint I created my own button which, using a js-file, goes to my ashx to mail my documents.

Bart Schelkens
  • 1,235
  • 4
  • 21
  • 45

2 Answers2

0

Try to add the following line before opening the stream:

wb.Headers.Add("User-Agent: Other");

It could be, that it's forbidden because your server doesn't think, that you are a trusted client and blocks all connections without a User-Agent.

[edit 1]

If you try to download a file from SharePoint you have to use the SharePointOnlineCredentials from the SharePoint Server 2013 Client Components SDK:

Somewhere you have to define the credentials:

const string username = "username";
const string password = "password";
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
var credentials = new SharePointOnlineCredentials(username, securedPassword);

After that you can user this credentials in your WebClient:

wb.Credentials = credentials;

[edit 2]

If your on in an on-premises environment than you could use NetworkCredentials. Try to use the following:

wb.Credentials = new NetworkCredential("DOMAINNAME\username", "password");

[edit 3]

Maybe your server wants a real User-Agent. Because if something is wrong with your credentials there would be a 401 (unauthorized). For example try:

wb.Headers.Add("user-agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
Claudio P
  • 2,133
  • 3
  • 25
  • 45
  • Still got the same error : The remote server returned an error: (403) Forbidden. – Bart Schelkens Mar 26 '15 at 09:06
  • @BartSchelkens You get the error on this line: `var stream = wb.OpenRead(path); `? – Claudio P Mar 26 '15 at 09:08
  • If your in a domain try to add the domain at your NetworkCredentials or maybe [this](http://stackoverflow.com/a/1702477/3455216) answer helps. There he uses a CredentialCache because the server uses NTL authentication. – Claudio P Mar 26 '15 at 09:17
  • Still getting the same error. The files are "hosted" in SharePoint. Could this be causing the problem? – Bart Schelkens Mar 26 '15 at 09:23
  • If the account which runs your application is a valid user in your sharepoint site you could try the UseDefaultCredentials. If this doesn't help [this](http://stackoverflow.com/questions/23684101/download-file-from-sharepoint-365) may help. – Claudio P Mar 26 '15 at 09:30
  • I've tried the defaultcredentials already and it didn't work. We are using SharePoint 2013. – Bart Schelkens Mar 26 '15 at 09:36
  • @BartSchelkens I updated my answer to fit for downloading a file from SharePoint 2013 – Claudio P Mar 26 '15 at 09:42
  • there is no other way? – Bart Schelkens Mar 26 '15 at 09:44
  • @BartSchelkens I updated my answer again: If this doesn't help there is no other option, as I know. – Claudio P Mar 26 '15 at 09:55
0

Provide the READ/WRITE rights/permission on the folder where your document is located.

Currently there is no read/write rights/permission on that folder that's why when ever you try to attach the document it returns 403 Forbidden error.

  • but when I'm going directly to the URI, i get the document – Bart Schelkens Mar 26 '15 at 10:04
  • Because that is managed by Sharepoint. and if i am not wrong then you are trying to attach file from IIS. So just give a try according following link to assign permission : http://sanuja.com/blog/resolving-iis-403-forbidden-access-is-denied – Aarifmohammad Mansuri Mar 26 '15 at 10:08