当前位置:编程学习 > C#/ASP.NET >>

跪求asp.net批量发送邮件问题

选择用户组后,给指定的用户组下的用户,批量发送邮件。 --------------------编程问答-------------------- 参考:
http://www.cnblogs.com/insus/articles/1689279.html턜Ҽ뒕牝ꯍ� --------------------编程问答-------------------- http://www.cnblogs.com/insus/articles/1689279.html --------------------编程问答-------------------- 谢谢我先看看 --------------------编程问答--------------------

/// <summary>
        /// 下发邮件
        /// </summary>
        /// <param name="context">邮件内容</param>
        /// <param name="employeeEmailArray">发送地址</param>
        /// <param name="employeeEmailccArray">抄送地址</param>
        /// <param name="employeeNameArray"></param>
        private static void SendMail(string context, string[] employeeEmailArray, string[] employeeEmailccArray)//
        {
            Log.WriteText("RemindProcesser--开始下发邮件", EnumLogType.Normal);

            string sServer = System.Configuration.ConfigurationManager.AppSettings["Server"];
            string sSender = System.Configuration.ConfigurationManager.AppSettings["Sender"];
            string sUserName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
            string sUserPass = System.Configuration.ConfigurationManager.AppSettings["UserPass"];
            string sSubject = System.Configuration.ConfigurationManager.AppSettings["Subject"];
            string sReceiver = employeeEmailArray[0].ToString();
            //if (employeeEmailccArray.Length > 0)
            //{
            //    string sCcReceiver = employeeEmailccArray[0].ToString();
            //}

            try
            {
                SmtpClient client = new SmtpClient(sServer);   //设置邮件协议
                client.UseDefaultCredentials = false;   //这一句得写前面
                client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过网络发送到Smtp服务器
                client.Credentials = new NetworkCredential(sUserName, sUserPass); //通过用户名和密码认证                  

                MailMessage mmsg = new MailMessage(new MailAddress(sSender), new MailAddress(sReceiver)); //发件人和收件人的邮箱地址

               if (employeeEmailArray.Length <= 1)
                {

                }
                else
                {
                    for (int i = 1; i < employeeEmailArray.Length; i++)
                    {
                        mmsg.To.Add(employeeEmailArray[i].ToString());

                    }
                }
                foreach (string employeeccEmail in employeeEmailccArray)
                {
                    mmsg.CC.Add(employeeccEmail);

                }

                mmsg.Subject = sSubject;      //邮件主题
                mmsg.SubjectEncoding = Encoding.UTF8;   //主题编码               
                mmsg.Body = context;         //邮件正文
                mmsg.BodyEncoding = Encoding.UTF8;      //正文编码
                mmsg.IsBodyHtml = true;    //设置为HTML格式
                mmsg.Priority = MailPriority.High;   //优先级
                Log.WriteText("开始发送:" , EnumLogType.Normal);
                client.Send(mmsg);
            }
            catch (Exception ex)
            {
                Log.WriteText("RemindProcesser--邮件下发失败:" + ex.Message, EnumLogType.Normal);
            }


            //InsertMailingList(context, employeeNameArray);

            Log.WriteText("RemindProcesser--成功下发邮件给:" + sReceiver, EnumLogType.Normal);
        }

贴一段自己的 --------------------编程问答-------------------- --------------------编程问答--------------------
给你一个我的例子,直接复制粘贴自己看看吧,都有注释:
页面内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendEmail2.aspx.cs" Inherits="SendEmail2" %>

<!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">
    <div>
        <table align="center" border="0" cellpadding="0" cellspacing="0" width="776">
            <tr>
                <td>
                    <table align="center" border="0" cellpadding="4" cellspacing="1" width="600" bgcolor="#cccccc">
                        <tr>
                            <td colspan="2" bgcolor="#f0f0f0" align="center">
                                电子邮件发送测试程序</td>
                        </tr>
                        <tr>
                            <td bgcolor="#f0f0f0" align="right" width="150">发送人:</td>
                            <td bgcolor="#ffffff" align="left"><asp:TextBox ID="fromMail" runat="server" Width="300" /></td>
                        </tr>
                        <tr>
                            <td bgcolor="#f0f0f0" align="right">收件人:</td>
                            <td bgcolor="#ffffff" align="left"><asp:TextBox ID="toMail" runat="server" Width="300" /></td>
                        </tr>
                        <tr>
                            <td bgcolor="#f0f0f0" align="right">抄送人:</td>
                            <td bgcolor="#ffffff" align="left"><asp:TextBox ID="ccMail" runat="server" Width="300" /></td>
                        </tr>
                        <tr>
                            <td bgcolor="#f0f0f0" align="right">暗送人:</td>
                            <td bgcolor="#ffffff" align="left"><asp:TextBox ID="bccMail" runat="server" Width="300" /></td>
                        </tr>
                        <tr>
                            <td bgcolor="#f0f0f0" align="right">主    题:</td>
                            <td bgcolor="#ffffff" align="left"><asp:TextBox ID="subject" runat="server" Width="300" /></td>
                        </tr>
                        <tr>
                            <td bgcolor="#f0f0f0" align="right">附    件:</td>
                            <td bgcolor="#ffffff" align="left"><input type="file" id="FileUpload1" runat="server" /></td>
                        </tr>
                        <tr>
                            <td bgcolor="#f0f0f0" align="right">内    容:</td>
                            <td bgcolor="#ffffff" align="left"><asp:TextBox ID="body" TextMode="multiLine" runat="server" Width="300" Height="200" /></td>
                        </tr>
                        <tr>
                            <td bgcolor="#f0f0f0" align="right">格    式:</td>
                            <td bgcolor="#ffffff" align="left"><asp:RadioButtonList ID="format" runat="server"/></td>
                        </tr>
                        <tr>
                            <td bgcolor="#f0f0f0" colspan="2" align="center">
                                <asp:Button ID="send" runat="server" Text="发送" OnClick="send_Click" />  
                                <asp:Button ID="reset" runat="server" Text="重置" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

页面代码:

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;

public partial class SendEmail2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            format.Items.Add(new ListItem("文本", "0"));
            format.Items.Add(new ListItem("HTML", "1"));
            format.Items[0].Selected = true;

            fromMail.Text = "Tianc@163.com"; //发送方邮件
            fromMail.Enabled = false;
        }
    }

    /// <summary>
    /// 点击发送邮件时引发的事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void send_Click(object sender, EventArgs e)
    {
        //附件的路径
        string filePath = String.Empty;
        //判断上传文件是否,并且是否小于10MB
        if (FileUpload1.PostedFile.ContentLength != 0 && FileUpload1.PostedFile.ContentLength < 10485760)
        {
            filePath = FileUpload1.PostedFile.FileName;
        }
        else
        {
            Response.Write("<script>alert('上传文件为空或者是上传文件大于10MB!');</script>");
            return;
        }
        bool isBodyHtml = false;
        if (format.SelectedValue == "1")
        {
            isBodyHtml = true;
        }
        //通过163邮箱来发送我要发送的内容,可以理解成邮件服务器或者叫中转服务器
        bool flag = SendMail.Send(fromMail.Text, toMail.Text, subject.Text, isBodyHtml, body.Text, "smtp.163.com", filePath, "Tianc", "123456");
        if (flag)
        {
            Response.Write("<script>alert('发送成功!');</script>");
        }
    }
}

/// <summary>
///发送邮件类
/// </summary>
public class SendMail
{
    public SendMail() { }
    /// <summary>    
    /// 给多个用户发送邮件    
    /// </summary>    
    /// <param name="from">发送人邮件地址</param>    
    /// <param name="to">接收人邮件地址</param>    
    /// <param name="subject">邮件主题</param>    
    /// <param name="isBodyHtml">是否是Html</param>    
    /// <param name="body">邮件主体内容</param>    
    /// <param name="smtpHost">SMTP服务器地址</param>    
    /// <param name="fileupLoadPath">文件客户端的上传路径</param>    
    /// <param name="userName">用户名</param>    
    /// <param name="password">密码</param>    
    public static bool Send(String from, String to, String subject, bool isBodyHtml, String body, String smtpHost, String fileupLoadPath, String userName, String password)
    {
        //根据,分隔成多个邮件
        string[] ts = to.Split(',');
        //是否成功
        bool isSuccess = true;
        //循环发送
        foreach (String t in ts)
        {
            try
            {
                //创建发送电子邮件类
                MailMessage message = new MailMessage();
                //设置发送人邮件地址
                message.From = new MailAddress(from);
                //将收件人邮件地址添加收件人电子邮件集合中
                message.To.Add(t.Trim());
                //设置电子邮件主题
                message.Subject = subject;
                //设置邮件正文是否可以是HTML标签
                message.IsBodyHtml = isBodyHtml;
                //设置电子邮件的优先级
                message.Priority = MailPriority.High;
                //设置邮件正文
                message.Body = body;
                //设置主题和正文编码为UTF8
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                //获取附加到电子邮件的附件集合
                if (fileupLoadPath.Length != 0)
                {
                    message.Attachments.Add(new Attachment(fileupLoadPath));
                }
                //抄送收件人,嘿嘿将邮件发给收件人以外的人喽
                //message.CC.Add("Tianc@163.com");
                //密件抄送收件人,嘿嘿我不想别人看到我同时发信给这个邮箱,那就用上吧
                //message.Bcc.Add("Tianc@126.com");
                //设置邮件的回复地址
                //message.ReplyTo = new MailAddress("Tianc@163.com");

                //使用简单邮件传输协议来发送邮件
                SmtpClient sc = new SmtpClient();
                //设置stmp邮件服务器地址
                sc.Host = smtpHost;
                //设置stmp服务器端口,这里使用163的端口
                sc.Port = 25;
                /*随身份验证信息一起发送,false表示不发送身份验证信息
                 有些 SMTP 服务器要求在代表客户端发送电子邮件前验证客户端的身份*/
                sc.UseDefaultCredentials = true;
                //如果服务器不支持ssl则报服务器不支持安全连接错误
                //Secure Socket Layer(SSL:安全套接字层)是一种能将在用户端与伺服器端间传送的邮件加密的通讯协定
                sc.EnableSsl = true;
                //验证发件人身份凭据
                sc.Credentials = new System.Net.NetworkCredential(userName, password);
                //指定通过网络发送电子邮件
                sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                //将指定邮件发送到STMP服务器
                sc.Send(message);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                isSuccess = false;
            }
        }
        return isSuccess;
    }
}
--------------------编程问答--------------------
这只是我自己的一个demo,你可以根据自己的需求进行修改,很简单的,现成的都给你了,给分吧。
--------------------编程问答-------------------- 谢谢各位的帮忙了。感谢!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,