Gerard2202
Gerard2202

Reputation: 151

c# Sending email though smtp mail format error happens

Hello I am using visual c# 2010 and I am trying to send an email with gmail but I get an exception when I try to send the email at:

MailMessage mail = new MailMessage(From.Text, To.Text, Subject.Text , richTextBox2.Text);

From.Text, To.Text, Subject.Text, richTextBox2.Text are all text boxes with the information inside.

Here is the whole mail segment I am using:

SmtpClient client = new SmtpClient(smtp.Text);
            client.Port = 587;
            client.Credentials = new System.Net.NetworkCredential(Username.Text, Password.Text);
            client.EnableSsl = true;
            client.Send(mail);
            MessageBox.Show("mail sent");

The exception is: FormatException was Unhanded

The exception description is :The specified string is not in the form required for an e-mail address

And I have tried just filling the information out like so :

MailMessage mail = new MailMessage("[email protected]", "[email protected]", "hello" , body.Text);
                                   //example       //example

But I still get an exception. What am I doing wrong ?

Here are the values :

 To.Text = "[email protected]";
        smtp.Text = "smtp.gmail.com";
        Password.Text = "Password";
        Username.Text = "staff.gerardcrafting.gmail.com"; 
        Subject.Text = textBox1.Text + "Banned" + richTextBox4.Text;

Upvotes: 0

Views: 1337

Answers (2)

Karl Anderson
Karl Anderson

Reputation: 34844

Per the MSDN documentation for MailMessage Constructor (String, String, String, String), when a FormatException happens with this constructor the reason is:

from or to is malformed.

You stated that your From.Text is taff.gerardcrafting.gmail.com, that is not a valid email address, because it is missing the @ symbol.

Upvotes: 1

matth
matth

Reputation: 6299

Change the input of Username.Text to simply "gerardcrafting".

Upvotes: 1

Related Questions