Reputation: 1931
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" 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 ?
Upvotes: 1
Views: 3718
Reputation: 35514
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("[email protected]", "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
Upvotes: 2