4

For every e-mails that my website sends I have it send myself an email as well. I noticed that about 5% of the e-mails (Thousands a day) are being sent twice. The time between the duplicates being sent however is completely inconsistent...Can be a week, a month or a minute. Completely inconsistent...I've tried to debug the issue but I can never duplicate the issue. Always just sends one e-mail and works properly. I'm out of ideas so decided to try and gain some input form the community. Any insight would be greatly appreciated...

public static void SendEmail(string subject, string body, string toAddress, string fromAddress)
    {
        MailMessage msg = new MailMessage();

        msg.From = new MailAddress(fromAddress);
        msg.To.Add(new MailAddress(toAddress));
        msg.Subject = subject;
        msg.Body = body;
        msg.IsBodyHtml = false;

        using (SmtpClient cli = new SmtpClient())
        {
            cli.Send(msg);
            cli.Dispose();
        }
    }

Dispose() was just one of my recent attempts that have failed to fix the problem....

And the ascx page that calls the function (note that it is called twice to send it to myslef the first time and the customer the second time if there e-mail address is not null. I did this during testing and decided to leave it in there:

Txp.SendEmail(strOrderConfirmationHeader, strOrderConfirmationText, TxpConst.ORDERNOTIFYADDR, TxpConst.RETURNADDR);

if (!String.IsNullOrWhiteSpace(custemail))
{
    Txp.SendEmail(strOrderConfirmationHeader, strOrderConfirmationText, custemail, TxpConst.RETURNADDR);
}

The fact that it's so inconsistent tells me it's probably not the code and it's not the e-mail server cause the mail logs show a completed transaction every time and it gets a different message id every time...I'm all out of ideas!

mmcclur2
  • 41
  • 1
  • 3
  • If you want to send to two people, instead of calling it twice, just add two people to the `To` collection on the physical `MailMessage`. Is this the only entry point to that `SendMail` method? – Arran Oct 04 '13 at 13:42
  • Or alternatively BCC them if you don't want the original recipient to see the alternate email address. – Ben Robinson Oct 04 '13 at 13:43
  • Your send email code would only send one email. To debug this kind of things you need to add some kind of logging so you can trace back where these emails are coming from. – Ben Robinson Oct 04 '13 at 13:44
  • No it's used elsewhere also but with different subjects...The fact that it can be a week, a month or hours is telling me it's not the e-mail code and maybe something with sessions...? I'm not sure...I've seen a duplicate e-mail get sent a year later.... – mmcclur2 Oct 04 '13 at 13:48
  • For instance I have three copies of the same e-mail in my inbox, One from Wed 6:21, Mon 3:26, Sun 7:24... – mmcclur2 Oct 04 '13 at 13:53
  • Are you sure that your emails are unique? – MadDeveloper Oct 04 '13 at 14:12
  • Hmm... Someone, twice a week presses F5, posting your mail form back again, and keep it in secret.. :) – Dmitry Oct 04 '13 at 14:32
  • Are you using exchange or some other smtp server to send the email? I've always found the smtp service in IIS to be really flaky (silent fail) and switched to using gmail as an SMTP service / pass through. The good thing about this technique is that you have an audit trail of emails sent. I haven't done this through Exchange but I'm sure you could do something similar. The point I think I'm trying to make is that I can't see a problem with the code above and if you're receiving duplicate emails even hours apart it suggests the emails are being possibly being queued and retried. – Damon Oct 04 '13 at 15:09
  • @Dmitry haha yes that's what I'm thinking...Sessions ending and refreshing the page...But that kind of goes out the window when the time frame is weeks or monthes when the duplicates is being sent. I just added a dispose to mailmessage and I commented out the one sendmail method that sends to myself and just added my email to the bcc in the sendmail function like you guys reccomended... – mmcclur2 Oct 04 '13 at 15:17
  • @MadDeveloper Yes e-mails definitely identical.....Except the time sent of course. – mmcclur2 Oct 04 '13 at 15:18
  • @Damon, Not using exchange...Using Unix exim. Thank you for your input. We have logging...It's not the mail server but like you said maybe something in IIS... – mmcclur2 Oct 04 '13 at 15:22
  • How big are the emails in size? Do they have large attachments or are we talking basic text emails, couple of K each? – SimonGoldstone Oct 23 '13 at 00:00
  • Talking just basic text... – mmcclur2 Oct 30 '13 at 17:38
  • Still not resolved...Any ideas welcome – mmcclur2 Feb 06 '14 at 19:44

1 Answers1

0

OK, I'm sure you've already considered this, but are you sure your visitor isn't hitting "submit" twice?

I've seen this many times, particularly when the email server is running a little slowly. User thinks the site isn't doing anything so clicks Submit again. (Or they just double-click the button - seen that too!)

You might just need to put some code your app to guard against multiple submission. It's a fairly common issue.

Hope this helps you.

SimonGoldstone
  • 5,096
  • 3
  • 27
  • 38
  • Although, having read your comments more thoroughly, this would not explain the week/month delay, only seconds. – SimonGoldstone Oct 22 '13 at 23:58
  • Yea, still have no luck...There just doesn't seem to be a logical reason for this behavior... – mmcclur2 Oct 30 '13 at 17:37
  • 2
    Check the headers of the emails. Usually, you'll see a unique ID in there. Example: `Message-ID: <1A2BEF3B84C62F44A4304674913B166A3A58E6E1@EXCH001.xxxxx.local>` Is the ID the same for the duplicates or is it a different ID? If it's the same, then it sounds like something in the transport layer or email server itself. If it's different, then it would suggest it's something on the client end instead. At least this will help you divide and conquer. – SimonGoldstone Nov 01 '13 at 16:49