Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Duplicate/Triplicate E-mails: .NET Framework 4.5.2; public class SmtpClient : IDisposable; public void Send (System.Net.Mail.MailMessage message)

$
0
0

Hi, Everybody!...

I have noticed that when I send an e-mail using the subject method with exactly oneTo MailAddress and an attachment, it sends exactly one e-mail.  But when I send an e-mail using the same method with severalTo MailAddresses, it sometimes inconsistently sends multiple e-mails to the same distribution...  Sometimes duplicates...  Sometimes triplicates.  What I mean byinconsistently is that for the same distribution, each recipient will either receive one, two or three identical e-mails.  The next time it might be different.  I have never seen more than three.  I cannot see a pattern.

I know that the application is not calling the method repeatedly.  I log a message ("...sending emails") and then I send the e-mail.

    this.Log().DebugFormat("Job '{0}' sending emails", name);
    EmailClient objEmailClient = new EmailClient();
    objEmailClient.SendEmail(filepath, name, subject, recipients);

See EmailClient excerpt below...

  :
  :
    lock (_SendEmailLock)
    {
        // Some initializations...

        var objMailMessage = new MailMessage();
        var emailAddresses = distribution.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

        foreach (var emailAddress in emailAddresses)
        {
            objMailMessage.To.Add(new MailAddress(emailAddress.Trim()));
        }

        objMailMessage.From = new MailAddress(from);
        objMailMessage.Subject = subject;
        objMailMessage.Body = body;
        objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
        objMailMessage.Attachments.Add(attachment);

        var objSmtpClient = new SmtpClient();

        objSmtpClient.Host = strSmtpServer;
        objSmtpClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]);
        objSmtpClient.UseDefaultCredentials = false;
        objSmtpClient.Credentials = new System.Net.NetworkCredential(userName, password);
        objSmtpClient.Send(objMailMessage);

        objMailMessage.Attachments.Dispose();
        objMailMessage.Dispose();
    }
  :
  :

As you can see, the subject method is enveloped in a lock statement.

How can multiple e-mails be explained?


Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>