通过WPF发送电子邮件使应用程序关闭

时间:2016-02-06 19:20:51

标签: c# wpf email crash send

当我尝试使用环境(Visual Studio 2015)在计算机上发送电子邮件时,一切都很好但是当我将其复制到另一台计算机(VMWare虚拟机)并尝试运行它时,这是主要代码:

    try
    {
        SmtpClient client = new SmtpClient();
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("bazymysql@gmail.com", "xyz");

        MailMessage mm = new MailMessage("donotreply@domain.com", "bazymysql@gmail.com", "Przechwycony ciag znakow", tresc.Text);
        mm.BodyEncoding = UTF8Encoding.UTF8;
        mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

        client.Send(mm);
    }
    catch (SmtpException ex)
    {
        MessageBox.Show(ex.Message);
    }

我尝试了什么:

  • 复制System.Net dll
  • 开发86x 64x
  • 以管理员身份运行

1 个答案:

答案 0 :(得分:0)

也许您可以尝试以下

try
{
    SmtpClient client = new SmtpClient();
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    client.Timeout = 10000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("bazymysql@gmail.com", "xyz");

    MailMessage mm = new MailMessage("donotreply@domain.com", "bazymysql@gmail.com", "Przechwycony ciag znakow", tresc.Text);
    mm.BodyEncoding = UTF8Encoding.UTF8;
    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    Thread sendMailThread = new Thread(()=>{
        client.Send(mm); //This will ensure that your sending happens on a second thread and that it does not crash your main thread.
    });
    sendMailThread.Start();
}
catch (SmtpException ex)
{
    MessageBox.Show(ex.Message);
}