时事通讯群发电子邮件

时间:2012-11-08 16:12:17

标签: c# asp.net

我的时事通讯有点麻烦......

让我解释一下: 我有一个带有我的电子邮件的sql表,没有其他内容和一个sql表,我发送邮件后存储邮件,不知何故邮件不会发送,但它会保存邮件。

它都是在表单视图中设置的。

protected void btnSendNewsLetter_Click(object sender, EventArgs e)
    {
        TextBox subject = (TextBox)fvNewsletter.Row.FindControl("txtSubject");
        TextBox body = (TextBox)fvNewsletter.Row.FindControl("txtBody");

        string connection = WebConfigurationManager.ConnectionStrings["GreenCollaborationConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(connection);

        con.Open();

        SqlCommand comm = new SqlCommand("Select Email from MailingList", con);
        SqlDataAdapter da1 = new SqlDataAdapter(comm);
        GreenDataSet ds1 = new GreenDataSet();
        da1.Fill(ds1);
        if(ds1.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i <= ds1.Tables[0].Rows.Count - 1; i++)
            {
                string email = ds1.Tables[0].Rows[i].ItemArray[0].ToString();
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                mail.From = new MailAddress("noreply@GreenCollaboration.com", "GreenCollaboration");
                mail.To.Add(email);
                mail.Subject = subject.Text;
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.Body = body.Text;
            }
        }
    }

我希望some1可以帮助我:)。

3 个答案:

答案 0 :(得分:1)

尝试以下方法:

protected void btnSendNewsLetter_Click(object sender, EventArgs e)
{
    //snip

    //Create the smtp client to send the mails with.
    SmtpClient client = new SmtpClient(server);
    // Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    if(ds1.Tables[0].Rows.Count > 0)
    {
        for (int i = 0; i <= ds1.Tables[0].Rows.Count - 1; i++)
        {
            string email = ds1.Tables[0].Rows[i].ItemArray[0].ToString();
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            //snip
            try 
            {
                client.Send(message);
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex.ToString());             
            }
        }
    }
}

Msdn MailMessage Class

答案 1 :(得分:0)

呃...好像你只是错过了以下内容:

SmtpClient client = new SmtpClient();
client.Send(message);

将其放在以下一行:

mail.Body = body.Text;

并在web.config

中添加以下详细信息
<system.net>
  <mailSettings>
    <smtp from="test@foo.com">
      <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
    </smtp>
  </mailSettings>
</system.net>

编辑:解决了不发送电子邮件的直接问题。

然而,如其他地方所述,要发送电子邮件,您应该执行更强大的实施,例如见this link for a mass email sender example

答案 2 :(得分:0)

您生成了消息但从未实际发送过消息。发送的机制很少,如SMTP或Exchange

因此,如果您打算使用SMTP

//Somewhere on top
var server="EMAIL.CORP.COMPANY" //or "10.10.10.10"
var smtpClient= new SmtpClient(server);
smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials; //AD credentials

//In the end of your for loop
try {
    smtpClient.Send(mail);
catch (Exception ex) {
    //Handle the exception    
}