user3261588
user3261588

Reputation: 51

sending mail using asp.net

i am trying to send mails using c# in asp.net page on a free asp server. i have wrote this code

public string send_email()
{
    SmtpClient client = new SmtpClient("relay-hosting.secureserver.net", 25);
    string to = "[email protected]";
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Credentials = new System.Net.NetworkCredential("[email protected]", "XXXXXXX");

    MailAddress fromAddress = new MailAddress("[email protected]", "Mona ");
    MailMessage message = new MailMessage();
    message.From = fromAddress;
    message.To.Add(to);
    message.Body = "This is Test message";
    message.Subject = "hi";

    client.Send(message); 
    message.Dispose(); 
    return "Email Send";
}

and wrote this in web.config

<mailSettings>
  <smtp from="[email protected]">
        <network host="relay-hosting.secureserver.net" port="25" userName="[email protected]" password="XXXXX"/>
  </smtp>
  </mailSettings>

but i have an error called

Mailbox name not allowed. The server response was: sorry, relaying denied from your location [XX.XXX.XX.XXX] (#5.7.1)

any help

Upvotes: 1

Views: 120

Answers (3)

Emanuele Greco
Emanuele Greco

Reputation: 12721

1) If you use web.config <mailsettings> configuration (and this is right!) you should not set SmtpClient programmatically, or configuration will be useless!

 SmtpClient client = new SmtpClient();  
 MailMessage message = new MailMessage();  
 message.To.Add(to);  
 message.Body = "This is Test message";  
 message.Subject = "hi";`    

this is enough, no server o from in code.

2) As Max's saying, this is not a code problem: this is a configuration problem! Try a server-port-user-password configuration using your mail client (outlook, thunderbird or any other) and if this works you just have to copy it in tag.

Upvotes: 0

Joel
Joel

Reputation: 161

Make it so that you can allow anonymous emailing from your server where the page resides then you can set:

    client.UseDefaultCredentials = false; 

and

    message.From = "[email protected]";

and like Max Al Farakh said, use the provided SMTP server which you have rights to.

Upvotes: 0

Max Al Farakh
Max Al Farakh

Reputation: 4476

It's a security error from your SMTP server, not your code's fault. Probably because you use your Gmail credentials to access a non-gmail SMTP server.

Upvotes: 2

Related Questions