字符串与电子邮件地址不兼容

Cod*_*ker 0 c# email winforms

我是C#的新手.在我学习这门语言的过程中,我创建了一个Windows窗体应用程序,它接收一个电子邮件地址和一个密码并将其发送到预先指定的电子邮件地址.虽然我收到错误,但我认为我搞砸了数据类型The specified string is not in the form required for an e-mail address.

我的代码如下:

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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我的表格截图:

在此输入图像描述

Sri*_*vel 6

MailMessage构造函数定义如下

public MailMessage(string from, string to);
Run Code Online (Sandbox Code Playgroud)

第一个参数来自地址,第二个参数是地址,但您似乎在第二个参数中传递密码.这就是你得到例外的原因.