Andreas Andreas - 11 days ago 7
ASP.NET (C#) Question

c# sending mail from window form not triggering

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("myFromEmail@hidden.com",email,emailSubject,emailBody);
SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["MailSMTP"]);

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

// dispose
mail.Dispose();
}

Answer

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.