我有c#代碼發送郵件。該代碼工作正常的Gmail,雅虎配置。但是對於hotmail,有時會發送郵件,有時郵件不會發送。我觀察到,與Gmail相比,雅虎郵件的響應時間更好。但與Yahoo,Gmail相比,hotmail的響應非常差。從hotmail發送郵件失敗有時使用c#
會是什麼原因?如何配置hotmail以獲得良好的響應。
這是我的代碼來發送郵件:
private void button2_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
message.To.Add("user_name");
message.From = new MailAddress("user_name");
message.Subject = "Email Confirmation";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.SubjectEncoding = System.Text.Encoding.UTF8;
client.Port=587
client.Host="smtp.live.com";
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("user_name", "password");
if (mail_server == "smtp.gmail.com" || mail_server == "smtp.live.com")
{
client.EnableSsl = true;
}
client.UseDefaultCredentials = false;
client.Credentials = nc;
Ping p = new Ping();
bool result = false;
int i = 10;
do
{
PingReply reply = p.Send("smtp.live.com", 587);
if (reply.Status == IPStatus.Success)
{
result = true;
} Thread.Sleep(500);
i--;
} while (result == false && i != 0);
if (result == true)
{
client.Send(message);
MessageBox.Show("Email sent successfully");
}
else
{
MessageBox.Show("Email not sent");
}
}
可笑是不是?兩種微軟技術不能很好地協作! – 2013-02-26 13:18:54
失敗時會得到什麼錯誤? – 2013-02-26 13:19:00
爲什麼'Ping' ????? – 2013-02-26 13:25:54