2017-05-24 1 views
0

嗨,大家好,我有問題發送羣發郵件使用Windows窗體中的列表框......它顯示發送失敗,當我嘗試發送大宗郵件..這裏是我的代碼我不知道我檢查和檢查的代碼有什麼問題當我運行代碼,併發送它顯示發送失敗

void SendMail1(string from,string to,string subject,string message,string host,int port,string un,string pw) 
    { 
     MailMessage tm = new MailMessage(); 
     tm.From = new MailAddress(from); 
     tm.To.Add(new MailAddress(to)); 
     tm.Subject = subject; 
     tm.Body = message; 
     tm.IsBodyHtml = true; 
     tm.Priority = MailPriority.Normal; 
     tm.SubjectEncoding = Encoding.UTF8; 
     var smtp = new SmtpClient(); 
     { 
      smtp.Host = host; 
      smtp.Port = port; 
      smtp.EnableSsl = true; 
      smtp.UseDefaultCredentials = true; 
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network;    
      //default credentials 
      smtp.Credentials = new NetworkCredential(un,pw); 
     } 
     smtp.Send(tm); 
     MetroFramework.MetroMessageBox.Show(this, "Bulk Mail Successfully Sent.....", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    } 


    private void btnSendEmail2_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      if (string.IsNullOrEmpty(txtSubject2.Text) || string.IsNullOrEmpty(txtMessage2.Text) || string.IsNullOrEmpty(txtFrom2.Text) || string.IsNullOrEmpty(txtEmailAddress2.Text) || string.IsNullOrEmpty(txtPassword2.Text) || string.IsNullOrEmpty(txtAttachment2.Text)) 
      { 
       MetroFramework.MetroMessageBox.Show(this, "One Or More Fields Empty..Please Check To See Which Fields Are Empty,Before Sending Mails...", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      } 
      else 
      { 
       //send mails here 
       foreach(string i in lstEmails.Items) 
       { 
        if (i.Trim() != string.Empty) 
        { 
         //declare all fields 
         DataTable dt = new DataTable("tblEmails"); 
         dt.Columns.Add("Email", typeof(string)); 
         dt.Rows.Add(i.ToString()); 
         string _from = txtFrom2.Text.Trim(); 
         string _subject = txtSubject2.Text.Trim(); 
         string _message = txtMessage2.Text.Trim(); 
         string _host = Convert.ToString(ddlProvider2.SelectedItem); 
         int _port = 587; 
         string _un = txtEmailAddress2.Text.Trim(); 
         string _pw = txtPassword2.Text.Trim(); 
         Parallel.ForEach(dt.AsEnumerable(), x => 
         { 
          SendMail1(_from, x["Email"].ToString(), _subject,_message 
           , _host, _port, _un, _pw); 
         }); 

         // MetroFramework.MetroMessageBox.Show(this, i + "\n", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        } 
        else 
        { 
         MetroFramework.MetroMessageBox.Show(this, "Please Load Some Emails To Send Mail Message...", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK); 
     } 
    } 
+2

哪一行會引發錯誤? SendMail1工作而SendMail2不工作?什麼是錯誤信息? – LarsTech

+0

這是顯示錯誤smtp.Send(tm)的行; –

+0

這是錯誤:發送郵件失敗。 –

回答

0

您試圖發送電子郵件批量。如果「有時」有效,那麼如果您的發送速率高於管理員設置的閾值,SMTP服務器很可能會拒絕您的消息。如果情況確實如此,您可以做的事情不多,除非管理員將更改其政策以適應您的應用程序。或者,您需要一個不同的SMTP服務(亞馬遜有一個,僅用於像您這樣的批量郵件應用程序)。

+0

哇不知道先生...請你能幫助我的鏈接瞭解更多關於它... –

+0

試試這個http://cybercity.nyc/blog/bulk-email-or-spam-how-to-send-email-blasts-and-newsletters-and-avoid-blacklisting/和這個https:/ /aws.amazon.com/ses/faqs/和這個http://support.proofpointessentials.com/index.php?/Knowledgebase/Article/View/74/4/bulk-email-limitations-explained –

+0

這些是發送限制。 –