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.
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?
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()
{
var fromAddress = "myEmail";
var toAddress = TextBox_navn.Text.ToString();
const string fromPassword = "password";
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";
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;
}
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