1

I'm creating a WinService to send E-mails and now I am trying to send a file via e-mail from a shared network. When I do the debugging I can send and receive the attachment successfully, but when I run the WinService it says that Access to the path '\\SharedPath\shared_file.txt' is denied.

Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential("user", "password")
SmtpServer.Host = "smtp.example.com"
SmtpServer.Port = 587
SmtpServer.EnableSsl = True

mail.From = New MailAddress("src_address@example.com")
mail.To.Add("dst_address@example.com")
mail.Subject = "E-Mail subject"
mail.Body = "Email body text."

Dim attachment As New Attachment("\\SharedPath\shared_file.txt")
mail.Attachments.Add(attachment)

SmtpServer.Send(mail)

Any suggestion?

mafap
  • 371
  • 3
  • 18
  • 40
  • 1
    Check the answer here. Accessing shared folder over a network in windows service (http://stackoverflow.com/questions/6016388/accessing-shared-folder-over-a-network-in-windows-service) – Ceres Jul 29 '15 at 17:00
  • My service is using the _LocalSystem Account_. Should I change it to _NetworkService Account_? And what's the reason why this works in the debugging mode and not when running the service itself? – mafap Jul 29 '15 at 23:01
  • From the q&a Ceres pointed you to, the first answer linked to this [MSDN how to: Debug a windows service application](https://msdn.microsoft.com/en-us/library/7a50syb3.aspx) – CBRF23 Jul 30 '15 at 00:05
  • @CBRF23 I am using another way to debug my windows service. Anyway, I granted the Log On As a Service right but it doesn't work yet. – mafap Jul 30 '15 at 10:17
  • Try to use your own account on service installation. Also, check if file exists before attaching it. – NETRookie Jul 30 '15 at 12:20
  • It must be a user permission's issue in the network. Supposing that [this](http://stackoverflow.com/questions/3622089/windows-service-cant-access-network-share) would solve my problem, what if I had a Remote Desktop Connection with a shared drive? Would it be the same or what should I do? – mafap Jul 31 '15 at 00:50

0 Answers0