views:

119

answers:

4

Hi Experts,

I am using asp.net 3.5 and C#.

I want to send mail from asp.net, for that I have got some details from my hosting provider

which are these:

  • mail.MySite.net
  • UserName
  • Password

But I am unable to send mail through these details, I have done the following changes in my web.config file:

<system.net>
    <mailSettings>
        <smtp>
            <network
                 host="mail.MySite.net"
                 port="8080"
                 userName="UserName"
                 password="Password" />
        </smtp>
    </mailSettings>
</system.net>

Also, at the code behind I am writing this function:

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
mail.Subject = "Hi";
mail.Body = "Test Mail from ASP.NET";
mail.IsBodyHtml = false;

SmtpClient smp = new SmtpClient();
smp.Send(mail);

but I am getting error message as message sending failed.

Please let me know what I am doing wrong and what I have to do to make it work fine.

Thanks in advance.

+1  A: 

Do you need to provide the client credentials?

smp.Credentials = CredentialCache.DefaultNetworkCredentials;

or

smp.Credentials = new NetworkCredential("yourUserID", "yourPassword", "yourDomainName");

Also, the exact exception you are getting would be useful.

See a post by Scott Guthrie for more help.

Kelsey
+2  A: 

I doubt port 8080 is the correct smtp port. Perhaps port 25 or 587.

Joel Coehoorn
@Joel Coehoorn I agree, his port seems totally off. An exception would let us know if it's a can't connect error vs an unauthorized... until then it's all guessing :)
Kelsey
I will provide you the correct exception, which I believe would be helpful in finding the real cause.
Zerotoinfinite
A: 

I have very similar code to yours that works, I think the difference is you need to supply the IP address to your SMTP server in the constructor for the SMTP client.

        MailMessage Email = new MailMessage("[email protected]", "[email protected]");
        Email.Subject = "RE: Hello World.";
        Email.Body = "Hello World";
        Email.IsBodyHtml = false;
        SmtpClient Client = new SmtpClient(SMTP_SERVER); //This will be an IP address
        Client.Send(Email);

Hope that helps! :)

(Btw, I've used this in Winforms, windows services, and ASP .NET. In ASP .NET I didn't need to supply anything in the aspx page.)

Brandi
A: 

I wonder if it has anything to do with SMTP relaying being turned off? I'm just throwing out an off-the-top-of-my-head idea.

When we see the actual Exception, we can give better information.

DaleyKD