Click here to Skip to main content
15,949,686 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:

C#
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("Anele.Ngqandu@myaccount.co.za");
    message.To.Add(new MailAddress("Anele.Ngqandu@yahoo.co.za"));
    message.Subject = subject;
    message.Body = body;

    SmtpClient client = new SmtpClient();
    client.Credentials = new System.Net.NetworkCredential ("Anele@myaccount.co.za", "mypassword");
    client.Port = 00;
    client.Host = "mail.myaccount.co.za";
    client.EnableSsl = false;
    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}
Posted
Updated 15-Feb-12 5:58am
v2
Comments
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    
Agree.
--SA

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:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stackframe.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

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,
—SA
 
Share this answer
 
v3

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