tags:

views:

2721

answers:

5

I am trying to send email from an ASP.NET web application using the SmtpClient class. So far I have granted relay access to 127.0.0.1. I am trying to send test emails to my gmail account. The EML files get stuck in the mailroot's queue folder. My WinXP firewall is disabled. I dont get any exceptions in the code but the emails are never delivered to the destination address. I also tried other email accounts.

        SmtpClient client = new SmtpClient();
        client.Host = "127.0.0.1";

        MailMessage message = new MailMessage();
        message.To.Add("[email protected]");
        message.From = "[email protected]";
        message.Subject = subject;
        message.IsBodyHtml = false;
        message.Body = body;

        client.Send(message);

Here is a snippet from the IIS SMTP log.

Software: Microsoft Internet Information Services 5.1
Version: 1.0
Date: 2009-01-16 18:28:28
Fields: time c-ip cs-method cs-uri-stem sc-status 
18:28:28 127.0.0.1 EHLO - 250
18:28:28 127.0.0.1 MAIL - 250
18:28:28 127.0.0.1 RCPT - 250
18:28:28 127.0.0.1 DATA - 250
18:29:45 127.0.0.1 MAIL - 250
18:29:45 127.0.0.1 RCPT - 250
18:29:45 127.0.0.1 DATA - 250
18:30:37 127.0.0.1 QUIT - 0
A: 

I am still testing this Jim, one thing that I have noticed is that you are not setting your from address correctly. From accepts a MailAddress class not a string.

Change:

message.From = "[email protected]";

To:

message.From = new MailAddress("[email protected]");

Under Default SMTP settings:

Is the "IP address:" dropdown set to 127.0.0.1 or (All Unassigned)? If possible have it set to All Unassigned. Try adding the ip address bound to your machine to be able to relay through the SMTP service. Though your sending email to your host 127.0.0.1, it may be sending it from your local network assigned ip. To make sure stuff is working you may want to select the radio button in Relay Restrictions to "All except the list below" and clear the list. This will open up the relay to test your configuration. You should restrict this once you figure out what your issue is.

Are there any error codes listed in your SMTP log?

Brettski
Thanks for the reply. I have tried your suggestions, but changing the IP Address from 'All Unassigned' to my machine IP resulted in an exception.Unable to read data from the transport connection: net_io_connectionclosed.
Sorry, I think I confused things in my writeup. I suggest the dropdown be set for All Unassigned. I suggest you set the machines which are allowed to relay through the smtp service to all (for testing) or at least your machine ip and localhost.
Brettski
A: 

If the code returns no exceptions then it's unlikely to be anything there. If you look at the raw .EML file, check through there and look for anything out of place.

However, the most likely problem is just give IIS a kick "iisreset". Do that and you'll probably see the messages start to get sent. Configuration changes on the SMTP server don't always come into play until a restart.

Robin Day
+1  A: 

Depending on how your network is set up, you might need to tell IIS to pass on emails to your Exchange server to be passed on to the outside world. To do this:
1. Right-click on the Default SMTP Virtual Server node in the IIS MMC Snap-in and open the Properties dialog.
2. Go to the Delivery tab and click on the Advanced button at the bottom of the tab.
3. In the Smart Host box, enter the name of your Exchange server. (Or a DNS entry for the same e.g. smtp.mydomain.com)

PhilPursglove
A: 

Check your event viewer, then you will see something like this

Message delivery to the remote domain 'yahoo.com' failed for the following reason: An SMTP protocol error occurred.

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

(This is mine). I think yahoo must have some kind of protocol? Or we are setting the wrong protocol? Still figuring.. Email me pls [email protected] (with a nice subject so I don't think as spam)

+1  A: 

I've just been fighting with the same problem and I think I know why - and the solution.

The problem and how to test for it
Firstly, I checked that I could connect out over port 25. Easily done by opening telnet in a command prompt and typing, for example (the little circle is the letter O on it's own, not a symbol :));

 o auth.smtp.1and1.co.uk 25

If that connects (rather than gives you an error) then it's not a firewall problem with port 25. If it does give you an error then first check your firewall settings until this works.

Next I enabled logging in the SMTP service and ticked all the extended properties. This then gave me two URLs in the log file. In my case they were
http://www.spamhaus.org/query/bl?ip=217.44.248.144
http://www.sorbs.net/lookup.shtml?217.44.248.144

Following those links it basically became clear that someone out there (I guess the receiving server) is checking and seeing that I am sending from a dynamic IP Address (I am just doing this from home over ADSL) and so blocking it as SPAM.

The solution
You need to configure your SMTP Server to relay via your ISP's SMTP Server.
1. Go to the Properties for the SMTP Service and go to Delivery -> Advanced
2. Put the name of your ISP's SMTP server in the "Smart host" field (for example auth.smtp.1and1.co.uk)
3. Save that
4. Back on the Delivery tab, click on the Outbound Security tab
5. Click on "basic authentication" and specify the username and password you use to access your email.

Note; I have assumed above that you have to authenticate to your ISPs SMTP server and that they use the same uid/pwd for POP3/IMAP and SMTP as that is the most common. Your situation may vary - check your email settings in your email program.

Frans