I'm trying to set up a SMTP server on my Windows 7 machine in IIS7. I have set it to "Deliver email to localhost, port 25, no authentication. But when I try to connect programmatically from my C# program, I get an error:
Failure sending mail", inner exception "No connection could be made because the target machine actively refused it 127.0.0.1:25
public static void SendEmail(MailMessage m) {
var smtp = new SmtpClient {
Host = "localhost",
Port = 25,
UseDefaultCredentials = true,
};
smtp.Send(m);
}
Why? What other secret switch do I have to flip?
For development purposes I use storing mails to filesystem, try this in the web.config
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\temp\mails\" />
</smtp>
</mailSettings>
Sorry, but most of these answers were totally confusing and don't explain the problem . Here is what the problem is.
The "SMTP E-mail" in IIS 7, on both Windows 7 Professional and "real" web server like Windows Server 2008 is that SMTP E-mail is not a true "virtual SMTP server" or what Microsoft calls "Simple Mail Transfer Protocol (SMTP)". Its just a an interface that allows you to apps that to an SMTP server online. The virtual SMTP server we used to have on older Windows is now only available as an add-on on server operating systems using the "Server Manager" under Administrative Tools and clicking "add features". Thats not found on say Windows 7 Professional. Yet another Microsoft blunder!
However, you can still use the "SMTP E-mail" piece under IIs in development or even your Web Server to route mail out to a real SMTP server. Its not like the old days when both were one and the same and you could route email out and back to your local box, etc for testing. They let you store that locally but thats not much help to me. Thats why when in SMTP E-mail "localhost" doesn't work. Thats what most people are saying. For that you would need to install third party software. A better solution is to just get the Windows Server Admin pack which has the virtual server and all the web server goodies found on the server OS and install that: http://www.sysprobs.com/install-admin-pack-windows-7-remote-desktop-manager
Keep in mind, you don't have to run ANY real SMTP virtual server on either your local box or on the server as long as you have an address to a real SMTP service (like "mail.yourwebhost.com"). Under IIS7, just click your SMTP E-mail piece under IIS7 and type in the address. But "localhost" wont wor'k. Usng SMTP E-mail with a remote host, I've found most SMTP or email providers require two additional things: a port other than "25", and you adding custom authentication credentials found under SMTP E-mail. Network Solutions likes to use your email address for the login and your address password. I hooked all that up and the mail c# object sent out mail without a virtual SMTP server on my local box. Last trick is be sure to also add this to your c# code:
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(
"mail.yourhost.com",155);//add custom port here
//This object stores the authentication values
System.Net.NetworkCredential mycredentials = new System.Net.NetworkCredential(
"[email protected]", "passwordhere");
client.UseDefaultCredentials = false;
client.Credentials = mycredentials;
Best method I found was to use SMTP4Dev, which listens for emails, and shows you what was "sent", but doesn't actually send anything. Great for testing!
You need to setup SMTP server in IIS7, here are the instructions how to setup:
http://learn.iis.net/page.aspx/751/configure-smtp-e-mail-in-iis-7/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With