using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;//引用此命名空间
using System.Text;
public partial class Send : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //发送者邮件地址 
        string host = "ceshiyong_xu@163.com";
        //发送者邮件密码   
        string pwd = "ceshiyong";
        //接受者邮箱地址 
        string reciver = "ceshizhuanyong_xu@163.com";
        //SMTP服务器的主机名 
        string domainhost = "smtp.163.com";
        //邮件标题 
        string subject = "XXX邮件测试";
        //邮件发送的主题内容  
        string body = "这是邮件发送的主题";
        //调用方法,发送邮件
        if (SendEmail(host, pwd, reciver, domainhost, subject, body))
        {
            Response.Write("<script>alert('发送成功!')</script>");
        }
    }

    /// <summary> /// 发送邮件,返回true表示发送成功/// </summary>  
    /// /// <param name="a">发件人邮箱地址;发件人用户名</param>   
    /// /// <param name="b">密码</param>   
    /// /// <param name="c">接受者邮箱地址</param>   
    /// /// <param name="host">SMTP服务器的主机名</param> 
    /// /// <param name="sub">邮件主题行</param>     
    /// /// <param name="body">邮件主体正文</param> 
    public bool SendEmail(string a, string b, string c, string host, string sub, string body)
    {
        System.Net.Mail.SmtpClient client = new SmtpClient();
        client.Host = host;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(a, b);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        try
        {
            System.Net.Mail.MailMessage message = new MailMessage(a, c);
            message.Subject = sub;
            message.Body = body;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            client.Send(message);
            return true;
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message + "')</script>");

            return false;
        }

    }

}
View Code

 

posted on 2013-07-23 11:28  小小乌龟把头藏  阅读(143)  评论(0编辑  收藏  举报