Ron Ron - 21 days ago 5
ASP.NET (C#) Question

Setting up SMTP loally without username, password

I am trying to setup SMTP relay to send mails from the web application without username and password. I read that you can setup locally on your IIS and use "No Authentication" enter image description here
In web.config, these are my settings:

<appSettings>
<add key="SmtpServerAddress" value="localhost" />
<add key="SmtpServerPort" value="25" />
<add key="SmtpServerTimeout" value="30" />
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="">
<network host="localhost" port="25" />
</smtp>
</mailSettings>




And my code-behind to send email is :

SmtpClient sc = new SmtpClient();
sc.Host = "localhost";
sc.Port = 25;
sc.UseDefaultCredentials = true;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
sc.Send(mm);
}
catch (Exception ex)
{
throw ex;
}


When I submit the click event, to send mail, I am getting "remote server not found".

Could you shed some light on this ?

Answer

You cannot add localhost and start sending emails (unless there actually IS an SMPT server running). You still need a "real" SMTP server. Those settings a nothing more then adding a default SMTP server in the Web.Config.

If you don't set those settings in IIS, you send mail like this. There are may examples of this on SO.

using (SmtpClient client = new SmtpClient())
{
    client.Host = "mail.fakedomain.nl";
    client.Port = 25;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("fake@fakedomain.nl", "abcd123");

    //send mail
}

But if you set the default settings in IIS, you can do this

using (SmtpClient client = new SmtpClient())
{
    //send mail
}

It saves a few lines of code.

Are you trying this on your development computer? If so you could install hMailServer