locked
Getting error when trying to send email RRS feed

  • Question

  • User-718146471 posted

    Hey guys, I'm getting an error that simply makes no sense to me so I was hoping a second pair of eyes might help.  I have this sending code that I am using for sending email and I get the most bizarre error message based on the line it is happening at.

    Error code:

    The specified string is not in  the form required for an e-mail 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.FormatException: The specified string is not in the form required for an e-mail 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:
    [FormatException: The specified string is not in the form required for an e-mail address.]
    System.Net.Mail.MailAddressParser.ReadCfwsAndThrowIfIncomplete(String data, Int32 index) +1471097
    System.Net.Mail.MailAddressParser.ParseDomain(String data, Int32& index) +135 System.Net.Mail.MailAddressParser.ParseAddress(String
    data, Boolean expectMultipleAddresses, Int32& index) +99
    System.Net.Mail.MailAddressParser.ParseAddress(String data) +23
    System.Net.Mail.MailAddress..ctor(String address, String displayName,
    Encoding displayNameEncoding) +220 System.Net.Mail.MailMessage..ctor()
    +130 BCCS.Contact.txtSubmit_Click(Object sender, EventArgs e) in
    c:UsersDanielDesktopBCCSContact.aspx.cs:40
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9752490
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String
    eventArgument) +196
    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
    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:4.0.30319;
    ASP.NET Version:4.0.30319.18449

    code behind:

            protected void txtSubmit_Click(object sender, EventArgs e)
            {
                var fromAddress = txtEmail.Text.ToString();  // this is the line it occurs on
                var toAddress = ddlDepartment.SelectedValue.ToString();
                string subject = "Contacting: " + ddlDepartment.SelectedItem.ToString();
                string body = "An email message from " + txtName.Text.ToString() + " and they wrote the following: <br /><br />" + txtBody.Text;
                try
                {
                    if (this.CodeNumberTextBox.Text == this.Session["CaptchaImageText"].ToString())
                    {
                        this.MessageLabel.Text = "";
                        var smtp = new System.Net.Mail.SmtpClient();
                        smtp.Send(fromAddress, toAddress, subject, body);
                        lblFeedbackOK.Visible = true;
    
                        if (ddlDepartment.SelectedValue.ToString() == "sales@domain.com")
                        {
                            smtp.Send("Alerts@bellacasarocks.com", "4102418191@vtext.com", "", "RFQ from " + txtName.Text.ToString() + " with email address of " + txtEmail.Text.ToString() + " is waiting in your inbox!");
                        }
                    }
                    else
                    {
                        // Display an error message.
                        this.MessageLabel.Text = "ERROR: Incorrect, try again.";
                        // Clear the input and create a new random code.
                        this.CodeNumberTextBox.Text = "";
                        lblFeedbackOK.Visible = false;
                        this.Session["CaptchaImageText"] = GenerateRandomCode();
                    }
    
                }
                catch (Exception ex)
                {
                    lblFeedbackKO.Visible = true;
                    lblFeedbackKO.Text = lblFeedbackKO.Text + " Error details: " + ex.ToString();
                }
            }
    

    Wednesday, August 6, 2014 9:26 AM

Answers

  • User-821857111 posted

    Have you used the debugger to examine the fromAddress and toAddress values? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 9:57 AM
  • User-821857111 posted

    Have you got a malformed email address specified in a <mailsettings> section in your web.config perhaps?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 10:52 AM
  • User-821857111 posted

    <smtp from="domain.com">

    That's not a valid email address.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 11:05 AM
  • User475983607 posted

    The message states an email address is invalid.  So either the email address format is incorrect or the email address used in the FROM field is not from the Arvixe domain.  SMTP servers may kick out email address when the from email address is not within the expected domain.  This error can also happen if the domain is correct but the email address is nonexistent.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 1:39 PM

All replies

  • User-821857111 posted

    Have you used the debugger to examine the fromAddress and toAddress values? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 9:57 AM
  • User-718146471 posted

    Yes and they come up perfectly fine.  That is why it is so weird.

    Wednesday, August 6, 2014 10:24 AM
  • User-821857111 posted

    Have you got a malformed email address specified in a <mailsettings> section in your web.config perhaps?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 10:52 AM
  • User-718146471 posted

    I don't think I do:

      <system.net>
        <mailSettings>
          <smtp from="domain.com">
            <network host="mail.domain.com" port="25" userName="mailservice@domain.com" password="**************" />
          </smtp>
        </mailSettings>
      </system.net>
    
    Wednesday, August 6, 2014 10:59 AM
  • User1428246847 posted

    Sorry to be a bit critical, but you say that 'fromAddress' is perfectly OK. But also, in the opening post, you say that the first line of the method (where 'fromAddress' gets its value) throws the error. So 'fromAddress' can never be perfectly fine.

    What am I missing?

    Wednesday, August 6, 2014 11:00 AM
  • User-821857111 posted

    <smtp from="domain.com">

    That's not a valid email address.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 11:05 AM
  • User-718146471 posted

    Oh snap, ok. Let me try that...  I knew it was something goofy.

    Wednesday, August 6, 2014 11:06 AM
  • User-718146471 posted

    Must have been looking at the code for too long, tired eyes.  Thanks guys!

    Wednesday, August 6, 2014 11:41 AM
  • User-718146471 posted

    Oh this is so annoying.  Take a look at this stuff now:

    Sorry, there was a problem sending your message. Error details: System.Net.Mail.SmtpFailedRecipientException: 
    Mailbox unavailable. The server response was: A valid address is required. at System.Net.Mail.SmtpTransport.SendMail
    (MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
    at System.Net.Mail.SmtpClient.Send(MailMessage message) at BCCS.Contact.txtSubmit_Click(Object sender, EventArgs e) in
    Contact.aspx.cs:line 52


    code:

    using System;
    using System.Net;
    using System.Net.Mail;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace BCCS
    {    
        public partial class Contact : System.Web.UI.Page
        {
            private Random random = new Random();
    
            protected void Page_Load(object sender, EventArgs e)
            {
                Panel pnlFooter = Master.FindControl("pnlFooter") as Panel;
                if (pnlFooter != null)
                {
                    pnlFooter.Visible = false;
                }
                
                if (!this.IsPostBack)
                {
                    // Create a random code and store it in the Session object.
                    this.Session["CaptchaImageText"] = GenerateRandomCode();
    
                }
            }
    
            //
            // Returns a string of six random digits.
            //
            private string GenerateRandomCode()
            {
                string s = "";
                for (int i = 0; i < 6; i++)
                    s = String.Concat(s, this.random.Next(10).ToString());
                return s;
            }
            protected void txtSubmit_Click(object sender, EventArgs e)
            {
                try
                {
                    if (this.CodeNumberTextBox.Text == this.Session["CaptchaImageText"].ToString())
                    {
                        SendMail();
                    }
                    else
                    {
                        // Display an error message.
                        this.MessageLabel.Text = "ERROR: Incorrect, try again.";
                        // Clear the input and create a new random code.
                        this.CodeNumberTextBox.Text = "";
                        lblFeedbackOK.Visible = false;
                        this.Session["CaptchaImageText"] = GenerateRandomCode();
                    }
    
                }
                catch (Exception ex)
                {
                    lblFeedbackKO.Visible = true;
                    lblFeedbackKO.Text = lblFeedbackKO.Text + " Error details: " + ex.ToString();
                }
            }
            protected void SendMail()
            {
                // Address from where you send the mail
                var fromAddress = "mailuser@domain.com";
                // any address where the email will be sending
                var toAddress = "user@user.com";
                //Password of your gmail address
                const string fromPassword = "**************";
                // Passing the values and make a email format to display
                string subject = "Contacting: " + ddlDepartment.SelectedItem.ToString() + "";
                string body = "An email message from " + txtName.Text.ToString() + " and they wrote the following: <br /><br />" + 
    txtBody.Text.ToString(); // smtp settings var smtp = new System.Net.Mail.SmtpClient(); { smtp.Host = "mail.domain.com"; smtp.Port = 465; smtp.EnableSsl = false; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); smtp.Timeout = 20000; } // Passing values to smtp object smtp.Send("mailservice@domain.com", "mailservice@domain.com", subject, body); } } }

    Wednesday, August 6, 2014 12:52 PM
  • User-718146471 posted

    For what it is worth, I am using Arvixe for my hosting.

    Wednesday, August 6, 2014 1:28 PM
  • User475983607 posted

    The message states an email address is invalid.  So either the email address format is incorrect or the email address used in the FROM field is not from the Arvixe domain.  SMTP servers may kick out email address when the from email address is not within the expected domain.  This error can also happen if the domain is correct but the email address is nonexistent.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 1:39 PM
  • User-718146471 posted

    It was the toAddress, the DB somehow had a truncated value for the email address, which was webmaster@domain so I fixed that and now it is working.  Thanks all for helping me through this.

    Wednesday, August 6, 2014 1:43 PM