Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi guys

I have a code to send email to my account. The mail is sent but problem is its sent twice while there are no loops. What might be the problem?
Below is the code:

public void sendCancelAppointmentEmail()
    string footer = "If you have any questions about appointment       cancelation,please do not hesitate to call your clinic front desk";
    string subject = "Appointment Canceled";
    string body = "Hi" + " " + "Anele" + "\n\n" + footer;
    MailMessage message = new MailMessage();

    message.From = new MailAddress("");
    message.To.Add(new MailAddress(""));
    message.Subject = subject;
    message.Body = body;

    SmtpClient client = new SmtpClient();
    client.Credentials = new System.Net.NetworkCredential ("", "mypassword");
    client.Port = 00;
    client.Host = "";
    client.EnableSsl = false;
    catch (Exception ex)
Updated 15-Feb-12 5:58am
ZurdoDev 15-Feb-12 11:59am    
Likely you are either calling sendCancelAppointmentEmail twice or you have an issue with your SMTP server. The code looks fine.
[no name] 15-Feb-12 12:41pm    
yes, the code looks fine.
Sergey Alexandrovich Kryukov 15-Feb-12 13:46pm    

1 solution

You see: three people say that your code is fine, so the reason should be outside this code. By some reason, this method itself could be called twice.

Do the following: run it under debugger. Set a break point at the very beginning of the method sendCancelAppointmentEmail. When executions comes to the break point, open the Debug window "Call stack" to see where the call comes from.

Alternatively, use System.Diagnostics.EventLog to write log information to the system log. To look at the stack, the classes System.Diagnostics.StackTrace and System.Diagnostics.StackFrame to format the stack data as some text and save this diagnostic information to the system log which you can read later.

Please see MSDN help and code samples:[^],[^],[^].

For the advanced usage of EventLog, please see my past solutions:
How to create event log under a folder[^],
MsBuild OutPut to the TextBox on the fly in Windows Application[^].

If you use these technique (which you will need to master sooner or later anyway, especially the Debugger), you will be able to find ends in no time.

Good luck,
Share this answer

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