views:

130

answers:

3

Okay I'm kindoff new to the .NET platform. And currently i'm learning ASP MVC.

I want to send an e-mail from my program and i have the following code:

public void sendVerrificationEmail()
    {
        //create the mail message
        MailMessage mail = new MailMessage();

        //set the addresses
        mail.From = new MailAddress("[email protected]");
        mail.To.Add("[email protected]");

        //set the content
        mail.Subject = "This is an email";
        mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
        mail.IsBodyHtml = true;

        //send the message
        SmtpClient smtp = new SmtpClient("127.0.0.1");
        smtp.Send(mail);
    }

Now when i execute this code i'll get the following exception:

System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25

Now i'm very very new to the IIS manager & stuff. so there is probably something wrong there.

Do i need to install a virtual smtp server or something?. Currently i have following settings (sorry not allowed to uplaod images yet =) ):

http://img153.imageshack.us/img153/695/capture2p.png

I've been looking for a few hours now but i can't seem to find a working solution.

Help would be appreciated!

Tyvm.

A: 

Well, as you try to send the email to the SMTP service running at 127.0.0.1 - there actually should OBVIOUSLY one run there to accept the email, or ;)?

Nothing about IIS manager - simple common sense suffices.

Basically:

  • Do NOT set the relay smtp service there. Just do not do it.

  • Configure the smtp service in the IIS config - this way it goes into your web.config and is not hardcoded in possibly a lot of places in your application.

TomTom
+2  A: 

as you are calling

SmtpClient smtp = new SmtpClient("127.0.0.1"); 

There should be a SMTP Server on localhost. If it is not there then you can use MailServer of your network.

for Testing purpose you can use

<system.net>
<mailSettings>
      <smtp from="[email protected]" deliveryMethod="SpecifiedPickupDirectory">
        <network host="127.0.0.1" port="25" userName="userID" password="*****" defaultCredentials="true" />
        <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
      </smtp>
    </mailSettings>
  </system.net>

This will save your emails in C:\temp\mail without sending it.

Krishna
Okay thanks, Is there a SMTP server included with visual studio 2010 or something?
wh0emPah
Oh and one more problem. The code for sending the email is in a class library. I don't think it can access the settings in my web.config?
wh0emPah
if that is so, you can set all these properties in code-behind.
Veer
A: 

I usually send mail via GMail's SMTP service from localhost. I have another configuration for when the project is uploaded to my web host.

Here's an example: http://www.shabdar.org/send-email-using-gmail-account-asp-net-csharp.html

Anders Nygaard