Hi All,
I need to send email from a C# application from localhost. I was successful in sending email using gmail SMPT port and host. But i need send email from local host SMPT virtual server. I followed the steps explained in http://forums.asp.net/t/268037.aspx and but the mail was not sending from my application. Also when i execute the command telnet 25 in commad promt. It shows "Connecting To mail.contoso.com...Could not open connection to the host, on port
25: Connect failed". Please help me to solve this.
Thanks

Dani AI

Generated

Good news: your Gmail test proves your code path is fine. The telnet attempt is the real clue. Do not test with mail.contoso.com (that is only a docs placeholder). Instead, verify your local listener with telnet 127.0.0.1 25. If you still see “Connect failed,” either the SMTP service is not installed/running, port 25 is blocked by Windows Firewall, or you’re connecting to the wrong host/port. Microsoft’s SMTP telnet guide shows the expected success/failure messages and common causes. (learn.microsoft.com)

Checklist to make the IIS SMTP virtual server work locally (building on ’s firewall note): install the SMTP Server feature (plus IIS 6 Management Compatibility), start the “Simple Mail Transfer Protocol (SMTP)” service, then allow port 25 inbound in Windows Firewall. In IIS 6 Manager, under Default SMTP Virtual Server > Access, grant 127.0.0.1 permission to connect and to relay; if your ISP blocks outbound 25, configure a Smart host. Microsoft’s setup articles cover enabling SMTP, relay restrictions, and Smart host configuration. (learn.microsoft.com, support.microsoft.com)

Code-wise, move off System.Web.Mail (used by and ); it’s obsolete and the recommended replacement is System.Net.Mail. For a true localhost SMTP test, target the network listener; for a no-network test, drop to the IIS Pickup folder and let the service deliver.

using System.Net.Mail;

var msg = new MailMessage("from@example.com", "to@example.com")
{
    Subject = "Test from localhost",
    Body = "Hello from IIS SMTP."
};

// Option A: send via local SMTP listener on port 25
using (var client = new SmtpClient("localhost", 25))
{
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Send(msg);
}

// Option B: drop into IIS Pickup (requires IIS SMTP installed)
using (var client = new SmtpClient())
{
    client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
    client.Send(msg);
}

System.Web.Mail types map to System.Net.Mail equivalents, and SmtpClient supports the Pickup and Network delivery methods shown above. Note: as of 2025 Microsoft no longer supports the legacy IIS 6 SMTP service for relaying to Exchange/M365; it’s fine for local dev, but plan a supported relay for production. (learn.microsoft.com)

Recommended Answers

All 5 Replies

You should have to enabled - "Internet Mail Server (SMTP)" of firewall.

use this code for "localhost"

using System.Web.Mail;
.
.
.
	MailMessage xMailMessage = new MailMessage();
	xMailMessage.From = "My Test System <test@test.com>";
	xMailMessage.To = "vatoovatoo@mousavi.net";
	xMailMessage.Subject = "This is a test";
	xMailMessage.BodyFormat = MailFormat.Html;
	xMailMessage.Body = "<html><head><title>Test emial</title></head><br /><br /><hr>JUST A TEST<hr><br /><br /></body></html>";

	SmtpMail.SmtpServer = "localhost";
	SmtpMail.Send(xMailMessage);	
.
.
.

have a look at the attached.

Hi


MailMessage obj = new MailMessage();

obj.From = "";
string Email = EmailId.ToString();

obj.To = Email;
obj.Subject = "Forgot SnapID";
obj.Body = flag.ToString();
obj.BodyFormat = MailFormat.Html;

SmtpMail.SmtpServer = "127.0.0.1";
SmtpMail.SmtpServer = "Localhost";
SmtpMail.Send(obj);

thanks

Shobhitzone

Hi All,
I need to send email from a C# application from localhost. I was successful in sending email using gmail SMPT port and host. But i need send email from local host SMPT virtual server. I followed the steps explained in http://forums.asp.net/t/268037.aspx and but the mail was not sending from my application. Also when i execute the command telnet 25 in commad promt. It shows "Connecting To mail.contoso.com...Could not open connection to the host, on port
25: Connect failed". Please help me to solve this.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.