views:

563

answers:

3

Hello. I'm having some wierd behaviour in my WinForm application coded in c#.

in my: private void buttonSave_Click(object sender, EventArgs e)

Im calling my function: functions.sendStatusEmail();

The weird thing is that, when i press the Save button then email send is not triggered. But if I close my application the mail is handle and sent.

Have I missed something or do one need to trigger som application event manually to have run the sendout.

( I tried using client.SendAsync(mail,null); then it triggerd at click but the mail was empty)

Thanks in advance

-- Edit: code expamples

private void buttonSave_Click(object sender, EventArgs e)
{
    // checks if a ticket is active
    if (workingTicketId > 0)
    {
        // update ticket information
        functions.updateTicketInfo(workingTicketId, comboBoxPriority.SelectedIndex, 
                                    comboBoxStatus.SelectedIndex, textBoxComment.Text);

        // gives feedback
        labelFeedback.Text = "Updated";

        // updates the active ticket list
        populateActiveTicketList();

        // marks working ticket row in list
        dataGridActiveTicketList.Rows[workingGridIndex].Selected = true;

        // checks for change of ticket status
        if (comboBoxStatus.SelectedIndex != workingTicketStatus)
        {
            // checks if contact person exists
            if (labelContactPersonValue.Text.ToString() != "")
            {
                // sends email to contact person
                functions.sendStatusEmail(labelContactPersonValue.Text, comboBoxStatus.SelectedIndex, workingTicketId, textBoxDescription.Text);
            }

            // updates working ticket status
            workingTicketStatus = comboBoxStatus.SelectedIndex;
        }

    }
}

and the send email function:

   // sends a status email to contact person   
    // returns noting
    public void sendStatusEmail(string email, int newStatus, int ticketId, string ticketText)
    {
        // defines variables
        string emailSubject;
        string emailBody;


        // some exkluded mailcontent handling
    //


        // sends mail
        MailMessage mail = new MailMessage("[email protected]",email,emailSubject,emailBody);
        SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["MailSMTP"]);

        mail.IsBodyHtml = true;
        client.Send(mail);

        // dispose
        mail.Dispose();
    }
+1  A: 

Cannot understand why it would not work. I used below function and it sends the email successfully:

public static bool SendEmail (string smtpServer, string fromAddress, string fromDisplayName,
    string toAddress, string subject, string contents, bool important) {

    MailAddress from = new MailAddress (fromAddress, fromDisplayName);
    MailPriority priority = important ? MailPriority.High : MailPriority.Normal;

    MailMessage m = new MailMessage {
        From = from,
        Subject = subject,
        Body = contents,
        Priority = priority,
        IsBodyHtml = false
    };

    MailAddress to = new MailAddress (toAddress);
    m.To.Add (to);

    SmtpClient c = new SmtpClient (smtpServer) { UseDefaultCredentials = false };

    c.Send (m);
    return true;
}

No offence but are you sure that it is closing the application which results in email being sent. Most of the times there is a delay between an email is sent and it is received because of the traffic on SMTP server. Try pressing that button and then wait for some time (3-4 minutes) and try refreshing your inbox during that time.

Hemant
No offence taken, Im quite new to .net alltogheter, and my conclusions does not make sense to me, but I have tried waiting with no result. The one thing is that when I close my application my desktop outlook gives the system icon of sending a mail. And then I recive it. And when I dont close the application I get a error from Norton saying something in the terms mail could not be sent due to timeout or so.
Andreas
Your desktop outlook has *nothing* to do with *sending* an email. Perhaps because you see the icon because you *receive* the email. To confirm, try closing the outlook and you should still be able to send emails. About Norton: Is it a message from Norton Anti-virus?
Hemant
Yep its from the antivir but not the regular kind. Anyhow, I tried to run it from another box and it worked, So I guess it has something todo with the Norton on my box. Thank you for your efforts, and sorry for the waste of time.
Andreas
I hear "It works on *my* machine" from developers but "doesn't work on developer's machine" is certainly a rare case. Good luck!
Hemant
The only real difference between Hemant's code and asd81 is the lack of a call to MailMessage.Dispose... I also notice in the example (http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx) the only call to Dispose is for the attachment, rather than the MailMessage.
Zhaph - Ben Duguid
+1  A: 

BTW, the SendAsync didnt work because you called mail.dispose() after the Async call. The right way to do it in async would be,

private void button1_Click(object sender, EventArgs e)
        {
            MailMessage mail = new MailMessage("[email protected]", "[email protected]", "subj", "body");
            SmtpClient client = new SmtpClient("SMTPHOST");
            mail.IsBodyHtml = true;
            client.SendCompleted += new
            SendCompletedEventHandler(SendCompletedCallback);

            client.SendAsync(mail,mail);//send the mail object itself as argument to callback


        }
        private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                //Mail sending is cancelled
            }
            if (e.Error != null)
            {
                //There is an error,e.Error will contain the exception
            }
            else
            {
                //Do any other success processing
            }

            ((MailMessage)e.UserState).Dispose();//Dispose
        }
Gee
A: 

I am also facing same problem and solution is not working. I have developed one applicaiton to continously Monitor Shared drive space one space goes below set threshold level it should send an email. And again because of Timer it should send mail again again... But the problem is that it is not sending all the or even single mail until i close my application..