ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
I am sending bulk emails using Parallel Task.When 'From Mail Id' and 'To Mail Id' are same then I got duplicate mails on To Mail Id.
I used SendEmail(SmtpServer server, SmtpMail mail) method to send mail.
Any suggestions...
ivan
  • ivan
  • 100% (Exalted)
  • Administration
11 years ago

Hi, you can use LogFileName property to generate a log file.

then you can check the log file.

You can find

RCPT TO: xxxxx

If there is only one RCPT TO, that means you only sent the email to the recipient once, if you still get two emails, that means your server duplicates the email. If there are one more RCPT TO, please check if you add the same recipient multiple times in your codes.
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
Hi,

I have these logs in log file ,I have got 8 mails while recipient only four and log shows three recipients

MAIL FROM:
235 2.7.0 Accepted
MAIL FROM:
235 2.7.0 Accepted
MAIL FROM:
250 2.1.0 OK oj6sm50418535pab.9 - gsmtp
RCPT TO:
250 2.1.0 OK ki1sm38481904pbd.1 - gsmtp
RCPT TO:
250 2.1.0 OK sd3sm38402247pbb.42 - gsmtp
RCPT TO:
250 2.1.5 OK er3sm38405830pbb.40 - gsmtp
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
Is this any issue with trial version of EA send mail?
ivan
  • ivan
  • 100% (Exalted)
  • Administration
11 years ago
Could you show me your source code?

If you re-use Mail instance, after called SendMail, please remember to call

oMail.To.Clear();

To remove current recipients and add new recipients to send another email.
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
Hello Ivan,

Please check below code ,I am using this code to send email When sender and receiver are same.


for (int i = 0; i < MaxThreads; i++)
{
tasks[i] = Task.Factory.StartNew(() =>
{

while (!PendngMails.IsCompleted)
{

SmtpClient mailSmtp = null;
try
{
var item = PendngMails.Take();

mailSmtp = new SmtpClient();


mailSmtp.RcptToErrorContinue = true;
mailSmtp.Tag = item;

SmtpMail oMail = new SmtpMail("TryIt");

string defultsender = "Dummy Email";
oMail.From = new MailAddress("" + defultsender + "<" + item.MailFrom + ">");

oMail.To = item.MailTo;
oMail.Subject = "Test Email";
oMail.TextBody = "This is test email.";
SmtpServer oServer = new SmtpServer("smtp.gmail.com");
oServer.Protocol = ServerProtocol.SMTP;

oServer.User = item.MailFrom;
oServer.Password = item.Emailpwd; ;

oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

mailSmtp.LogFileName = @"C:\Log.txt";
mailSmtp.SendMail(oServer, oMail);

oMail.To.Clear();
Console.WriteLine("Mail sent");

_CrossThreadSetItemText(mailSmtp.Tag, "Success");
}
catch (Exception ex)
{

_CrossThreadSetItemText(mailSmtp.Tag, ex.Message);
}
}

});
}
ivan
  • ivan
  • 100% (Exalted)
  • Administration
11 years ago
You are using multiple threads

in your codes:

for (int i = 0; i < MaxThreads; i++)

You send the same email for MaxThreads times. If you only want to send one email, please don't use for ....
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
I want to send multiple email so i used MaxThreads here.
Is there any solution of this problem which can prevent duplicate email when From=To ?
ivan
  • ivan
  • 100% (Exalted)
  • Administration
11 years ago
sure it is possible.

But please tell me what is PendngMails and Item.To? Is it an array?
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
When i use simple email code with c# using above sample code the no duplicate mails send even From=To.
Then why this issue occur when use your code to send email?
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
PendngEmail is BlockingCollection of Email class.
Item reference of Email class.
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
As you saw our sample code we use Console.WriteLine("Mail sent");
Its shows only 4 times if i want send 4 mails.
PendngMails.Take don't take duplicated emails,BlockingCollection is also thread safe.
Same code works smooth with System.Net.Mail
ivan
  • ivan
  • 100% (Exalted)
  • Administration
11 years ago
I just wrote a simple project, but it works fine.

Here are my codes:




namespace BlockSending
{
    class Program
    {
        static void Main(string[] args)
        {
            MailSending send = new MailSending();
            send.Test();
        }
    }

    class MailSending
    {
        BlockingCollection<string> PendngMails = new BlockingCollection<string>() ;
        public void Test()
        {
            for (int i = 0; i < 4; i++)
            {
                PendngMails.Add("ivan@mydomain.com");
            }
            PendngMails.CompleteAdding();

            Task[]  tasks = new Task[10];
            for (int i = 0; i < 10; i++)
            {
                tasks[i] = Task.Factory.StartNew(() =>
                {

                    while (!PendngMails.IsCompleted)
                    {

                        SmtpClient mailSmtp = null;
                        try
                        {
                            var item = PendngMails.Take();

                            mailSmtp = new SmtpClient();


                            mailSmtp.RcptToErrorContinue = true;
                            mailSmtp.Tag = item;

                            SmtpMail oMail = new SmtpMail("TryIt");

                            string defultsender = "Dummy Email";
                            oMail.From = new MailAddress("" + defultsender + "<" + item + ">");

                            oMail.To = item;
                            oMail.Subject = "Test Email";
                            oMail.TextBody = "This is test email.";
                            SmtpServer oServer = new SmtpServer("localhost");
                            oServer.Protocol = ServerProtocol.SMTP;



                            oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                            //mailSmtp.LogFileName = @"C:\Log.txt";
                            mailSmtp.SendMail(oServer, oMail);

                            oMail.To.Clear();
                            Console.WriteLine("Mail sent");

                           // _CrossThreadSetItemText(mailSmtp.Tag, "Success");
                        }
                        catch (Exception ex)
                        {

                           // _CrossThreadSetItemText(mailSmtp.Tag, ex.Message);
                        }
                    }

                });


               
            }
            System.Threading.Tasks.Task.WaitAll(tasks);
        }
    }
}

So could you send me your project so that I can have a test?

Another possibility is:

Here are two email addresses in Item.MailTo

you can add:

Console.WriteLine("Mail sent");
Console.WriteLine( Mail.To.ToString());

to see how many email addresses in Mail.To
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
Hello Ivan,

I also try sample code with localhost its work fine, Can you try same code when server is smtp.gmail.com and FROM=TO ?
ivan
  • ivan
  • 100% (Exalted)
  • Administration
11 years ago
Yes, I just tested it with my gmail account, no duplicated emails.

Another possibility is:

Here are two email addresses in Item.MailTo

you can add:

Console.WriteLine("Mail sent");
Console.WriteLine( Mail.To.ToString());

to see how many email addresses in Mail.To
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
I used your code mentioned in your comment.
Console.WriteLine("Mail sent");
Console.WriteLine( Mail.To.ToString());

Here is output window trace log:

'TestApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Mail sent

A first chance exception of type 'System.NullReferenceException' occurred in TestApp.exe
Mail sent

A first chance exception of type 'System.NullReferenceException' occurred in TestApp.exe
A first chance exception of type 'System.NullReferenceException' occurred in TestApp.exe
Mail sent

A first chance exception of type 'System.NullReferenceException' occurred in TestApp.exe
Mail sent

The thread '' (0x1cc8) has exited with code 0 (0x0).
The thread '' (0x15ec) has exited with code 0 (0x0).
The thread '' (0x1808) has exited with code 0 (0x0).
The thread '' (0x1b7c) has exited with code 0 (0x0).


But still mails are duplicate
ivan
  • ivan
  • 100% (Exalted)
  • Administration
11 years ago

I am sorry, it is really weird

Console.WriteLine("Mail sent");
Console.WriteLine(mailSmtp.SmtpConversation);


this displays SMTP conversation, you can see how many RCPT TO sent for each SMTP conversation.

It is only about your gmail account or every email with same From, To?
ashish
  • ashish
  • 50.25% (Neutral)
  • Newbie Topic Starter
11 years ago
Hi Ivan,

I noticed that this issue occurs with my gmail account not with others.
If I come across with any issue, I will let you know.
Thanks for answering my questions patiently.

EXPLORE TUTORIALS

© All Rights Reserved, AIFEI Software Limited & AdminSystem Software Limited.