I have an Event Receiver that was working on our Test Farm, then stopped working (with the error below) after some server updates and restarts. I moved it to our Staging Farm, and it works there sometimes but more often returns the error below:
Category: SharePoint Guidance: An exception has occurred. ExceptionType: 'WebException' ExceptionMessage: 'The remote server returned an error: (401) Unauthorized.'
The event receiver is set up to send an email with an attachment of the pdf file that was just loaded into the Document Library. The users who get the email depend on various factors.
Part of the code in question is below. When it fails, it is at "Dim data as Byte() = webclient.DownloadData(strURL)"
strURL = properties.Web.Site.Url + "/" + properties.ListItem.Url
...
Dim webClient As System.Net.WebClient = New System.Net.WebClient
Dim impContext As WindowsImpersonationContext
impContext = WindowsIdentity.GetCurrent(TokenAccessLevels.MaximumAllowed).Impersonate()
Using impContext
webClient.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim data As Byte() = webClient.DownloadData(strURL)
impContext.Undo()
Dim memoryStreamOfFile As System.IO.MemoryStream = New System.IO.MemoryStream(data)
mailMessage.Attachments.Add(New Attachment(memoryStreamOfFile, properties.ListItem.Name, "application/pdf"))
Dim smptClient As SmtpClient = New System.Net.Mail.SmtpClient(smtpServer)
mailMessage.IsBodyHtml = True
smptClient.Send(mailMessage)
End Using
All 4 servers in the Farm have Windows Authentication, and ASP.Net Impersonation enabled.
I checked web authentication mode in Powershell and it is Windows.
Any help with what could be causing this and how to fix it would be great. Again, it does work sometimes.
Thanks!