Question

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("you@gmail.com", "me@gmail.com", "hello" , body.Text);
                                   //example       //example

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

Here are the values :

 To.Text = "gerardcrafting@gmail.com";
        smtp.Text = "smtp.gmail.com";
        Password.Text = "Password";
        Username.Text = "staff.gerardcrafting.gmail.com"; 
        Subject.Text = textBox1.Text + "Banned" + richTextBox4.Text;
Était-ce utile?

La solution

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.

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top