0

I am new to C#. In my verge of learning this language, I made a windows form application application that takes in an email address and a password and sends it to a pre-specified email address. I don't think that I have messed up the data types though I am getting an error The specified string is not in the form required for an e-mail address.

My code is below :

namespace mailTest
{


public partial class Form1 : Form
{
    string mailAddress = "lancepreston@gmail.com";
    string mailPassword = "123456789";
    string SMTP = "smtp.gmail.com";

    public Form1()
    {
        InitializeComponent();
    }


    private void button_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage(Email.Text, Password.Text); \\ I get the error on this line
        SmtpClient client = new SmtpClient(SMTP);
        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential(mailAddress,mailPassword);
        client.EnableSsl = true;
        client.Send(mail);
        MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);
    }
}
}

Screenshot of my form :

enter image description here

CodeWalker
  • 2,281
  • 4
  • 23
  • 50
  • possible duplicate of http://stackoverflow.com/questions/4757001/the-specified-string-is-not-in-the-form-required-for-an-e-mail-address – Tianyun Ling Aug 01 '14 at 13:59

3 Answers3

6

MailMessage constructor is defined as below

public MailMessage(string from, string to);

First parameter is from address and second is to address, but you seem to pass password in second parameter. That's why you get the exception.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • I edited my code to `MailMessage mail = new MailMessage(emailFrom.Text,mailAddress,subject,Password.Text);` but I still get an error on `Password.Test` saying `Format Exception Unhandled` and `The specified string is not in the form required for an e-mail address.` – CodeWalker Aug 01 '14 at 17:07
  • That is also wrong, check the [documentation](http://msdn.microsoft.com/en-us/library/5k0ddab0%28v=vs.110%29.aspx) please – Sriram Sakthivel Aug 01 '14 at 17:09
1

That particular constructor of the MailMessage class expects the first parameter to be the email address of the sender, and the second parameter to be the email address of the recipient:

http://msdn.microsoft.com/en-us/library/14k9fb7t(v=vs.110).aspx

You are providing a password to the parameter that expects the recipient's email address.

I presume you really want to pass the password in the body of the message.

Take a look at the constructor that populates the body, or set the Body property after initializing a MailMessage:

http://msdn.microsoft.com/en-us/library/5k0ddab0(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.body(v=vs.110).aspx

Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
  • I edited my code to `MailMessage mail = new MailMessage(emailFrom.Text,mailAddress,subject,Password.Text);` but I still get an error on `Password.Test` saying `Format Exception Unhandled` and `The specified string is not in the form required for an e-mail address.` – CodeWalker Aug 01 '14 at 17:10
1

You have to specify UserName in NetworkCredential , like the following:

NetworkCredential myCredentials = new NetworkCredential("","","");
myCredentials.Domain = domain;
myCredentials.UserName = username;
myCredentials.Password = passwd;

See the explanation at: http://msdn.microsoft.com/en-us/library/system.net.networkcredential.username%28v=vs.110%29.aspx

Also, your MailMessage has wrong parameters (shoud be from/to). Regards,

Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
  • I edited my code to `MailMessage mail = new MailMessage(emailFrom.Text,mailAddress,subject,Password.Text);` but I still get an error on `Password.Test` saying `Format Exception Unhandled` and `The specified string is not in the form required for an e-mail address.` – CodeWalker Aug 01 '14 at 17:09