3
votes

I am trying to send an email using C# on a Windows 2016 server, running IIS 10. I installed IIS SMTP and it can send out messages using both Telnet and the windows pickup service.

Now I am using a C# webservice to send an email out. At first I tried to use SmtpDeliveryMethod.Network like this:

MailMessage mail = new MailMessage(mailFrom, mailTo);
SmtpClient client = new SmtpClient();

client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "localhost";

however that resulted in this error: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25

So as the pickup service of the IIS SMTP (on C:\inetpub\mailroot\Pickup) is working I thought it would be best to just start using the pickup service.

Using this code:

MailMessage mail = new MailMessage(mailFrom, mailTo);
SmtpClient client = new SmtpClient();

client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

However this results in a Cannot get IIS pickup directory error.

So in IIS Services manager under SMTP I check the bullet at "Store e-mail in pickup directory:" And added C:\inetpub\mailroot\Pickup in the text field.

This didn't work however. So I also changed the web.config and added these lines:

<system.net>
<mailSettings>
  <smtp deliveryMethod="SpecifiedPickupDirectory">
    <specifiedPickupDirectory pickupDirectoryLocation="C:\inetpub\mailroot\Pickup"  />
  </smtp>
</mailSettings>
</system.net>

This however didn't do the trick either. This will result in the following error:

Failure sending mail. ---> System.IO.DirectoryNotFoundException: Could not find a part of the path &#39;C:\inetpub\mailroot\Pickup\bb3fc5af-e213-43d3-af47-cb2836de78c3.eml&#39;.

I still get the same error and don't know how to fix this.

Could you please help out?

Best regards

1
client.Host = "localhost";...have you tried specifying the same host name you configured for the smtp. - Ross Bush
Just tried it and results into: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed. - BonifatiusK
Could it be an anti-virus program blocking your connection? - Duston
It's not the anti virus program, we disabled it and still we cannot sent out mails with IIS / C# - BonifatiusK
As follow up to the last DirectoryNotFoundException. Maybe too obvious, but because you didn't mention; does the account/identity configured on the ApplicationPool have write (and read) rights to that folder? IIS often gives a not-found-alike error instead of eg. access-denied. - pfx

1 Answers

0
votes

I couldn't get it to work. Eventually I just brought everything back to it origin and started over.

It works now, but what it is exactly I don't know for sure.