4

I have an asp.net mvc app running on a local iis website that is failing to send mail using SmtpClient from System.Net.Mail. I am trying to send a mail to my hotmail address and I get a socket exception. Any idea what the problem might be?

using(var message = new MailMessage())
            {
                message.From = new MailAddress(AdminEmail);
                message.To.Add(new MailAddress(to));
                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = false;

                var mailClient = new SmtpClient();
                mailClient.Send(message);
            }

Here is the exception:

{System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)}

I think the default app pool does not have access to intepub\wwwroot. Here is the error that I get when trying to test the connection:

The server is configured to use pass-through authentication with a built-in account to access the specified physical path. However, IIS Manager cannot verify whether the built-in account has access. Make sure that the application pool identity has Read access to the physical path. If this server is joined to a domain, and the application pool identity is NetworkService or LocalSystem, verify that \$ has Read access to the physical path. Then test these settings again.

tereško
  • 58,060
  • 25
  • 98
  • 150
Joe
  • 409
  • 2
  • 9
  • 19
  • Do you have an smtp server running on your localhost? – GregD Nov 28 '08 at 22:57
  • I think this is part of my problem. I opened IIS Manager and set the SMTP server as localhost but it still does not work. Is there anything else that needs to be configured? – Joe Nov 28 '08 at 23:18
  • Can you telnet to port 25 on your local machine? What OS are you running? Do you have a firewall on the local machine? – GregD Nov 28 '08 at 23:37
  • I am running vista home premium. I do have a firewall but would that matter since the connection is outgoing? Also, I am not sure how to get started with telnet. – Joe Nov 28 '08 at 23:55

4 Answers4

3

Based on your answers to my comments above Joe, you don't have SMTP enabled on your local machine. Vista does not come with SMTP.

As such, you'll either have to install a 3rd party SMTP app that will run on Vista, or use another app to send via, in this case, your Hotmail account may allow you to send outgoing via it. I don't use Hotmail so don't know if it will or not but it should be something like smtp.hotmail.com and your credentials. My primary account is a gmail account, so I can use it via smtp.gmail.com and of course, my credentials.

bkaid
  • 51,465
  • 22
  • 112
  • 128
GregD
  • 6,860
  • 5
  • 34
  • 61
  • I think I understand now. I have specified the SMTP server as localhost in IIS so my webapps will send mail to an STMP app on localhost where it will be relayed. But I need to set up an app to do that first. Does that sound right? – Joe Nov 29 '08 at 23:15
2

For development on your local machine, you can also use a drop folder:

http://www.codersbarn.com/post/2008/11/30/ASPNET-Contact-Form.aspx

bkaid
  • 51,465
  • 22
  • 112
  • 128
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
0

Rather than trying to setup SMTP locally, why don't you just configure the SMTP connection to send directly via hotmail, just add the configuration elements to the web.config to enable everything.

Edit

I think this article that I wrote a while back might help you out.

bkaid
  • 51,465
  • 22
  • 112
  • 128
Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • @Joe - You can either specify the credentials when you declare the SMTP client, OR you can work with the web.config section. added a link to the post – Mitchel Sellers Dec 01 '08 at 15:29
-1

Try explicitly setting the host of the SmtpClient.

Ex:
SmtpClient mailClient = new SmtpClient();

mailClient.Host = "127.0.0.1";

mailClient.Send(message);

Matt
  • 619
  • 5
  • 12