Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have used this tutorlal Send Mail / Contact Form using ASP.NET and C#[^] but I get a error:

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.
HTML
Source Error: 

Line 41:         }
Line 42:         // Passing values to smtp object
Line 43:         smtp.Send(fromAddress, toAddress, subject, body);
Line 44:     }
Line 45:     protected void Button_send_Click(object sender, EventArgs e)

This is how my backend code looks like, what have I been doing wrong?
C#
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class kontakt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void SendMail()
    {
        // Gmail Address from where you send the mail
        var fromAddress = "myEmail";
        // any address where the email will be sending
        var toAddress = TextBox_navn.Text.ToString();
        //Password of your gmail address
        const string fromPassword = "password";
        // Passing the values and make a email formate to display
        string subject = TextBox_emne.Text.ToString();
        string body = "From: " + TextBox_navn.Text + "\n";
        body += "Email: " + TextBox_email.Text + "\n";
        body += "Subject: " + TextBox_emne.Text + "\n";
        body += "Question: \n" + TextBox_besked.Text + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }
    protected void Button_send_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "INSERT INTO kontakt (navn, emne, email, besked) VALUES(@navn, @emne, @email, @besked)";

        cmd.Parameters.Add("@navn", SqlDbType.VarChar).Value = TextBox_navn.Text;
        cmd.Parameters.Add("@emne", SqlDbType.Text).Value = TextBox_emne.Text;
        cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = TextBox_email.Text;
        cmd.Parameters.Add("@besked", SqlDbType.Text).Value = TextBox_besked.Text;



        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

        SendMail();
        TextBox_navn.Text = "";
        TextBox_emne.Text = "";
        TextBox_email.Text = "";
        TextBox_besked.Text = "";
        Label1.Text = "Din besked er sendt";


          
    }
}


Hope someone could help me with this

Tina
Posted
Updated 28-May-13 2:51am
v3
Comments
What are you passing in toAddress ?
Rockstar_ 29-May-13 1:22am    
Put email manually in the from and to address then check, where the actual error is?

For SMTP, the following format to declare the message works fine for me:
C#
MailMessage mail = new MailMessage();
// add message 'from' field
mail.From = new MailAddress("tinaw25@gmail.com");
// add recipient 'to' field
mail.To.Add("some@address.com");
// append the message subject
mail.Subject = "my mail subject";
// append the message body
mail.Body = "my message body";

smtp.Send(mail);
 
Share this answer
 
Hi, First of all you used code in asp.net page but whenever you take reference,it used control not asp.net page

your reference code are working fine..i check it out..

Thanks

Happy coding...
 
Share this answer
 
Comments
tina_overgaard 28-May-13 8:41am    
It is not working, when I click the send button it comes with an error
bbirajdar 28-May-13 8:54am    
because you are not using a valid email address
renish patel 28-May-13 8:53am    
can you please explain What are you passing in toAddress ?
tina_overgaard 28-May-13 9:09am    
var fromAddress = "myEmail"; here I have my gmail

var toAddress = TextBox_navn.Text.ToString(); How do I insert my email in here??

const string fromPassword = "password"; here i have my password

[Reply by @Renish Patel]

what is the value of TextBox_navn.Text which are you entering at runtime? I means in webform..
Hi...
see this link,may its useful to u.

How I can track Email send or Not Through Asp .Net?[^]

thank u.
 
Share this answer
 
Hi You forget to add IsMailBodyHrml = true as your want to send an HTML body.

Set this and hope it will run.

C#
smtp.IsMailBodyHrml=true; 
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900