2012-01-04 47 views
0

我試圖按照問題中所述編寫這種類型的程序。我失敗了,在msdn或google沒有運氣之後,我正在問StackOverflow的智慧。我的代碼低於那些有興趣閱讀它的人。如果url關閉,發送電子郵件通知的程序

我希望這個程序在執行時能夠讀取一個URL,看它是否處於活動狀態並正常工作。如果不是,並且我的回覆不好,則會發送電子郵件通知某人該網站已關閉。

我寫這是visual studio 2010與C#和3.5net。該程序正在從我的SQL Server 2008數據庫中讀取信息(URL和電子郵件地址),數據庫將根據基於HttpResponse讀取的站點信息(OK或NOTOK)進行更新。如果網站是NOTOK,那麼會發送一封電子郵件。我正在使用XOUlitities.Email的直接鏈接庫。

主要問題是,它不工作,我不知道爲什麼。它出去讀取網站並返回,但我沒有收到任何電子郵件。 我的問題是,電子郵件功能有一個更簡單的方法嗎?我可以在不使用XOUtilities dll的情況下在程序中寫入整個命令嗎?我基本上在尋找建議。當我運行.exe時,沒有錯誤,但我相信問題可能會在電子郵件功能中出現。如果任何人都可以在這個問題上發現任何亮點,那就太好了。先謝謝你!

using System; 
using System.Collections.Generic; 
using System.Text; 
using XOUtilities; 
using System.Web; 
using System.Net; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.IO; 

namespace WebsiteStatusCheck 
{ 
class Program 
{ 
    static string errorMsg; 

    static void Main(string[] args) 
    { 
     string connectionString = ConfigurationManager.AppSettings["ConnectionString"]; 
     string tableName = ConfigurationManager.AppSettings["WebsiteListTableName"]; 
     string mailServer = ConfigurationManager.AppSettings["MailServer"]; 
     string replyToEmail = ConfigurationManager.AppSettings["ReplyToEmail"]; 

     string query = "SELECT * FROM " + tableName; 
     SqlDataAdapter sDA = new SqlDataAdapter(query, connectionString); 
     DataTable table = new DataTable(); 
     sDA.Fill(table); 
     string[][] websites = new string[table.Rows.Count][]; 
     int i = 0; 
     table.Columns.Add("isSiteAlive"); 
     table.Columns.Add("isDBAlive"); 
     foreach (DataRow row in table.Rows) 
     { 
      string[] temp = CheckURL(row["URL"].ToString()); 
      row["isSiteAlive"] = temp[0]; 
      row["isDBAlive"] = temp[1]; 
     } 

     XOUtilities.Email email = new XOUtilities.Email(); 
     email.fromAddress = replyToEmail; 
     email.server = mailServer; 
     email.subject = "Website needs IMMEDIATE action"; 
     email.isHtml = true; 
     email.body = @"The following website looks to be down:<br /><br /><table><tr><th>URL</th><th>Website</th><th>Database</th>"; 
     foreach(DataRow row in table.Rows) 
     { 
      if (row["isSiteAlive"].ToString().Trim() != "OK" || row["isDBAlive"].ToString().Trim() != "OK") 
      { 
       string tempbody = email.body; 
       email.body += @"<tr><td><center>" + row["URL"].ToString() + @"</center></td><td><center>" + row["isSiteAlive"].ToString() + @"</center></td><td><center>" + row["isDBAlive"].ToString() + @"</center></td></tr>"; 
       email.toAddresses = row["EMAILS_CSV"].ToString().Split(new char[] { ',' }); 
       email.SendEmail(); 
       email.body = tempbody; 
      } 
     } 
    } 

    //string[0] = website value 
    //string[1] = database value 
    static string[] CheckURL(string url) 
    { 
     string[] ret = new string[2]; 
     try 
     { 
      WebClient client = new WebClient(); 
      Stream resp = client.OpenRead(url); 
      StreamReader reader = new StreamReader(resp); 
      string result = reader.ReadToEnd(); 
      ret[0] = "OK"; 
      ret[1] = result; 
     } 
     catch (WebException e) 
     { 
      errorMsg = e.Status.ToString(); 
      if (e.Status == WebExceptionStatus.ProtocolError) 
      { 
       errorMsg = ((HttpWebResponse)e.Response).StatusDescription; 
      } 
      ret[0] = errorMsg; 
      ret[1] = "unreachable"; 
     } 
     catch (Exception e) 
     { 
      errorMsg = e.Message; 
      ret[0] = errorMsg; 
      ret[1] = "unreachable"; 
     } 
     return ret; 
    } 
} 

}

+2

到目前爲止,您在調試方面有哪些嘗試?你在調試器中浸泡過代碼嗎?如果是的話,直到你確信一切似乎正在工作?調試器控制檯窗口中是否有任何錯誤消息? – dgvid 2012-01-04 21:12:59

+1

您沒有使用['SmtpClient'](http://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient%28v=VS.90%29.aspx)用於電子郵件的任何原因部分? – Yahia 2012-01-04 21:13:22

+0

是這個庫發送電子郵件Java庫或.NET網絡庫..也在你的C#代碼中,你正在使用操作符重載的字符串..我建議使用StringBuilder來構建複雜的字符串..電子郵件的SMTP將工作多更好地在我看來什麼版本的.Net你使用btw – MethodMan 2012-01-04 21:14:29

回答

2

我從來沒有使用XOUtilities,爲什麼不使用.NET中包含的庫?只要確保你的項目有System.Net的引用,那麼你可以做這樣的事情:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); 
message.To.Add("[email protected]"); 
message.Subject = "This is the Subject line"; 
message.From = new System.Net.Mail.MailAddress("[email protected]"); 
message.Body = "This is the message body"; 
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost"); 
smtp.Send(message); 

來源這個片段的: http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8

+0

是啊我不知道爲什麼我會嘗試從另一個.net項目包括一個庫,我應該完全按照你說的去做。 Tyvm。 – javasocute 2012-01-04 22:31:47

1

你不喜歡的System.Net.Mail命名空間和使用MailMessage?然後你使用SmtpClient併發送它(所有從你自己的.NET環境的舒適)

順便說一句,我會發布的例子,但msdn有一些很好的。

1

你可能要檢查是否有不同的狀態。例如。如果狀態是成功,則不做任何事,否則發送電子郵件。

您可以配置默認的主機,端口和地址從配置文件,然後新建立一個smtpclient發送消息

using(var email = new MailMessage {Subject = "", Body = ""}) 
{ 
    email.To.Add(recipient); 
    new SmtpClient().Send(email); 
} 

你也可能會碰到OOM取決於(內存不足)從數據庫返回的記錄數。我建議迭代數據閱讀器而不是加載數據表。

最後,你會想要處理實施IDisposable的對象。 StreamReaderSqlConnection/Command是最明顯的。

+0

Upvote用於記住IDisposable類。這是我第一次觀察。 – 2012-01-04 21:28:20

相關問題