2013-07-25 51 views
3

我需要發送郵件給60到100名學生,當我點擊發送按鈕。這是行之有效的,如果我有6至7名學生,但是當我開始發送給60名學生我的代碼顯示以下錯誤無法通過SMTP發送郵件使用Asp.Net

系統存儲不足。服務器響應是:每個連接的電子郵件太多。

這裏是我的代碼

保護無效btnsend_Click(對象發件人,EventArgs的){

 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("[email protected]"); 
      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); 


     } 

    } 

}

請告訴我解決........ ................

+2

身體是否一模一樣?將所有收件人放入密件抄送中,並用一封電子郵件發送。 – Alexander

+0

取決於查詢cmd.CommandText =「從Student_Info中選擇EMail_1,其中Branch_Code ='ap'和Student_ID in(從Admissions中選擇Student_ID,其中Branch_Code ='ap'和Batch_ID ='」+ txtbatchid.Text +'')「;電子郵件將會改變 – user2618074

+0

在我的回答中,我將展示如何將所有收件人放入BCC –

回答

2

SmtpClient.SendAsync()可能會導致其他問題(SMTP服務器設置每分鐘的最大郵件數量,避免垃圾郵件) !

由於郵件完全相同,因此解決方案爲1個郵件,收件人全部爲隱藏收件人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("[email protected]"); 
mm.From = new MailAddress("[email protected]"); 
mm.Subject = "SMS and Interview Questions of Oracle 11g DBA"; 

.... 

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

非常感謝你的工作很好,非常感謝 – user2618074

1

錯誤顯示'每個連接的電子郵件太多'。爲什麼不每次連接發送更少的電子郵件?添加一個循環,每處理幾封電子郵件,然後創建一個新的連接?

1

使用SmtpClient.SendAsync Method來發送郵件一個接一個地發送郵件背景。

Reference Link 1

Reference Link 2

+0

我在那裏使用了SendAsync()方法我得到了這個錯誤「在這種情況下不允許異步操作,啓動異步操作的頁面必須具有Async屬性設置爲true,異步操作只能在PreRenderComplete事件之前的頁面上啓動。「 – user2618074