Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

I was trying to send email using ASP.NET 2.0 using the following code:

MailMessage message = new MailMessage();
        message.From = 
new MailAddress("iamazam@hotmail.com");
        message.To.Add(
new MailAddress("azamsharp@gmail.com"));
       
        message.Subject = "This is my subject";
        message.Body = "This is the content";
        
        SmtpClient client = 
new SmtpClient();
        client.Host = "localhost"; 
        client.Send(message);  

but I keep getting the error:

Failure sending mail.Unable to connect to the remote server

The bad thing is that there is no documentation available which tells that how you can send an email using localhost as your default server.

There was a huge problem in ASP.NET 1.X with the CDONTS thingy and now this error. Why is'nt there a simple solution which let's you send a simple email?


 

powered by IMHO

 

Posted on Wednesday, December 21, 2005 7:53 PM | Back to top


Comments on this post: Hey Microsoft! Can you make this any harder?

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
Hey Mohammed, did you check to see if there's an SMTP server running on your local machine? ;-)

SmtpClient will send directly to the Email domain in TO if you dont specify the client.Host OR you can parse out the domain from the address and set client.Host to be that.
Left by Eric Newton on Dec 22, 2005 1:04 PM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
Here's the code I use to send mail...


public static string sendMail(string from, string to, string subject, string body)
{
string UserName = WebConfigurationManager.AppSettings["NetworkCredential_UserName"];
string Password = WebConfigurationManager.AppSettings["NetworkCredential_Password"];
string Domain = WebConfigurationManager.AppSettings["NetworkCredential_Domain"];
string Host = WebConfigurationManager.AppSettings["SMTPClient_Host"];

System.Net.Mail.MailMessage M = new System.Net.Mail.MailMessage(from, to, subject, body);
System.Net.Mail.SmtpClient SMTPClient = new System.Net.Mail.SmtpClient();
System.Net.NetworkCredential NTLMAuthentication = new System.Net.NetworkCredential(UserName, Password, Domain);
M.IsBodyHtml = true;
SMTPClient.Host = Host;
SMTPClient.UseDefaultCredentials = false;
SMTPClient.Credentials = NTLMAuthentication;

SMTPClient.Send(M);
return "";
}
Left by Brian on Dec 27, 2005 5:43 AM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
Here is the simplest solution I have, no authentication:

Dim mailer As New System.Net.Mail.SmtpClient
mailer.Host = "localhost"
mailer.Send("From", "To", "Subject", "Message")
Left by Scott Kuhl on Dec 27, 2005 10:42 AM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
I have tried all the approaches that are listed above and none of them worked. All giving me the same error which says "Unable to send email".
Unable to connect to remote server.
Left by AzamSharp on Dec 30, 2005 10:22 AM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
Dim SmtpClient As New System.Net.Mail.SmtpClient
SmtpClient.Host = “localhost”
SmtpClient.Credentials = New NetworkCredential( "username" , "password" )
SmtpClient.Send( "fromemail" ,toemail,subject,body)
Left by Anonymous on Jan 07, 2006 9:34 PM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
Are you sure your ISP lets you send? Lots block the SMTP ports unless you are sending only through their SMTP server, ie smtp.yourisp.com
Left by Anonymous on Jan 15, 2006 10:59 PM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
I have been banging my head on this all day. After 6 hours I figured out my issue. I would bet 100 bucks it's yours too. Good ole McAfee Virus scan. I needed to disable its access protection of port 25. And you need to keep going back and disabling it because it has a mind of its own.

Good luck
Left by Brian Bender on Sep 12, 2006 9:57 AM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
all you have to do is add the exe name of your application on the exemption list so that i would allow your application to pass through the port 25 while it is activated.
Left by jake andrews on Oct 17, 2006 11:44 PM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
Hi AsamSharp,

I exactly have the same problem that you got before and I have spent more than 8 hrs already trying everything suggested here.
Were you able to resolved this problem, if yes how did you do it. Please tell me, I'm desperate already.
Left by Henry on Nov 17, 2006 1:22 PM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
i like to see how the time pass by
Left by time is on my side on Jan 11, 2007 8:40 AM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
Is your SMTP Server service running? Also, you need to go into your IIS settings in Windows' Computer Management console and make sure that you have added your IP Address (127.0.0.1 works if you're using 'localhost') to the "Relay Restrictions" item on the Access tab.

Good Luck!
-Thomas
Left by Thomas Lunsford on Feb 21, 2007 3:52 PM

# re: Hey Microsoft! Can you make this any harder?
Requesting Gravatar...
haha

Yeah it was mcafees. It stops mass email worms when your client sends anonymous emails to a server.

Thx for that
Left by simion on Sep 18, 2007 8:56 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net