rt..........
我把源码帖出来!!!!
global.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Timers;
using System.Threading;
namespace SendMailService 
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
Thread objth ;
public Global()
{
InitializeComponent();
} static public string test;

EmailSendService mail;
protected void Application_Start(Object sender, EventArgs e)
{
SqlConnection objConn = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
SqlDataAdapter roleDA = new SqlDataAdapter("select * from role", objConn);
SqlDataAdapter orderDA = new SqlDataAdapter("select * from connect", objConn);
objConn.Open(); DataSet ds = new DataSet("EmailSendRole");
roleDA.Fill(ds, "roles");
orderDA.Fill(ds, "configure");
objConn.Close(); DataRelation relation = ds.Relations.Add("rela",
ds.Tables["roles"].Columns["ID"],
ds.Tables["configure"].Columns["roleID"]);
relation.Nested = true;
ds.Tables["roles"].Columns["id"].ColumnMapping=MappingType.Hidden;
ds.Tables["roles"].Columns["role"].ColumnMapping =MappingType.Attribute;
//ds.Tables["roles"].Columns["From"].ColumnMapping = MappingType.Attribute;
ds.Tables["configure"].Columns["id"].ColumnMapping=MappingType.Hidden;
ds.Tables["configure"].Columns["roleID"].ColumnMapping = MappingType.Hidden;
ds.Tables["configure"].Columns["emailaddr"].ColumnMapping=MappingType.Attribute;
ds.WriteXml(Server.MapPath("roles.xml"));


mail = new EmailSendService();
    objth = new Thread(new ThreadStart(mail.send));
//objth.IsBackground = true;
objth.Start();
Thread.Sleep(1000);

}

protected void Session_Start(Object sender, EventArgs e)
{ } protected void Application_BeginRequest(Object sender, EventArgs e)
{ } protected void Application_EndRequest(Object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{ } protected void Application_Error(Object sender, EventArgs e)
{ } protected void Session_End(Object sender, EventArgs e)
{ } protected void Application_End(Object sender, EventArgs e)
{
if(null != objth)
objth.Abort();
}

#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.components = new System.ComponentModel.Container();
}
#endregion
}
}

解决方案 »

  1.   

    SendMailService.csusing System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    using System.Data.SqlClient;
    using System.Xml;
    using System.Web.Mail;
    using System.Threading;
    namespace SendMailService
    {
    /// <summary>
    /// EmailSendService 的摘要说明。
    /// </summary>
    [WebService(Namespace="Nox",Description="在Web Services里发送邮件。")]
    public class EmailSendService : System.Web.Services.WebService
    {
    public EmailSendService()
    {
    //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
    InitializeComponent();
    } #region 组件设计器生成的代码

    //Web 服务设计器所必需的
    private IContainer components = null;

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if(disposing && components != null)
    {
    components.Dispose();
    }
    base.Dispose(disposing);
    }

    #endregion // WEB 服务示例
    // HelloWorld() 示例服务返回字符串 Hello World
    // 若要生成,请取消注释下列行,然后保存并生成项目
    // 若要测试此 Web 服务,请按 F5 键// [WebMethod]
    // public string HelloWorld()
    // {
    // return "Hello World";
    // }

    public void send()
    {
    Thread obj = new Thread(new ThreadStart(SendMail));
    while(true)
    {
    try
    {
    obj.Start();
    Thread.Sleep(1000);
    }
    catch(Exception e)
    {
    obj.Abort();
    break;

    }
    }
    }
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <returns>true or false</returns>
    [WebMethod]
    public void SendMail()
    //public void SendMail(string UsType)
    {
    Application.Lock();
    Mail mail; // = new Mail();
    mail = MakeInMail("admin");
    if(mail != null)
    {
    int ID = mail.mailid;
    string sql = "update email set senddate=getdate(),sendflag=1 where id="+ID;
    try
    {
    MailMessage Objmail = new MailMessage();
    Objmail.To = mail.to;
    Objmail.From = mail.from;
    Objmail.Bcc = mail.bcc;
    Objmail.Cc = mail.cc;
    Objmail.Subject = mail.subject;
    Objmail.Body = mail.body;
    if(mail.attfile!="")
    {
    string sAttach = mail.attfile.ToString();
    char[] delim = new char[] {','};
    foreach (string sSubstr in sAttach.Split(delim))
    {
    MailAttachment MyAttachment = new MailAttachment(sSubstr);
    Objmail.Attachments.Add(MyAttachment);
    } }
    Objmail.UrlContentBase = "http://www.contoso.com/images";
    Objmail.UrlContentLocation = "http://www.contoso.com/images";
    Objmail.BodyFormat = MailFormat.Text;
    Objmail.Priority = MailPriority.Normal; Objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
    Objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", ""+mail.username+"");
    Objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", ""+mail.password+""); SmtpMail.SmtpServer = mail.mailserver;

    SmtpMail.Send(Objmail);

    UpdateDataBase(sql);
    //return true;
    }
    catch
    {
    //return false;
    }
    }
    else
    return ;//false;
    Application.UnLock();
    }
    /// <summary>
    /// 根据身份,email类型返回发送email的邮件服务器地址,用户名,密码
    /// </summary>
    /// <param name="filename">XML文件名称</param>
    /// <param name="usertype">用户身份</param>
    /// <param name="email">email地址</param>
    /// <returns>返回包含邮件服务器地址,用户名,密码的ArrayList</returns>
    public ArrayList SearchXml(string filename,string usertype,string email)
    {
    ArrayList arr = new ArrayList();
    XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath(filename));
    string em = "other";
    string am = "default";
    XmlNode node=doc.SelectSingleNode("//configure[@EmailAddr='"+email+"' and ../@role='"+usertype+"']");
    if(node!=null)
    {
    arr.Add(""+node.ChildNodes[0].InnerText.ToString()+"");
    arr.Add(""+node.ChildNodes[1].InnerText.ToString()+"");
    arr.Add(""+node.ChildNodes[2].InnerText.ToString()+"");
    arr.Add(""+node.ChildNodes[3].InnerText.ToString()+"");

    //arr.Add(""+node.ParentNode.Attributes["From"].Value.ToString()+"");
    }
    else
    {
    XmlNode node1 = doc.SelectSingleNode("//configure[@EmailAddr='"+am+"' and ../@role='"+usertype+"']");
    if(node1 !=null)
    {
    arr.Add(""+node1.ChildNodes[0].InnerText.ToString()+"");
    arr.Add(""+node1.ChildNodes[1].InnerText.ToString()+"");
    arr.Add(""+node1.ChildNodes[2].InnerText.ToString()+"");
    arr.Add(""+node1.ChildNodes[3].InnerText.ToString()+"");
    //arr.Add(""+node1.ParentNode.Attributes["From"].Value.ToString()+"");

    }
    else
    {
    XmlNode node2 = doc.SelectSingleNode("//@EamilAddr='"+em+"' and ../@role='"+am+"']");
    arr.Add(""+node2.ChildNodes[0].InnerText.ToString()+"");
    arr.Add(""+node2.ChildNodes[1].InnerText.ToString()+"");
    arr.Add(""+node2.ChildNodes[2].InnerText.ToString()+"");
    arr.Add(""+node2.ChildNodes[3].InnerText.ToString()+"");
    //arr.Add(""+node2.ParentNode.Attributes["From"].Value.ToString()+"");
    }
    }
    return arr;
    }
    /// <summary>
    /// 更新数据库,使邮件为已发送状态
    /// </summary>
    /// <param name="sql">sql语句</param>
    public void UpdateDataBase(string sql)
    {
    SqlConnection objConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["strConn"]);
    SqlCommand objCmd = new SqlCommand(sql,objConn);
    try
    {
    if(objConn.State == ConnectionState.Closed)
    objConn.Open();
    objCmd.ExecuteNonQuery();
    }
    catch
    {

    }
    finally
    {
    if(objConn.State == ConnectionState.Open)
    objConn.Close();
    } }
    /// <summary>
    /// 从数据库里面读数据,然后给Mail类的属性赋值
    /// </summary>
    /// <returns>Mail类的实例</returns>
    public Mail MakeInMail(string ustype)
    {
    Application.Lock();
    string sql = "select top 1 * from email where sendflag=0 order by savedate desc";
    string file = "roles.xml";
    //string user = "admin";
    string emailadd;
    string[] mailTo = new string[2];
    Mail mail = new Mail();
    ArrayList arrayList = new ArrayList();
    SqlConnection objConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["strConn"]);
    SqlCommand objCmd = new SqlCommand(sql,objConn);
    SqlDataReader objDr;
    try
    {
    if(objConn.State == ConnectionState.Closed)
    objConn.Open();
    objDr = objCmd.ExecuteReader();
    XmlDocument doc = new XmlDocument();
    if(objDr.Read())
    {
    mail.mailid = Convert.ToInt32(objDr.GetValue(0));
    mail.to = objDr.GetValue(1).ToString();
    mailTo = mail.to.Split('@');
    emailadd = mailTo[1].ToString();
    //mail.from = "[email protected]";
    mail.bcc = objDr.GetValue(2).ToString();
    mail.cc = objDr.GetValue(3).ToString();
    mail.subject = objDr.GetValue(4).ToString();
    mail.body = objDr.GetValue(5).ToString();
    mail.attfile = objDr.GetValue(6).ToString();
    mail.usertype = ustype.ToString();

    arrayList = SearchXml(file,mail.usertype,emailadd);

    mail.mailserver = arrayList[0].ToString();
    mail.username = arrayList[1].ToString();
    mail.password = arrayList[2].ToString();
    mail.from = arrayList[3].ToString(); return mail;

    }
    else
    return null;
    }
    catch
    {
    return null;
    }
    Application.UnLock();
    }
    }
    }
      

  2.   

    /// <summary>
    /// 自定义email类
    /// </summary>
    public class Mail
    {
    private string From;
    private string To;
    private string Subject;
    private string Body;
    private string AttFile;
    private string Cc;
    private string Bcc;
    private string MailServer;
    private string UserType;
    private string UserName;
    private string Password;
    private int MailId;
    public string to
    {
    get
    {
    return To;
    }
    set
    {
    To = value;
    }
    }
    public string from
    {
    get
    {
    return From;
    }
    set
    {
    From = value;
    }
    }
    public string subject
    {
    get
    {
    return Subject;
    }
    set
    {
    Subject = value;
    }
    }
    public string body
    {
    get
    {
    return Body;
    }
    set
    {
    Body = value;
    }
    }

    public string attfile
    {
    get
    {
    return AttFile;
    }
    set
    {
    AttFile = value;
    }
    }

    public string cc
    {
    get
    {
    return Cc;
    }
    set
    {
    Cc = value;
    }
    }

    public string bcc
    {
    get
    {
    return Bcc;
    }
    set
    {
    Bcc = value;
    }
    }
    public string mailserver
    {
    get
    {
    return MailServer;
    }
    set
    {
    MailServer = value;
    }
    }
    public string usertype
    {
    get
    {
    return UserType;
    }
    set
    {
    UserType = value;
    }
    }
    public string username
    {
    get
    {
    return UserName;
    }
    set 
    {
    UserName = value;
    }
    }
    public string password
    {
    get
    {
    return Password;
    }
    set 
    {
    Password = value;
    }
    }
    public int mailid
    {
    get
    {
    return MailId;
    }
    set
    {
    MailId = value;
    }
    }
    public Mail()
    {

    }
    }
      

  3.   

    http://blog.csdn.net/goody9807/archive/2004/12/31/235464.aspx