c#从窗体发送邮件不触发

时间:2021-03-03 15:49:44

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

我在用c#编码的WinForm应用程序中有一些奇怪的行为。

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

在我的:private void buttonSave_Click(对象发送者,EventArgs e)

Im calling my function: functions.sendStatusEmail();

我调用我的函数: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.

我是否遗漏了某些内容,或者是否需要手动触发som应用程序事件以运行sendout。

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

(我尝试使用client.SendAsync(mail,null);然后在点击时触发,但邮件为空)

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();
}

2 个解决方案

#1


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.

没有冒犯,但你确定它正在关闭导致发送电子邮件的应用程序。大多数情况下,由于SMTP服务器上的流量,在发送电子邮件和收到电子邮件之间存在延迟。尝试按下该按钮,然后等待一段时间(3-4分钟)并尝试在此期间刷新收件箱。

#2


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

顺便说一下,SendAsync没有用,因为你在异步调用后调用了mail.dispose()。在异步中执行此操作的正确方法是,

private void button1_Click(object sender, EventArgs e)
{
    MailMessage mail = new MailMessage("from@domain.com", "to@domain.com", "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
}

#1


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.

没有冒犯,但你确定它正在关闭导致发送电子邮件的应用程序。大多数情况下,由于SMTP服务器上的流量,在发送电子邮件和收到电子邮件之间存在延迟。尝试按下该按钮,然后等待一段时间(3-4分钟)并尝试在此期间刷新收件箱。

#2


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

顺便说一下,SendAsync没有用,因为你在异步调用后调用了mail.dispose()。在异步中执行此操作的正确方法是,

private void button1_Click(object sender, EventArgs e)
{
    MailMessage mail = new MailMessage("from@domain.com", "to@domain.com", "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
}