0

hi guys am having problem sending bulk mails using listbox in windows forms....it shows sending failed when i try to send the bulk mails..here is my code i do not know what is wrong with the code i have checked and checked i do not know what is wrong

 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
    Which line throws the error? Does SendMail1 work and SendMail2 not? What's the error message? – LarsTech May 24 '17 at 19:47
  • this is the line that shows the error smtp.Send(tm); – John Iliya May 24 '17 at 19:53
  • this is the error:Failure sending mail. – John Iliya May 24 '17 at 19:54
  • If something isn't working, try to make things less complicated. Remove that Parallel.ForEach. You only have one row in your DataTable anyway. What are your values when you try to send the email? This is a debugging problem Use the debugger. Also, see [smtpclient “ failure sending mail”](https://stackoverflow.com/q/2209617/719186) – LarsTech May 24 '17 at 20:00
  • My guess is that you are not using the correct smtp address to send email. – Akshay Mahajan May 24 '17 at 20:05
  • if i remove the Parallel.ForEach how will i now do it sir...am new to c# – John Iliya May 24 '17 at 20:17
  • Just call SendMail, change the `x["eMail"].ToString()` to just `i`. You don't have to do `i.ToString()` since `i` is already a string. – LarsTech May 24 '17 at 20:25
  • wow works great changed it from the x["eMail"].ToString() to just i... thanks for the great help LarsTech – John Iliya May 24 '17 at 20:34

1 Answers1

0

You're trying to send e-mail in bulk. If this is working "sometimes", then what's very likely is that the SMTP server is rejecting your messages if your send rate higher than a threshold set by the administrators. There's not much you can do if this is the case, unless the administrators will change their policy to accommodate your application. Or, you need a different SMTP service (Amazon has one, just for bulk mail applications like yours).

Xavier J
  • 4,326
  • 1
  • 14
  • 25
  • Try this http://cybercity.nyc/blog/bulk-email-or-spam-how-to-send-email-blasts-and-newsletters-and-avoid-blacklisting/ and this https://aws.amazon.com/ses/faqs/ and this http://support.proofpointessentials.com/index.php?/Knowledgebase/Article/View/74/4/bulk-email-limitations-explained – Xavier J May 24 '17 at 21:50
  • These are examples of sending limits. – Xavier J May 24 '17 at 21:50