user323395
user323395

Reputation:

ASP MVC: Sending an E-mail

I'm kind of new to the .NET platform. And currently I'm learning ASP.NET 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("");
        mail.To.Add("");

        //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:

http://img153.imageshack.us/img153/695/capture2p.pnghttp://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!

Upvotes: 4

Views: 6922

Answers (3)

Anders Nygaard
Anders Nygaard

Reputation: 5552

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

Upvotes: 0

Krishna
Krishna

Reputation: 486

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.

Upvotes: 6

TomTom
TomTom

Reputation: 62093

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.

Upvotes: 0

Related Questions