【问题标题】:Unable to send Mails through SMTP using Asp.Net无法使用 Asp.Net 通过 SMTP 发送邮件
【发布时间】:2013-07-25 09:50:05
【问题描述】:

当我点击发送按钮时,我需要一次向 60 到 100 名学生发送邮件。如果我有 6 到 7 名学生,这很好,但是当我开始发送给 60 名学生时,我的代码显示以下错误

系统存储空间不足。服务器响应是:每个连接的电子邮件过多。

这是我的代码

protected void btnsend_Click(object sender, EventArgs e) {

     if (drptopics.SelectedValue == "-1")
            {
                Response.Write(@"<script language='javascript'>alert('Please select one Topic');</script>");
            }


     else

     {

    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=xxxx; User Id=sa; Password=xxxx; Initial Catalog=xxxx; Integrated Security=SSPI;";
    conn.Open();


    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "select EMail_1 from Student_Info where Branch_Code='ap' and Student_ID in(select Student_ID from Admissions where Branch_Code='ap' and Batch_ID='" + txtbatchid.Text + "')";
    cmd.CommandType = CommandType.Text;

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.SelectCommand = cmd;
    DataSet ds = new DataSet();
    da.Fill(ds);




    if (ds.Tables[0].Rows[0][0] != null)
    {
        int count = ds.Tables[0].Rows.Count;


        for (int i = 0; i < count; i++)
        {

            MailMessage mm = new MailMessage();
            mm.To.Add(new MailAddress(ds.Tables[0].Rows[i][0].ToString()));
            mm.From = new MailAddress("xxxxx@abc.com");
            mm.Subject = "SMS and Interview Questions of Oracle 11g DBA";
            mm.IsBodyHtml = true;





            //Day 1 Architecture 1
             if (drptopics.SelectedValue == "1")
            {
                Attachment attachment1 = new Attachment(Server.MapPath("~/Oracle 11g DBA/Day 1/Architecture-1-I.doc")); //create the attachment
                mm.Attachments.Add(attachment1);
                Attachment attachment2 = new Attachment(Server.MapPath("~/Oracle 11g DBA/Day 1/Architecture1-S.doc")); //create the attachment
                mm.Attachments.Add(attachment2);

            }


            //Day 2 Architecture 2
            else if (drptopics.SelectedValue == "2")
            {
                Attachment attachment1 = new Attachment(Server.MapPath("~/Oracle 11g DBA/Day 2/Architecture-2-I.doc")); //create the attachment
                mm.Attachments.Add(attachment1);
                Attachment attachment2 = new Attachment(Server.MapPath("~/Oracle 11g DBA/Day 2/Architecture2-S.doc")); //create the attachment
                mm.Attachments.Add(attachment2);
            }

             mm.Body = "<html><head><body><h1>Thank you for choosing Wilshire</h1><br><b>Team Wilshire<b></body></head></html>";
            SmtpClient s = new SmtpClient("smtp.net4india.com");
            s.Send(mm);


        }

    }

}

请告诉我解决方案............

【问题讨论】:

  • 主体是否相同?将所有收件人放入密件抄送中,然后使用一封电子邮件发送。
  • 取决于查询 cmd.CommandText = "从 Student_Info 中选择 EMail_1,其中 Branch_Code='ap' 和 Student_ID in(从招生中选择 Student_ID,其中 Branch_Code='ap' and Batch_ID='" + txtbatchid.Text + "')";电子邮件将发生变化
  • 在我的回答中,我展示了如何将所有收件人放入密件抄送中

标签: asp.net


【解决方案1】:

SmtpClient.SendAsync() 可能会导致其他问题(SMTP 服务器设置了每分钟的最大邮件数以避免垃圾邮件)!

由于邮件相同,解决方案是一封邮件,所有收件人都为隐藏收件人 (Bcc)!

MailMessage mm = new MailMessage();

for (int i = 0; i < count; i++)
{
  mm.Bcc.Add(new MailAddress(ds.Tables[0].Rows[i][0].ToString()));
}
mm.To.Add(new MailAddress("xxxxx@abc.com");
mm.From = new MailAddress("xxxxx@abc.com");
mm.Subject = "SMS and Interview Questions of Oracle 11g DBA";

....

SmtpClient s = new SmtpClient("smtp.net4india.com");
s.Send(mm);

【讨论】:

  • 非常感谢它的工作正常,非常感谢
【解决方案2】:

错误提示“每个连接的电子邮件过多”。为什么不每次连接发送更少的电子邮件?每隔几封电子邮件添加一个循环并处理连接,然后创建一个新的?

【讨论】:

    【解决方案3】:

    使用SmtpClient.SendAsync Method发送邮件,它的发送邮件后台一一发送。

    Reference Link 1

    Reference Link 2

    【讨论】:

    • 我在那里使用了 SendAsync() 方法我收到此错误“在此上下文中不允许异步操作。开始异步操作的页面必须将 Async 属性设置为 true,并且异步操作只能是在 PreRenderComplete 事件之前的页面上开始。"
    【解决方案4】:

    如果您正在使用最新的框架,您也可以使用 ASP.NET4.5 的新功能。

    参考链接:

    http://www.codeproject.com/Articles/599756/Five-Great-NET-Framework-4-5-Features

    功能 1:async 和 await(代码标记)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-13
      • 2017-12-14
      • 2013-11-28
      • 1970-01-01
      • 2019-05-15
      • 2020-02-21
      • 2020-05-30
      • 2014-03-05
      相关资源
      最近更新 更多