Click here to Skip to main content
15,356,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone I'm coding bulk mail sender application with c# doing part by part and when I send single mail everything is fine I'm not getting any problem bu when I want to send bulk mail for example to 5 recipient I'm getting an error 4 time the error message is "The Smtp Client Not Connected" 1 time "Message Sent" so out of 5 mails only 1 mail sending

C#
using MailKit.Net.Smtp;
using MailKit;
using MimeKit;

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;

            BodyBuilder builder = new BodyBuilder();
            builder.HtmlBody = rchtxtboxebody.Text;

            MimeMessage message = new MimeMessage();
            message.Subject = txtsubject.Text;
            message.Body = builder.ToMessageBody();

            SmtpClient sc = new SmtpClient();
            sc.Timeout = 3000;
            sc.AuthenticationMechanisms.Remove("XOAUTH2");

            sc.Connect(txthostname.Text, int.Parse(txtport.Text), chkssl.Checked);
            sc.Authenticate(txtusername.Text, txtpassword.Text);
            foreach (string email in lstleads.Items)
            {
                try
                {
                    message.From.Add(new MailboxAddress(txtfromname.Text, txtfrommail.Text));
                    message.To.Add(MailboxAddress.Parse(email));
                    sc.Send(message);

                    Thread.Sleep(3000);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    sc.Disconnect(true);
                }
            }

        }

this is the my code I'm using where is problem?

What I have tried:

I tried to change timeout to 10000 but not worked
Posted
Updated 17-May-22 2:45am

1 solution

The finally { sc.Disconnect(true); } block inside your loop causes the client to disconnect after the first message is sent, and you never try to reconnect it.

You also don't clear the recipient list after the first message. If you send 10 messages, the first recipient will receive 10 copies, each one with one more address from your list appended to it. The second recipient will receive 9 messages, and so on.
C#
try
{
    foreach (string email in lstleads.Items)
    {
        message.To.Clear();
        message.To.Add(MailboxAddress.Parse(email));
        sc.Send(message);
        Thread.Sleep(3000);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    sc.Disconnect(true);
}

NB: This sort of code is highly likely to get your email account banned for spamming.
   
Comments
YusufA0 17-May-22 9:08am
   
thanks thats helped me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900