如果网址已关闭,则发送电子邮件通知的程序 [英] Program that sends an email notification if url is down

查看:159
本文介绍了如果网址已关闭,则发送电子邮件通知的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照问题写的这种类型的程序。我失败了,在msdn或谷歌没有运气后,我问StackOverflow的智慧头脑。对于那些有兴趣阅读的人,我的代码如下。

I am attempting to write this type of program as stated in the question. I have failed miserably and after no luck on msdn or google I am asking the intelligent minds of StackOverflow. My code is below for those interested in reading it.

我希望这个程序在执行时读取一个url,看看它是否处于活动状态并正常工作。如果不是,我收到一个不好的回复,发送电子邮件通知某人网站已关闭。

I would like this program, on execution, to read a url to see if it is active and working properly. If it is not, and I get a bad response, an email is sent notifying someone that the website is down.

我写这个是Visual Studio 2010与C#和3.5net。该程序正在从SQL Server 2008的数据库中读取信息(URLS和电子邮件地址),数据库将根据HttpResponse(OK或NOTOK)的站点读取信息进行更新。如果网站是NOTOK,那么会发送一封电子邮件。我正在使用XOUlitities.Email的直接链接库。

Im writing this is visual studio 2010 with C# and 3.5net. The program is reading the information (URLS and Email Addresses) from my Database from SQL Server 2008, the database will update with information based upon the sites reading per the HttpResponse (OK or NOTOK). If the website is NOTOK, then an email is sent. I am using a direct link library for the XOUlitities.Email.

主要的问题是,它不起作用,我不知道为什么。它出来读取网站并回来,但我没有收到电子邮件。
我的问题是,电子邮件功能有更简单的方法吗?我可以在程序中写整个命令,而不使用XOUtilities dll吗?我基本上是在寻找建议。当我运行.exe时,没有错误,但我相信问题可能在电子邮件功能里面。如果任何人在这个问题上可以看清楚,那将是巨大的。谢谢你提前!

The main issue is, it does not work and I have no clue why. It goes out and reads the website and comes back, but I receive no email. My question is, is there an easier way for the email function? Can I just write the entire command inside the program without using the XOUtilities dll? I am basically looking for advice. When I run the .exe, there are no errors, but I believe the problem may lye within the email function. If anyone can shed any light on this issue, that would be great. Thank you in advance!

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;
    }
}

}

推荐答案

我从来没有使用XOUtilities,为什么不使用.NET中包含的库?只要确保你的项目有一个System.Net参考,然后你可以这样做:

I've never used XOUtilities, why not just use the libraries included in .NET? Just make sure your project has a reference to System.Net and then you can do something like this:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("luckyperson@online.microsoft.com");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com");
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

这篇关于如果网址已关闭,则发送电子邮件通知的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆