我正在尝试使用 C# 框架创建一个小应用程序来发送电子邮件。但是,它不起作用。该应用程序总是给我“操作超时”。我不知道为什么。
这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
MailAddress fromAddress = new MailAddress("from@gmail.com");
MailAddress toAddress = new MailAddress("to@gmail.com");
MailMessage mail = new MailMessage(fromAddress.Address, toAddress.Address);
mail.Subject = "Testing";
mail.Body = "contents.";
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password");
try
{
client.Send(mail);
MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
}
}
谢谢!