2013-02-26 65 views
0

我有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"); 

       } 
} 
+0

可笑是不是?兩種微軟技術不能很好地協作! – 2013-02-26 13:18:54

+1

失敗時會得到什麼錯誤? – 2013-02-26 13:19:00

+0

爲什麼'Ping' ????? – 2013-02-26 13:25:54

回答

0

如果你想在上面的代碼發送電子郵件,它顯然不能夠把它,我不知道你用的是什麼原因Ping類發送電子郵件。

固定碼:

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 = Encoding.UTF8; 
    message.SubjectEncoding = Encoding.UTF8; 

    client.Port = 587; 
    client.Host = "smtp.live.com"; 

    NetworkCredential nc = new NetworkCredential("user_name", "password"); 
    string mail_server = "smtp.live.com"; 
    if (mail_server == "smtp.gmail.com" || mail_server == "smtp.live.com") 
     { 
      client.EnableSsl = true; 
     } 
     client.UseDefaultCredentials = false; 
     client.Credentials = nc; 

    try 
    { 
     client.Send(message); 
    } 
    catch (SmtpException exception) 
    { 
     Console.WriteLine(exception.Message); 
    } 
+0

此代碼工作正常......但如果使用ping,我有時不會從hotmail服務器獲得響應。 ping也用於檢查用戶名,密碼是否正確發送郵件。我必須驗證正確的郵件帳戶並顯示「用戶帳戶無效」警報。 – 2013-02-27 06:07:30

+0

爲我的問題投票..如果問題是有效的:-) – 2013-02-27 06:09:58