asp.net邮件群发

因为要用到邮件群发所以研究了一下,现在拿出来与大家分享(缺点:有数量限制,如果数量大的话需要自己架设smtp服务器)

aspx界面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendMail.aspx.cs" Inherits="WebApplication1._Default" Async="true"%>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>发送邮件</title>
</head>
<body>
<form id="form1" runat="server">
   
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>

.cs界面

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

 

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }
        public void SendEmail()
        {
            string from = "palmenzweig@163.com";   //发送方邮箱
            MailMessage newEmail = new MailMessage();

            #region 发送方邮件
            newEmail.From = new MailAddress(from, from);
            #endregion

            #region 发送对象,可群发
            newEmail.To.Add(new MailAddress("644639751@qq.com"));  //接收方邮箱一
            //newEmail.To.Add(new MailAddress("palmenzweig@yeah.net"));  //接收方邮箱二
            //newEmail.To.Add(new MailAddress("zimoqinghe@163.com"));
            //newEmail.To.Add(new MailAddress("julyqianzimo@qq.com"));
            //newEmail.To.Add(new MailAddress("zimoqinghe@qq.com"));
            //newEmail.To.Add(new MailAddress("ws_788@163.com "));
            //newEmail.To.Add(new MailAddress("wangsen619@qq.com"));
            //newEmail.To.Add(new MailAddress("Engle_love@163.com"));
            #endregion

            #region Subject
            newEmail.Subject = TextBox1.Text;  //标题
            #endregion

            #region Body
            string strBody = "<p><b>邮件群发测试,邮件测试内容</b></p>"; //html格式,也可以是普通文本格式
            newEmail.Body = strBody;  //内容
            #endregion

            #region 上传附件
            // Attachment MsgAttach = new Attachment(this.FileUpload1.PostedFile.FileName);//可通过一个FileUpload地址获取附件地址
            //newEmail.Attachments.Add(MsgAttach);
            #endregion

            #region Deployment
            newEmail.IsBodyHtml = true;                //是否支持html
            newEmail.Priority = MailPriority.High;  //优先级
            #endregion

            //发送方服务器信息
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = new System.Net.NetworkCredential("palmenzweig@163.com", "zhoukeqiangcxh");
            smtpClient.Host = "smtp.163.com"; //主机

            //smtpClient.Send(newEmail);   //同步发送,程序将被阻塞

            #region 异步发送, 会进入回调函数SendCompletedCallback,来判断发送是否成功
            smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);//回调函数
            string userState = "测试";
            smtpClient.SendAsync(newEmail, userState);
            #endregion

        }

        private static void SendCompletedCallback(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)  //邮件发送被取消
            {
                Console.WriteLine("发送被取消!");
            }
            if (e.Error != null)   //邮件发送失败
            {
                Console.WriteLine("发送失败!");
            }
            else   //发送成功
            {
                Console.WriteLine("发送成功!");
            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            SendEmail();

        }
    }
}

如有转载请注明:http://www.cnblogs.com/xiaoting/(西湖小巷---博客园)

posted @ 2010-12-31 17:33  西湖小巷  阅读(2515)  评论(1编辑  收藏  举报