Click here to Skip to main content
12,292,585 members (69,608 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET WebForms
I cont able to send a mail using this code: Please help me with this code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net.Mime;
public partial class _Default : System.Web.UI.Page
MailMessage mail = new MailMessage();
private void clear()
TextBox2.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
protected void Button2_Click(object sender, EventArgs e)
{ {
SmtpClient mailserver = new SmtpClient();
mailserver.Credentials = new System.Net.NetworkCredential ("", "*******");
// mailserver.Port = 587; //gmail port: 587
mailserver.Host = ""; // ""; // //"" "";
mailserver.EnableSsl = false;
mail = new MailMessage();
String[] addr = TextBox2.Text.Split(',');
mail.From = new MailAddress("", "HI", System.Text.Encoding.UTF8);
Byte i;
for (i = 0; i < addr.Length; i++)
mail.Subject = TextBox4.Text;
mail.Body = TextBox5.Text;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.ReplyTo = new MailAddress(TextBox2.Text);
Label1.Visible = true;
Label1.Text = "Hi Your Massage Has been sent";
catch (Exception ex)
Label1.Visible = true;
Label1.Text = "mail not sent "+ ex.ToString();
protected void Button1_Click (object sender, EventArgs e)

This is the error i am getting:

mail not sent System.Net.Mail.SmtpFailedRecipientExcep… Mailbox name not allowed. The server response was: sorry, relaying denied from your location [] (#5.7.1) at System.Net.Mail.SmtpTransport.SendMail(M… sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMess… message) at _Default.Button2_Click(Object sender, EventArgs e) in
Posted 2-Aug-11 21:55pm
Edited 2-Aug-11 22:24pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

what I see from the error is that you use a different mail server than the network you are in. So that is way it is saying that relaying is denied.

and set: mailserver.DeliveryMethod = SmtpDeliveryMethod.Network;

Which mail client do you use on your development system?
manoharnch 3-Aug-11 4:06am
digimanus 3-Aug-11 4:50am
view for all detail settings
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

The link digimanus posted works like a charm thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.160518.1 | Last Updated 24 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100