问题
I have a small tcp server that is used, among other things, to send mails through a SMTP server.
The problem is, it works correctly when I run it on my development machine (a remote connection to the smtp server), however when I run the application on the same machine as the SMTP server (Windows Server 2008 R2) I get the following exception:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
The SMTP Server is configured to only allow relay from 127.0.0.1 and the IP address that the tcp application binds to, as well as my development machine. It's also configured to only allow connections from the same IP list.
The C# bit to send the mail is as follows:
var mail = new MailMessage();
mail.To.Add(recipient);
mail.From = new MailAddress("me@mydomain.com");
mail.Subject = "subject";
mail.Body = "message";
var smtpClient = new SmtpClient(MailServerAddress, Constants.MAIL_SERVER_PORT);
smtpClient.Send(mail);
MailServerAddress
is defined when the application is first run.
I've tried setting it to both 127.0.0.1 and the SMTP Server's configured IP address, but I still get the same exception.
Checking the server logs shows no sign of connection, which fits with the above exception.
What am I doing wrong here?
回答1:
Just a stab in the dark answer, but try this as I had a similar problem about 3 years ago but can't remember the fix, but this line of code stands out to me when I had a look
_client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
So maybe try (assuming your using IIS for SMTP):
var smtpClient = new SmtpClient(MailServerAddress, Constants.MAIL_SERVER_PORT);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtpClient.Send(mail);
回答2:
Answer copied from here:
Unable to read data from the transport connection: net_io_connectionclosed
What this error means is that System.net.mail was unable to find the smtp server.
The answer will vary depending on whether you have a fixed IP or a dynamic IP but,
basically, you need to assign a valid IP to your smtp server.
With fixed IP's this is relatively straightforward.
With dynamic IP's it takes a bit of tweaking.
Open the IIS Manager and check the properties for the smtp server.
In the Default SMTP Virtual Server's properties, in the "Access" tab,
in the Connection Control and Relay dialogs, make sure that your local IP is assigned.
( In my case, it's 10.0.0.2... )
You may also need to modify your hosts file, to point 127.0.0.1 to your machine name.
( \WINDOWS\system32\drivers\etc\hosts )
Then, in your code, assign your machine name to the smtp client :
SmtpClient client = New SmtpClient("yourmachinename")
client.Send(mail)
Something else is you may have your firewall blocking your test. Disconnect from the internet and disable your firewall if you can without posing a security risk. See if it works then. If it does you will need to look deeper into the firewall settings. If not, it really may be your IP address as explained above.
来源:https://stackoverflow.com/questions/15180772/smtp-client-fails-to-send-message-on-local-host-succeeds-on-remote-host