Sending email from asp.net not working

Discussion in 'Email' started by Clarence, May 30, 2015.

  1. I have a contact up page on my website done in asp.net and the page isn't working. I used code that I've used on other sites that I've done but for some reason it doesn't work on Winhost. I found some code in Winhost's forums but that isn't working either. The error I keep getting is "The parameter 'address' cannot be an empty string". Here is the code from the button click event in my code behind and the error message I keep getting.
    Can someone help me figure out what's wrong? Thanks!!!

    Code:

    protected void btnSend_Click(object sender, EventArgs e)
    {
    if(MyCaptcha1.IsValid)
    {
    string sBody = string.Format("Reply to: {0} <br/> {1} <br/><br/> {2}", txtName.Text, txtEmail.Text, txtMessage.Text);

    //Build the mail object
    //MailMessage message = new MailMessage("[email protected]", "[email protected]", txtSubject.Text, sBody);
    MailMessage message = new MailMessage();
    message.From = new MailAddress("[email protected]");
    message.To.Add("[email protected]");

    message.Subject = txtSubject.Text;
    message.Body = sBody;

    message.IsBodyHtml = true;

    SmtpClient client = new SmtpClient("mail.backgroundsound.biz");
    client.Credentials = new System.Net.NetworkCredential("username", "password");

    //Send email
    client.Send(message);​
    }
    else
    {
    //Display error message
    ClientScript.RegisterClientScriptBlock(typeof(Page), "ValidateMsg", "<script>alert('You entered INCORRECT CAPTCHA Code!');</script>");​
    }​
    }

    Error:

    The parameter 'address' cannot be an empty string.
    Parameter name: address

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: The parameter 'address' cannot be an empty string.
    Parameter name: address

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [ArgumentException: The parameter 'address' cannot be an empty string.
    Parameter name: address]
    System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) +1561969
    System.Net.Mail.MailAddress..ctor(String address, String displayName) +10
    perrysanders.ContactUs.btnSend_Click(Object sender, EventArgs e) in ContactUs.aspx.cs:30
    System.EventHandler.Invoke(Object sender, EventArgs e) +0
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628722
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
     
  2. Elshadriel

    Elshadriel Winhost Staff

    Try outputting the collection for message.To. Most posts I see indicate it's a problem with that.
     
  3. I have tried outputting the collection for the message.To and From, but it goes directly to the error message when I click the send button. I've tried even commenting out a little code at a time until the error disappears but the error still occurs even after commenting out the entire button click event body. This is really awkward.
     
  4. The ContactUs page on my website will not send emails. I keep getting the error stating that the parameter address cannot be an empty string. The parameter is there in the code for both the from and to attributes. I have tried to output the values of the from attribute and to collection of the MailMessage, but when I click on the Send button, it still displays the error. I commented out the code bit by bit until the entire method was commented out and the error message still displayed every time. This is code I've used before and I even used code that I found in the forum to send emails from your website, which wasn't much different from mine and it still doesn't work. Can someone help me out with this? Has anyone else had this problem with sending email from your website on Winhost? Below is the code from my code behind.

    protected void btnSend_Click(object sender, EventArgs e)
    {
    if(MyCaptcha1.IsValid)
    {
    MailMessage message = new MailMessage();

    string sBody = string.Format("Reply to: {0} <br/> {1} <br/><br/> {2}", txtName.Text, txtEmail.Text, txtMessage.Text);

    //Build the mail object
    //MailMessage message = new MailMessage("[email protected]", "[email protected]", txtSubject.Text, sBody);
    message.From = new MailAddress("[email protected]");
    message.To.Add("[email protected]");

    message.Subject = txtSubject.Text;
    message.Body = sBody;

    message.IsBodyHtml = true;

    SmtpClient client = new SmtpClient("mail.backgroundsound.biz");
    client.EnableSsl = false;
    client.Credentials = new System.Net.NetworkCredential("[email protected]", "<password>");

    //Send email
    client.Send(message);​
    }
    else
    {
    //Display error message
    ClientScript.RegisterClientScriptBlock(typeof(Page), "ValidateMsg", "<script>alert('You entered INCORRECT CAPTCHA Code!');</script>");​
    }​
    }
     
    Last edited by a moderator: Oct 14, 2015

Share This Page