发送电子邮件列表c#

时间:2013-07-15 12:50:58

标签: c# asp.net .net

我的网页包含“文件夹访问权限”和一个CheckBox

今天,在我的.cs中,对于此文件夹访问中选中的每个checkbox,我都会向此文件夹的所有者发送电子邮件。 要收到此电子邮件,我会在我的页面中创建一个HiddenField to Email。

但是,现在,每个文件夹都有多个所有者。如果以这种方式继续,我有重复的行。 我如何获得每个文件夹的电子邮件列表?

我的.aspx

<Columns>
    <asp:BoundField DataField="FolderAccess" HeaderText="Folder Access" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server" Text="Access to Read" OnCheckedChanged="CheckBox1_ChangeCheck"
                AutoPostBack="true" />
            <asp:HiddenField ID="Email" runat="server" Value='<%# Bind("Email") %>' />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

我的.cs

public class ListRequest
{
    public string Email { get; set; }
    public string FolderAccess { get; set; }
}

public List<ListRequest> PreencheValores(SqlDataReader reader)
{
    var lista = new List<ListRequest>();
    while (reader.Read())
    {
        var listRequest = new ListRequest();

        listRequest.Email = reader["Email"].ToString();
        listRequest.FolderAccess = reader["FolderAccess"].ToString();
        lista.Add(listRequest);
    }
    return lista;
}


public List<ListRequest> ConsultarRequest()
{
    var lstRetorno = new List<ListRequest>();
    using (objConexao = new SqlConnection(strStringConexao))
    {
        using (objCommand = new SqlCommand(strSelectPorID, objConexao))
        {
            try
            {
                objConexao.Open();
                var objDataReader = objCommand.ExecuteReader();
                if (objDataReader.HasRows)
                    lstRetorno = PreencheValores(objDataReader);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                objConexao.Close();
            }
        }
    }
    return lstRetorno;
}

protected void btnSend_OnClick(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView.Rows)
    {
        CheckBox check = (CheckBox)row.FindControl("CheckBox1");
        HiddenField hd1 = (HiddenField)row.FindControl("Email");
        string email = hd1.Value.ToString();

        if (check.Checked == true)
        {

            System.Net.Mail.MailMessage objEmail = new System.Net.Mail.MailMessage();
            objEmail.From = new MailAddress("caio.jesus@br.com", "XXX");
            objEmail.To.Add(email);
            objEmail.Priority = System.Net.Mail.MailPriority.High;
            objEmail.IsBodyHtml = true;
            objEmail.Subject = "System NDRSecurity - Novas Requisições.";
            objEmail.Body = "TEST";
            objEmail.SubjectEncoding = Encoding.GetEncoding("ISO-8859-1");
            objEmail.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");
            SmtpClient objSmtp = new SmtpClient("XXX");
            objSmtp.EnableSsl = true;
            objSmtp.Port = 25;
            objSmtp.Credentials = new NetworkCredential("caio.jesus@br.com", "XXX");
            objSmtp.Send(objEmail);
        }
    }

    Response.Redirect("home.aspx");
}

2 个答案:

答案 0 :(得分:1)

我想你可以让你的ListRequest对象包含一个电子邮件地址列表。然后你的隐藏字段将包含该列表。

public class ListRequest
{
    public List<string> EmailAddresses { get; set; }
    public string FolderAccess { get; set; }
}

然后你必须改变你的onlcick来循环hd1变量中的值。

if (check.Checked == true)
{
    foreach(string email in (List<string>hd1.Value))
    {
        System.Net.Mail.MailMessage objEmail = new System.Net.Mail.MailMessage();
        objEmail.From = new MailAddress("caio.jesus@br.com", "XXX");
        objEmail.To.Add(email);
        objEmail.Priority = System.Net.Mail.MailPriority.High;
        objEmail.IsBodyHtml = true;
        objEmail.Subject = "System NDRSecurity - Novas Requisições.";
        objEmail.Body = "TEST";
        objEmail.SubjectEncoding = Encoding.GetEncoding("ISO-8859-1");
        objEmail.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");
        SmtpClient objSmtp = new SmtpClient("XXX");
        objSmtp.EnableSsl = true;
        objSmtp.Port = 25;
        objSmtp.Credentials = new NetworkCredential("caio.jesus@br.com", "XXX");
        objSmtp.Send(objEmail);
    }
}

答案 1 :(得分:0)

除了John Kraft建议的内容之外,如果您仍希望保留String而不是列表,则可以使用CSV电子邮件地址。

String s = "email1@email.com, email2@email.com, email3@email.com";

然后使用Regex进行拆分(这需要额外的空间):

foreach(string email in Regex.Split(s, @"\s*[,;]\s*"))
{ ... }