昨天,我的上级分配了一项任务,要在 .net 中构建一个 Windows 窗体应用程序,它看起来就像我附上的图像。我做了有关电子邮件应用程序发送过程的所有工作,但我卡在一个地方,我无法弄清楚如何验证电子邮件表单中的密码。密码必须是在“发件人:”字段中提供的同一电子邮件。
这是我的表格背后的代码,
public partial class Form1 : Form
{
MailMessage message;
SmtpClient smtp;
public Form1()
{
InitializeComponent();
lbl_Error.Visible = false;
}
private void chk_Show_Password_CheckedChanged(object sender, EventArgs e)
{
if (chk_Show_Password.Checked == true)
txt_Password.PasswordChar= '\0';
else
txt_Password.PasswordChar='*';
}
private void btn_Send_Click(object sender, EventArgs e)
{
btn_Send.Enabled = false;
txt_Password.Text = "";
try
{
message = new MailMessage();
if(isValidEmail(txt_From.Text))
{
message.From = new MailAddress(txt_From.Text);
}
if (isValidEmail(txt_To.Text))
{
message.To.Add(txt_To.Text);
}
message.Body = txt_Details.Text;
//attributes for smtp
smtp = new SmtpClient("smtp.gmail.com");
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("imad.majid90@gmail.com", "mypassword");
smtp.Send(message);
}
catch(Exception ex)
{
btn_Send.Enabled = true;
MessageBox.Show(ex.Message);
}
}
public bool isValidEmail(string email)
{
bool flagFalse = false; ;
if (!email.Contains('@'))
{
lbl_Error.Visible = true;
lbl_Error.ForeColor = System.Drawing.Color.Red;
lbl_Error.Text = "Email address must contain @";
return flagFalse;
}
return true;
}
}