我希望我的系統在單擊按鈕時發送電子郵件,但是我每次都會在smtp.send(消息)中發現問題,沒有任何錯誤或任何內容,它只是雖然它仍然在運行,但它仍然凍結。我寫了4次嘗試,都不成功。無法使用c#中的smtp發送電子郵件#
1日嘗試:
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
MessageBox.Show("Email sent successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
第2次嘗試:
try
{
//string s = $"User {edtID} has failed to login";
MailMessage message = new MailMessage();
message.To.Add("[email protected]");
message.Subject = "Employee login failure";
message.From = new MailAddress("[email protected]");
message.Body = "Hello";
SmtpClient smtp = new SmtpClient("smtp.saix.net");
smtp.Send(message);
MessageBox.Show("mail Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SmtpClient smtp = new SmtpClient();
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.EnableSsl = true;
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress("[email protected]", "ME"));
mailMessage.Subject = "Some Subject";
mailMessage.Body = "Test";
smtp.Send(mailMessage);
第三嘗試:
// string s = $"User {edtID} has failed to login";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add("[email protected]");
mail.From = new MailAddress("[email protected]");
mail.Subject = "Employee login failure";
mail.IsBodyHtml = true;
mail.Body = "Hello";
SmtpServer.Host = "smtp.saix.net";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show("Exception Message: " + ex.Message);
if (ex.InnerException != null)
MessageBox.Show("Exception Inner: " + ex.InnerException);
}
第四嘗試:
var sysLogin = "[email protected]";
var sysPass = "[email protected]";
var sysAddress = new MailAddress(sysLogin, "Message from me!");
var receiverAddress = new MailAddress("mymail#gmail.com");
var smtp = new SmtpClient
{
Host = "smtp.gmail.com", //gmail example
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(sysLogin, sysPass)
};
using (var message = new MailMessage(sysAddress, receiverAddress) { Subject = "Some subject", Body = "Some text" })
{
smtp.Send(message);
}
MessageBox.Show("Success");
如果你的代碼全部被註釋掉了,你如何期待它做任何事情?如果您使用'SmtpClient',如何「使用SQL發送電子郵件」?你爲什麼要將'SmtpClient'的實例稱爲'SmtpServer'?這似乎倒退了。 – mason
它的全部註釋,因爲我是單獨嘗試每個人,它貫穿一切,但在smtp.Send(消息)失敗; – Armee
查看以下網址:https://support.google.com/accounts/answer/185833?hl = zh_CN https://support.google.com/accounts/answer/6010255?hl = zh_CN –